Skip to content

Commit

Permalink
improve email validation. rockstor#688
Browse files Browse the repository at this point in the history
  • Loading branch information
schakrava committed Jun 29, 2015
1 parent 585a3a9 commit ddec2ad
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 144 deletions.
9 changes: 5 additions & 4 deletions src/rockstor/storageadmin/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
</div>
</div>
<div class="form-group">
<% if (username == null) { %>
<% if (!username) { %>
<label class="col-sm-4 control-label">Password<span class="required"> *</span></label>
<% } else { %>
<label class="col-sm-4 control-label">Password<span class="required"></span></label>
Expand All @@ -82,7 +82,7 @@
</div>
</div>
<div class="form-group">
<% if (username == null) { %>
<% if (!username) { %>
<label class="col-sm-4 control-label">Confirm Password<span class="required"> *</span></label>
<% } else { %>
<label class="col-sm-4 control-label">Confirm Password<span class="required"></span></label>
Expand Down Expand Up @@ -116,15 +116,15 @@
</select>
</div>
</div>
<% if (username == null) { %>
<% if (!username) { %>
<div class="form-group">
<label class="col-sm-4 control-label">UID</label>
<div class="col-sm-8 controls">
<input class="form-control shorten-input" type="text" id="uid" name="uid" placeholder="Create a new one" title="Enter a custom uid here or one will be autogenerated">
</div>
</div>
<% } %>
<% if (username == null) { %>
<% if (!username) { %>
<div class="form-group">
<label class="col-sm-4 control-label">Group</label>
<div class="col-sm-8 controls">
Expand All @@ -139,10 +139,15 @@
<% } %>
<div class="form-group">
<label class="col-sm-4 control-label">Email</label>
<div class="col-sm-8 controls">
<input class="form-control shorten-input" type="text" id="email" name="email" title="Enter an email address to associate with the user">
<div class="col-sm-8 controls">
<% if (!username) { %>
<input class="form-control shorten-input" type="text" id="email" name="email" title="Enter an email address to associate with the user">
<% } else { %>
<input class="form-control shorten-input" type="text" id="email" name="email" title="Enter an email address to associate with the user" value="<%= user.get('email') %>">
<% } %>
</div>
</div>
<% >
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<button id="cancel" class="btn btn-default">Cancel</button>
Expand Down
273 changes: 139 additions & 134 deletions src/rockstor/storageadmin/static/storageadmin/js/views/add_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

});

0 comments on commit ddec2ad

Please sign in to comment.