diff --git a/app/client/src/services/SettingsService.js b/app/client/src/services/SettingsService.js
index f2789b3e1..96afe9eda 100644
--- a/app/client/src/services/SettingsService.js
+++ b/app/client/src/services/SettingsService.js
@@ -48,6 +48,11 @@ angular.module('reg')
allowMinors: allowMinors
});
},
+ updateAllowAllEmails: function(allowAllEmails){
+ return $http.put(base + 'allEmails', {
+ allowAllEmails: allowAllEmails
+ });
+ },
};
}
diff --git a/app/client/views/admin/settings/adminSettingsCtrl.js b/app/client/views/admin/settings/adminSettingsCtrl.js
index c534ca89c..60be091d7 100644
--- a/app/client/views/admin/settings/adminSettingsCtrl.js
+++ b/app/client/views/admin/settings/adminSettingsCtrl.js
@@ -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
diff --git a/app/client/views/admin/settings/settings.html b/app/client/views/admin/settings/settings.html
index ae67b32f0..9d711263b 100644
--- a/app/client/views/admin/settings/settings.html
+++ b/app/client/views/admin/settings/settings.html
@@ -99,6 +99,17 @@
+
+
+
+
+
+
diff --git a/app/server/controllers/UserController.js b/app/server/controllers/UserController.js
index 4dac18817..cf4ecab5b 100644
--- a/app/server/controllers/UserController.js
+++ b/app/server/controllers/UserController.js
@@ -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
@@ -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);
});
});
diff --git a/app/server/models/Settings.js b/app/server/models/Settings.js
index 18eb3c1bb..2af2cccc1 100644
--- a/app/server/models/Settings.js
+++ b/app/server/models/Settings.js
@@ -39,6 +39,9 @@ var schema = new mongoose.Schema({
},
allowMinors: {
type: Boolean
+ },
+ allowAllEmails: {
+ type: Boolean
}
});
diff --git a/app/server/routes/api.js b/app/server/routes/api.js
index 4be14e954..e597a381e 100644
--- a/app/server/routes/api.js
+++ b/app/server/routes/api.js
@@ -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));
+ });
+
};