Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin panel allow all emails option #41

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/client/src/services/SettingsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ angular.module('reg')
allowMinors: allowMinors
});
},
updateAllowAllEmails: function(allowAllEmails){
return $http.put(base + 'allEmails', {
allowAllEmails: allowAllEmails
});
},
};

}
Expand Down
12 changes: 12 additions & 0 deletions app/client/views/admin/settings/adminSettingsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ angular.module('reg')
});
};

$scope.updateAllowAllEmails = function () {
SettingsService
.updateAllowAllEmails($scope.settings.allowAllEmails)
.success(function (data) {
$scope.settings.allowAllEmails = data.allowAllEmails;
const successText = $scope.settings.allowAllEmails ?
"All emails are now allowed to register." :
"Only whitelisted emails are now allowed to register."
swal("Looks good!", successText, "success");
});
};

// Whitelist --------------------------------------

SettingsService
Expand Down
11 changes: 11 additions & 0 deletions app/client/views/admin/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@
<label>Allow minors</label>
</div>
</div>
<div class="field">
<div class="ui toggle checkbox">
<input
type="checkbox"
name="allowAllEmails"
ng-class="{true: 'checked', false: ''}[settings.allowAllEmails]"
ng-model="settings.allowAllEmails"
ng-change="updateAllowAllEmails()">
<label>Allow all emails</label>
</div>
</div>
</div>
</div>
</div>
Expand Down
47 changes: 37 additions & 10 deletions app/server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ function endsWith(s, test){
return test.indexOf(s, test.length - s.length) !== -1;
}

/**
* Determine whether or not a registration email is whitelisted.
* @param {String} email Email of the user
* @param {Function} callback args(err, true, false)
* @return {[type]} [description]
*/
function checkWhitelistedEmails(email, callback) {
// Check for emails.
Settings.getWhitelistedEmails(function (err, emails) {
if (err || !emails) {
return callback(err);
}
for (var i = 0; i < emails.length; i++) {
if (endsWith(emails[i], email)) {
return callback(null, true);
}
}
return callback({
message: "Not a valid educational email."
}, false);
});
}

/**
* Determine whether or not a user can register.
* @param {String} email Email of the user
Expand Down Expand Up @@ -49,19 +72,23 @@ function canRegister(email, password, callback){
});
}

// Check for emails.
Settings.getWhitelistedEmails(function(err, emails){
if (err || !emails){
if(!validator.isEmail(email)){
return callback({
message: "Not a valid email."
}, false);
}

Settings.getPublicSettings(function (err, settings) {
if (err || !settings) {
return callback(err);
}
for (var i = 0; i < emails.length; i++) {
if (validator.isEmail(email) && endsWith(emails[i], email)){
return callback(null, true);
}

// Check for all emails allowed.
if (settings.allowAllEmails) {
return callback(null, true);
} else {
checkWhitelistedEmails(email, callback);
}
return callback({
message: "Not a valid educational email."
}, false);
});

});
Expand Down
3 changes: 3 additions & 0 deletions app/server/models/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ var schema = new mongoose.Schema({
},
allowMinors: {
type: Boolean
},
allowAllEmails: {
type: Boolean
}
});

Expand Down
13 changes: 13 additions & 0 deletions app/server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,17 @@ module.exports = function(router) {
SettingsController.updateField('allowMinors', allowMinors, defaultResponse(req, res));
});

/**
* [ADMIN ONLY]
* {
* allowAllEmails: Boolean
* }
* res: Settings
*
*/
router.put('/settings/allEmails', isAdmin, function(req, res){
var allowAllEmails = req.body.allowAllEmails;
SettingsController.updateField('allowAllEmails', allowAllEmails, defaultResponse(req, res));
});

};