add validation for users user_management
authorymh <ymh.work@gmail.com>
Tue, 22 Oct 2013 17:54:30 +0200
branchuser_management
changeset 225 0fcce86e650c
parent 224 0167b777ad15
child 226 d2b688aadd8c
add validation for users
server/src/main/java/org/iri_research/renkan/forms/UserForm.java
server/src/main/java/org/iri_research/renkan/forms/UserFormValidator.java
server/src/main/java/org/iri_research/renkan/models/User.java
server/src/main/webapp/WEB-INF/i18n/messages.properties
server/src/main/webapp/WEB-INF/i18n/messages_en.properties
server/src/main/webapp/WEB-INF/i18n/messages_fr.properties
server/src/main/webapp/WEB-INF/spring-security.xml
server/src/main/webapp/WEB-INF/templates/fragment/spaceForm.html
server/src/main/webapp/WEB-INF/templates/fragment/userForm.html
--- a/server/src/main/java/org/iri_research/renkan/forms/UserForm.java	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/java/org/iri_research/renkan/forms/UserForm.java	Tue Oct 22 17:54:30 2013 +0200
@@ -12,13 +12,25 @@
 @Component
 public class UserForm extends RenkanForm<String, User> {
 
+    private String avatar;
+
+    private Date credentialExpirationDate;
+
+    private String email;
+
+    private boolean enabled;
+    private Date expirationDate;
+    private boolean locked;
+    private String password;
+    private String passwordConfirm;
     @Autowired
     private UsersRepository usersRepository;
-
+    
+    
     public UserForm() {
         super();
     }
-
+    
     public UserForm(User model) {
         super(model);
         if (model != null) {
@@ -31,65 +43,52 @@
         }
     }
 
-    private String avatar;
-    private String email;
-    private Date credentialExpirationDate;
-    private Date expirationDate;
-    private boolean enabled;
-    private boolean locked;
-
     public String getAvatar() {
         return avatar;
     }
 
-    public void setAvatar(String avatar) {
-        this.avatar = avatar;
-    }
-
-    public String getEmail() {
-        return email;
-    }
-
-    public void setEmail(String email) {
-        this.email = email;
-    }
-
     public Date getCredentialExpirationDate() {
         return credentialExpirationDate;
     }
 
-    public void setCredentialExpirationDate(Date credentialExpirationDate) {
-        this.credentialExpirationDate = credentialExpirationDate;
+    public String getEmail() {
+        return email;
     }
 
     public Date getExpirationDate() {
         return expirationDate;
     }
 
-    public void setExpirationDate(Date expirationDate) {
-        this.expirationDate = expirationDate;
+    @Override
+    protected User getModelInstance() {
+        return new User();
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public String getPasswordConfirm() {
+        return passwordConfirm;
+    }
+
+    @Override
+    protected IRenkanRepository<User, String> getRepository() {
+        return this.usersRepository;
+    }
+
+    public UsersRepository getUsersRepository() {
+        return usersRepository;
     }
 
     public boolean isEnabled() {
         return enabled;
     }
 
-    public void setEnabled(boolean enabled) {
-        this.enabled = enabled;
-    }
-
     public boolean isLocked() {
         return locked;
     }
 
-    public void setLocked(boolean locked) {
-        this.locked = locked;
-    }
-
-    public UsersRepository getUsersRepository() {
-        return usersRepository;
-    }
-
     @Override
     protected void saveToModel() {
         if (this.getId() == null || this.getId().length() == 0) {
@@ -104,14 +103,36 @@
 
     }
 
-    @Override
-    protected IRenkanRepository<User, String> getRepository() {
-        return this.usersRepository;
+    public void setAvatar(String avatar) {
+        this.avatar = avatar;
+    }
+
+    public void setCredentialExpirationDate(Date credentialExpirationDate) {
+        this.credentialExpirationDate = credentialExpirationDate;
+    }
+
+    public void setEmail(String email) {
+        this.email = email;
     }
 
-    @Override
-    protected User getModelInstance() {
-        return new User();
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+    }
+
+    public void setExpirationDate(Date expirationDate) {
+        this.expirationDate = expirationDate;
+    }
+
+    public void setLocked(boolean locked) {
+        this.locked = locked;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public void setPasswordConfirm(String passwordConfirm) {
+        this.passwordConfirm = passwordConfirm;
     }
 
     @Autowired
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/main/java/org/iri_research/renkan/forms/UserFormValidator.java	Tue Oct 22 17:54:30 2013 +0200
@@ -0,0 +1,48 @@
+package org.iri_research.renkan.forms;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.Errors;
+import org.springframework.validation.ValidationUtils;
+import org.springframework.validation.Validator;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@Component
+public class UserFormValidator implements Validator {
+
+    @SuppressWarnings("unused")
+    private Logger logger = LoggerFactory.getLogger(UserFormValidator.class);
+
+    @Override
+    public boolean supports(Class<?> clazz) {
+        return UserForm.class.equals(clazz);
+    }
+
+    @Override
+    public void validate(Object target, Errors errors) {
+        UserForm userForm = (UserForm) target;
+
+        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title",
+                "renkan.error.title.empty");
+        
+        //TODO : check for user name unicity
+        String pswd = userForm.getPassword();
+        String pswdConf = userForm.getPasswordConfirm();
+        
+        if( (pswd == null && pswdConf != null)
+            || ((pswd != null || pswdConf != null) && !pswd.equals(pswdConf))) {
+            errors.rejectValue("password", "renkan.error.password.equals");
+        }
+        
+        if(userForm.getId() == null && (pswd == null || pswd.length() == 0)) {
+            errors.rejectValue("password", "renkan.error.password.missing");
+        }
+
+    }
+
+}
--- a/server/src/main/java/org/iri_research/renkan/models/User.java	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/java/org/iri_research/renkan/models/User.java	Tue Oct 22 17:54:30 2013 +0200
@@ -30,8 +30,6 @@
 
     private String password;
 
-    private String salt;
-
     public User() {
     }
 
--- a/server/src/main/webapp/WEB-INF/i18n/messages.properties	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/i18n/messages.properties	Tue Oct 22 17:54:30 2013 +0200
@@ -75,6 +75,9 @@
 
 renkan.error.title.empty = Title must not be empty or null
 renkan.error.bin_config.json = bin config field must contain a valid json
+renkan.error.password.equals = Password and Password confimation do not match
+renkan.error.password.missing = Password missing
+
 
 renkanAuth.log_in = Log in
 renkanAuth.username_label = Username:
@@ -86,6 +89,6 @@
 
 renkanHeader.login = login
 renkanHeader.logout = logout
-renkanHeader.admin = admin
+renkanHeader.admin = administration
 renkanHeader.home = home
 
--- a/server/src/main/webapp/WEB-INF/i18n/messages_en.properties	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/i18n/messages_en.properties	Tue Oct 22 17:54:30 2013 +0200
@@ -86,6 +86,9 @@
 
 renkan.error.title.empty = Title must not be empty or null
 renkan.error.bin_config.json = bin config field must contain a valid json
+renkan.error.password.equals = Password and Password confimation do not match
+renkan.error.password.missing = Password missing
+
 
 renkanAuth.log_in = Log in
 renkanAuth.username_label = Username:
@@ -97,5 +100,5 @@
 
 renkanHeader.login = login
 renkanHeader.logout = logout
-renkanHeader.admin = admin
+renkanHeader.admin = administration
 renkanHeader.home = home
--- a/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties	Tue Oct 22 17:54:30 2013 +0200
@@ -84,6 +84,9 @@
 
 renkan.error.title.empty = Le champ titre ne doit pas ĂȘtre vide
 renkan.error.bin_config.json = le champ bin config doit contenir un json valide
+renkan.error.password.equals = Le mot de passe et sa confimation ne corresponde pas
+renkan.error.password.missing = Mot de passe manquant
+
 
 renkanAuth.log_in = Connection
 renkanAuth.username = Identifiant :
--- a/server/src/main/webapp/WEB-INF/spring-security.xml	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/spring-security.xml	Tue Oct 22 17:54:30 2013 +0200
@@ -31,12 +31,12 @@
  
     <security:http auto-config="true" use-expressions="true">
         <security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" />
-        <security:intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')" />
+        <security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
         <security:form-login login-page="/auth/login" authentication-failure-url="/auth/loginfailed" password-parameter="password" username-parameter="username" />
         <security:logout />
     </security:http>
  
-    <security:authentication-manager>        
+    <security:authentication-manager>
 	    <security:authentication-provider>
 	        <security:password-encoder hash="sha-256"/>
             <security:user-service>
--- a/server/src/main/webapp/WEB-INF/templates/fragment/spaceForm.html	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/templates/fragment/spaceForm.html	Tue Oct 22 17:54:30 2013 +0200
@@ -10,28 +10,28 @@
     //<![CDATA[
 
         function spaceFormSubmit() {
+            return true;
+            var errors = {};
+            var valid = true;
 
-        	var errors = {};
-        	var valid = true;
-        	
-        	if(!$('#title').val()) {
-        		errors['title'] = /*[[#{renkan.error.title.empty}]]*/"renkan.error.title.empty";
-        		valid = false;
-        	}
+            if(!$('#title').val()) {
+                errors['title'] = /*[[#{renkan.error.title.empty}]]*/"renkan.error.title.empty";
+                valid = false;
+            }
             
-        	if($('#binConfig').val()) {
-        		
-        		var editor = ace.edit("binConfigDiv");
-        		var annotations = editor.getSession().getAnnotations();
-        		if(annotations.length>0) {
+            if($('#binConfig').val()) {
+
+                var editor = ace.edit("binConfigDiv");
+                var annotations = editor.getSession().getAnnotations();
+                if(annotations.length>0) {
                     var error_message = /*[[#{renkan.error.bin_config.json}]]*/"renkan.error.bin_config.json";
                     errors['binConfigDiv'] = error_message + ". "+ annotations[0].type + ": (" +(annotations[0].row+1)+","+annotations[0].column+") " + annotations[0].text; 
-        			valid = false;        			
-        		}
-        	}
+                    valid = false;
+                }
+            }
             
             showformErrors(errors);
-                        
+
             return valid;
         }
 
@@ -43,11 +43,11 @@
             }
             else {
                 $('.binConfigButton').removeAttr("disabled");
-            }        	
+            }
         }
-        
+
         $(function(){
-        	var tabSize = 2;
+            var tabSize = 2;
             var editor = ace.edit("binConfigDiv");
             editor.setTheme("ace/theme/xcode");
             editor.getSession().setMode("ace/mode/json");
@@ -55,23 +55,23 @@
             editor.getSession().setUseSoftTabs(true);
             _setBinConfigStatus(editor);
             editor.getSession().on('change', function(e) {
-            	$('#binConfig').val(editor.getValue());
+                $('#binConfig').val(editor.getValue());
             });
             editor.getSession().on('changeAnnotation', function() {
                 _setBinConfigStatus(editor);
             });
-            
+
             $('#binConfigFormatButton').click(function(){
-            	var jsonText = editor.getValue();
-            	try {
-					editor.setValue(formatJson(jsonText, tabSize),0);
-					editor.moveCursorTo(0,0);
-					editor.clearSelection();
-				} catch (e) {
-					//do nothing
-				}
+                var jsonText = editor.getValue();
+                try {
+                    editor.setValue(formatJson(jsonText, tabSize),0);
+                    editor.moveCursorTo(0,0);
+                    editor.clearSelection();
+                } catch (e) {
+                    //do nothing
+                }
             });
-            
+
             $('#binConfigCompactButton').click(function(){
                 var jsonText = editor.getValue();
                 try {
@@ -82,17 +82,22 @@
                     //do nothing
                 }
             });
-            
+
             $('#color').spectrum({
-            	showInput: true,
-            	showAlpha: true,
-            	showPalette: true,
-            	showInitial: true
+                showInput: true,
+                showAlpha: true,
+                showPalette: true,
+                showInitial: true
             });
+
+            $("#model-form").submit(function(e) {
+                return spaceFormSubmit();
+            });
+
         });
     //]]>
   </script>
-  <form action="#" th:object="${space}" th:action="@{/admin/spaces/save}" method="post" onsubmit="return spaceFormSubmit()">
+  <form action="#" th:object="${space}" th:action="@{/admin/spaces/save}" method="post" id="model-form">
      <fieldset class="form-fields">
        <input type="hidden" th:field="*{id}" th:if="*{id}" />
        <input type="hidden" th:field="*{binConfig}"/>
--- a/server/src/main/webapp/WEB-INF/templates/fragment/userForm.html	Mon Oct 21 17:55:12 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/templates/fragment/userForm.html	Tue Oct 22 17:54:30 2013 +0200
@@ -11,17 +11,26 @@
 
         function userFormSubmit() {
 
-        	var errors = {};
-        	var valid = true;
-        	
-        	if(!$('#title').val()) {
-        		errors['title'] = /*[[#{renkan.error.title.empty}]]*/"renkan.error.title.empty";
-        		valid = false;
-        	}
+            var errors = {};
+            var valid = true;
+
+            if(!$('#title').val()) {
+                errors['title'] = /*[[#{renkan.error.title.empty}]]*/"renkan.error.title.empty";
+                valid = false;
+            }
+
+            var password = $('#password').val();
+            var passwordConf = $('#passwordConf').val();
+            var objId = $('#id').val();
             
-        	if($('#binConfig').val()) {
-        		
-        	}
+            if(objId && !password) {
+                errors['password'] = /*[[#{renkan.error.password.missing}]]*/"renkan.error.passwsord.missing";
+                valid = false;
+            }
+            if(password !== passwordConf) {
+                errors['password'] = /*[[#{renkan.error.password.equals}]]*/"renkan.error.passwsord.equals";
+                valid = false;
+            }
 
             showformErrors(errors);
 
@@ -34,15 +43,19 @@
             $.datepicker.setDefaults($.datepicker.regional[ "" ]);
             $('.datepicker').datepicker($.datepicker.regional[regionalValue]);
             $('#color').spectrum({
-            	showInput: true,
-            	showAlpha: true,
-            	showPalette: true,
-            	showInitial: true
+                showInput: true,
+                showAlpha: true,
+                showPalette: true,
+                showInitial: true
             });
+            $("#model-form").submit(function(e) {
+                return userFormSubmit();
+            });
+            
         });
     //]]>
   </script>
-  <form action="#" th:object="${user}" th:action="@{/admin/users/save}" method="post" onsubmit="return userFormSubmit()">
+  <form action="#" th:object="${user}" th:action="@{/admin/users/save}" method="post" id="model-form">
      <fieldset class="form-fields">
        <input type="hidden" th:field="*{id}" th:if="*{id}" />
        <div>
@@ -67,6 +80,14 @@
          <input type="text" th:field="*{avatar}" /> 
        </div>
        <div>
+         <label for="password" th:text="#{renkanAdmin.form.password}">Password: </label> 
+         <input type="password" th:field="*{password}" /> 
+       </div>
+       <div>
+         <label for="passwordConfirm" th:text="#{renkanAdmin.form.passwordConfirm}">Confirm password: </label> 
+         <input type="password" th:field="*{passwordConfirm}" /> 
+       </div>       
+       <div>
          <label for="credentialExpirationDate" th:text="#{renkanAdmin.form.credentialExpirationDate}">Credential expiration date: </label> 
          <input type="text" th:field="*{credentialExpirationDate}" class="datepicker"/> 
        </div>