From ddec2ada76308befad21fff860e298cfed871508 Mon Sep 17 00:00:00 2001 From: Suman Chakravartula Date: Mon, 29 Jun 2015 14:17:33 -0700 Subject: [PATCH] improve email validation. #688 --- src/rockstor/storageadmin/models/user.py | 9 +- .../js/templates/users/add_user.jst | 17 +- .../static/storageadmin/js/views/add_user.js | 273 +++++++++--------- 3 files changed, 155 insertions(+), 144 deletions(-) diff --git a/src/rockstor/storageadmin/models/user.py b/src/rockstor/storageadmin/models/user.py index 88824b91c..40a42395d 100644 --- a/src/rockstor/storageadmin/models/user.py +++ b/src/rockstor/storageadmin/models/user.py @@ -18,25 +18,26 @@ from django.db import models from django.contrib.auth.models import User as DjangoUser +from django.core.validators import validate_email from django.conf import settings from storageadmin.models import Group import grp class User(models.Model): - user = models.OneToOneField(DjangoUser, null=True, + user = models.OneToOneField(DjangoUser, null=True, blank=True, related_name='suser') username = models.CharField(max_length=4096, unique=True, default='') uid = models.IntegerField(default=settings.START_UID) gid = models.IntegerField(default=settings.START_UID) - public_key = models.CharField(max_length=4096, null=True) + public_key = models.CharField(max_length=4096, null=True, blank=True) smb_shares = models.ManyToManyField('SambaShare', null=True, related_name='admin_users') shell = models.CharField(max_length=1024, null=True) homedir = models.CharField(max_length=1024, null=True) - email = models.CharField(max_length=1024, null=True) + email = models.CharField(max_length=1024, null=True, blank=True, validators=[validate_email]) admin = models.BooleanField(default=True) - group = models.ForeignKey(Group, null=True) + group = models.ForeignKey(Group, null=True, blank=True) @property def groupname(self, *args, **kwargs): diff --git a/src/rockstor/storageadmin/static/storageadmin/js/templates/users/add_user.jst b/src/rockstor/storageadmin/static/storageadmin/js/templates/users/add_user.jst index c80992bd1..ff8d17ef4 100644 --- a/src/rockstor/storageadmin/static/storageadmin/js/templates/users/add_user.jst +++ b/src/rockstor/storageadmin/static/storageadmin/js/templates/users/add_user.jst @@ -72,7 +72,7 @@
- <% if (username == null) { %> + <% if (!username) { %> <% } else { %> @@ -82,7 +82,7 @@
- <% if (username == null) { %> + <% if (!username) { %> <% } else { %> @@ -116,7 +116,7 @@
- <% if (username == null) { %> + <% if (!username) { %>
@@ -124,7 +124,7 @@
<% } %> - <% if (username == null) { %> + <% if (!username) { %>
@@ -139,10 +139,15 @@ <% } %>
-
- +
+ <% if (!username) { %> + + <% } else { %> + + <% } %>
+ <% >
diff --git a/src/rockstor/storageadmin/static/storageadmin/js/views/add_user.js b/src/rockstor/storageadmin/static/storageadmin/js/views/add_user.js index 36eb62a41..277dc43e6 100644 --- a/src/rockstor/storageadmin/static/storageadmin/js/views/add_user.js +++ b/src/rockstor/storageadmin/static/storageadmin/js/views/add_user.js @@ -25,147 +25,152 @@ */ AddUserView = RockstorLayoutView.extend({ - events: { - "click #cancel": "cancel" - }, + events: { + "click #cancel": "cancel" + }, - initialize: function() { - // call initialize of base - this.constructor.__super__.initialize.apply(this, arguments); - // set template - this.template = window.JST.users_add_user; - this.username = this.options.username; - if (!_.isUndefined(this.username)) { - this.user = new User({username: this.username}); - this.dependencies.push(this.user); - } else { - this.user = new User(); - } - this.groups = new GroupCollection(); - this.groups.pageSize = RockStorGlobals.maxPageSize; - this.dependencies.push(this.groups); - }, + initialize: function() { + // call initialize of base + this.constructor.__super__.initialize.apply(this, arguments); + // set template + this.template = window.JST.users_add_user; + this.username = this.options.username; + if (!_.isUndefined(this.username)) { + this.user = new User({username: this.username}); + this.dependencies.push(this.user); + } else { + this.user = new User(); + } + this.groups = new GroupCollection(); + this.groups.pageSize = RockStorGlobals.maxPageSize; + this.dependencies.push(this.groups); + }, + + render: function() { + this.fetch(this.renderUserForm, this); + return this; + }, - render: function() { - this.fetch(this.renderUserForm, this); - return this; - }, + renderUserForm: function() { + var _this = this; + $(this.el).html(this.template({ + username: this.username, + user: this.user, + groups: this.groups - renderUserForm: function() { - var _this = this; - //$(this.el).html(this.template()); - $(this.el).html(this.template({ - username: this.username, - user: this.user, - groups: this.groups + })); - })); + this.$('#user-create-form :input').tooltip({placement: 'right'}); + this.$('#group').chosen(); - this.$('#user-create-form :input').tooltip({placement: 'right'}); - this.$('#group').chosen(); + this.validator = this.$("#user-create-form").validate({ + onfocusout: false, + onkeyup: false, + rules: { + username: "required", + password: { + required: { + depends: function(element) { + return _this.username == null || _this.username == undefined; + } + } + }, + password_confirmation: { + required: { + depends: function(element) { + return _this.username == null || _this.username == undefined; + } + }, + equalTo: "#password" + } + }, + messages: { + password_confirmation: { + equalTo: "The passwords do not match" + } + }, - this.validator = this.$("#user-create-form").validate({ - onfocusout: false, - onkeyup: false, - rules: { - username: "required", - password: { - required: { - depends: function(element) { - return _this.username == null || _this.username == undefined; - } - } - }, - password_confirmation: { - required: { - depends: function(element) { - return _this.username == null || _this.username == undefined; - } - }, - equalTo: "#password" - } - }, - messages: { - password_confirmation: { - equalTo: "The passwords do not match" - } - }, + submitHandler: function() { + var username = _this.$("#username").val(); + var password = _this.$("#password").val(); + var admin = _this.$("#admin").prop("checked"); + var shell = _this.$("#shell").val(); - submitHandler: function() { - var username = _this.$("#username").val(); - var password = _this.$("#password").val(); - var admin = _this.$("#admin").prop("checked"); - var shell = _this.$("#shell").val(); - - var public_key = _this.$("#public_key").val(); - if (_.isEmpty(public_key)) { - public_key = null; - } - var uid = _this.$("#uid").val(); - if (_.isEmpty(uid)) { - uid = null; - } - var group = _this.$("#group").val(); - if (group == 'Create a new one') { - group = null - } - if(_this.username != null && _this.username != undefined){ - if (!_.isEmpty(password)) { - _this.user.set({password: password}); - } else { - _this.user.unset('password'); - } - if (!_.isEmpty(public_key)) { - _this.user.set({public_key: public_key}); - } else { - _this.user.unset('public_key'); - } - - _this.user.set({admin: admin}); - _this.user.set({group: group}); - _this.user.set({shell: shell}); - _this.user.save(null, { - success: function(model, response, options) { - app_router.navigate("users", {trigger: true}); - }, - error: function(model, xhr, options) { - } - }); - } else { - // create a dummy user model class that does not have idAttribute - // = username, so backbone will treat is as a new object, - // ie isNew will return true - var tmpUserModel = Backbone.Model.extend({ - urlRoot: "/api/users" - }); - var user = new tmpUserModel() - user.save({ - username: username, - password: password, - admin: admin, - group: group, - shell: shell, - uid: uid, - public_key: public_key - }, { - success: function(model, response, options) { - _this.$('#user-create-form :input').tooltip('hide'); - app_router.navigate("users", {trigger: true}); - }, - error: function(model, xhr, options) { - _this.$('#user-create-form :input').tooltip('hide'); - } - }); - } - return false; - } - }); - return this; - }, + var public_key = _this.$("#public_key").val(); + if (_.isEmpty(public_key)) { + public_key = null; + } + var uid = _this.$("#uid").val(); + if (_.isEmpty(uid)) { + uid = null; + } + var group = _this.$("#group").val(); + if (group == 'Autogenerate') { + group = null; + } + var email = _this.$("#email").val(); + if (_.isEmpty(email)) { + email = null; + } + if(_this.username != null && _this.username != undefined){ + if (!_.isEmpty(password)) { + _this.user.set({password: password}); + } else { + _this.user.unset('password'); + } + if (!_.isEmpty(public_key)) { + _this.user.set({public_key: public_key}); + } else { + _this.user.unset('public_key'); + } - cancel: function(event) { - event.preventDefault(); - app_router.navigate("users", {trigger: true}); - } + _this.user.set({admin: admin}); + _this.user.set({group: group}); + _this.user.set({shell: shell}); + _this.user.set({email: email}); + _this.user.save(null, { + success: function(model, response, options) { + app_router.navigate("users", {trigger: true}); + }, + error: function(model, xhr, options) { + } + }); + } else { + // create a dummy user model class that does not have idAttribute + // = username, so backbone will treat is as a new object, + // ie isNew will return true + var tmpUserModel = Backbone.Model.extend({ + urlRoot: "/api/users" + }); + var user = new tmpUserModel(); + user.save({ + username: username, + password: password, + admin: admin, + group: group, + shell: shell, + uid: uid, + public_key: public_key, + email: email + }, { + success: function(model, response, options) { + _this.$('#user-create-form :input').tooltip('hide'); + app_router.navigate("users", {trigger: true}); + }, + error: function(model, xhr, options) { + _this.$('#user-create-form :input').tooltip('hide'); + } + }); + } + return false; + } + }); + return this; + }, + + cancel: function(event) { + event.preventDefault(); + app_router.navigate("users", {trigger: true}); + } });