Corrected date management in users. user_management
authorymh <ymh.work@gmail.com>
Mon, 04 Nov 2013 15:56:44 +0100
branchuser_management
changeset 230 793eece3691e
parent 229 5064a6ddcb08
child 231 e65766f81b15
Corrected date management in users.
server/src/main/java/org/iri_research/renkan/controller/admin/UsersAdminController.java
server/src/main/java/org/iri_research/renkan/forms/UserForm.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/templates/fragment/userForm.html
--- a/server/src/main/java/org/iri_research/renkan/controller/admin/UsersAdminController.java	Mon Oct 28 18:20:56 2013 +0100
+++ b/server/src/main/java/org/iri_research/renkan/controller/admin/UsersAdminController.java	Mon Nov 04 15:56:44 2013 +0100
@@ -2,8 +2,12 @@
 
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
+import java.util.Date;
+import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
 
 import javax.annotation.Resource;
 import javax.inject.Inject;
@@ -15,11 +19,14 @@
 import org.iri_research.renkan.RenkanException;
 import org.iri_research.renkan.controller.Utils;
 import org.iri_research.renkan.forms.UserForm;
+import org.iri_research.renkan.forms.UserFormValidator;
 import org.iri_research.renkan.models.User;
 import org.iri_research.renkan.repositories.ProjectsRepository;
 import org.iri_research.renkan.repositories.UsersRepository;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.propertyeditors.CustomDateEditor;
+import org.springframework.beans.propertyeditors.StringTrimmerEditor;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Sort.Direction;
@@ -29,6 +36,8 @@
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -51,6 +60,22 @@
     
     @Resource(name="renkanPasswordEncoder")
     private PasswordEncoder passwordEncoder;
+    
+    @InitBinder(value = { "user" })
+    protected void initBinder(WebDataBinder binder) {
+        binder.setValidator(new UserFormValidator());
+    }
+
+    @InitBinder
+    public void initDateBinder(final WebDataBinder dataBinder, final Locale locale) {
+        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        sdf.setLenient(false);
+        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
+        CustomDateEditor cde = new CustomDateEditor(sdf, true);
+        dataBinder.registerCustomEditor(Date.class, cde);
+        
+        dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
+    }
 
     @RequestMapping(value = "", method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" })
     public String usersList(
--- a/server/src/main/java/org/iri_research/renkan/forms/UserForm.java	Mon Oct 28 18:20:56 2013 +0100
+++ b/server/src/main/java/org/iri_research/renkan/forms/UserForm.java	Mon Nov 04 15:56:44 2013 +0100
@@ -14,11 +14,11 @@
     private String avatar;
 
     private Date credentialExpirationDate;
+    private Date expirationDate;
 
     private String email;
 
     private boolean enabled;
-    private Date expirationDate;
     private boolean locked;
     private String password;
     private String passwordConfirm;
@@ -105,7 +105,9 @@
         this.model.setExpirationDate(this.expirationDate);
         this.model.setEnabled(this.enabled);
         this.model.setLocked(this.locked);
-        this.model.setPassword(this.passwordEncoder.encode(this.password));
+        if(this.password != null && this.password.length() > 0) {
+            this.model.setPassword(this.passwordEncoder.encode(this.password));
+        }
 
     }
 
--- a/server/src/main/java/org/iri_research/renkan/models/User.java	Mon Oct 28 18:20:56 2013 +0100
+++ b/server/src/main/java/org/iri_research/renkan/models/User.java	Mon Nov 04 15:56:44 2013 +0100
@@ -3,13 +3,10 @@
 import java.util.Collection;
 import java.util.Date;
 
-import javax.annotation.Resource;
-
 import org.springframework.data.mongodb.core.mapping.Document;
 import org.springframework.data.mongodb.core.mapping.Field;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.userdetails.UserDetails;
-import org.springframework.security.crypto.password.PasswordEncoder;
 
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonIgnore;
--- a/server/src/main/webapp/WEB-INF/i18n/messages.properties	Mon Oct 28 18:20:56 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/i18n/messages.properties	Mon Nov 04 15:56:44 2013 +0100
@@ -1,8 +1,11 @@
 
 date.format = yyyy/MM/dd HH:mm
+date.date.format = yyyy/MM/dd
+date.date.datePicker.format = yy/mm/dd
 question.yes = yes
 question.no = no
 
+
 renkanIndex.renkan_exp = Create a Renkan
 renkanIndex.project_list = Renkan list
 renkanIndex.project_name = Name
--- a/server/src/main/webapp/WEB-INF/i18n/messages_en.properties	Mon Oct 28 18:20:56 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/i18n/messages_en.properties	Mon Nov 04 15:56:44 2013 +0100
@@ -1,5 +1,7 @@
 
 date.format = yyyy/MM/dd HH:mm
+date.date.format = yyyy/MM/dd
+date.date.datePicker.format = yy/dd/mm
 question.yes = yes
 question.no = no
 
--- a/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties	Mon Oct 28 18:20:56 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties	Mon Nov 04 15:56:44 2013 +0100
@@ -1,5 +1,7 @@
 
 date.format = dd/MM/yyyy HH:mm
+date.date.format = dd/MM/yyyy
+date.date.datePicker.format = dd/mm/yy
 question.yes = oui
 question.no = non
 
--- a/server/src/main/webapp/WEB-INF/templates/fragment/userForm.html	Mon Oct 28 18:20:56 2013 +0100
+++ b/server/src/main/webapp/WEB-INF/templates/fragment/userForm.html	Mon Nov 04 15:56:44 2013 +0100
@@ -23,11 +23,11 @@
             var passwordConf = $('#passwordConfirm').val();
             var objId = $('#id').val();
             
-            if(objId && !password) {
+            if(!objId && !password) {
                 errors['password'] = /*[[#{renkan.error.password.missing}]]*/"renkan.error.passwsord.missing";
                 valid = false;
             }
-            if(password !== passwordConf) {
+            if(valid && password !== passwordConf) {
                 errors['password'] = /*[[#{renkan.error.password.equals}]]*/"renkan.error.passwsord.equals";
                 valid = false;
             }
@@ -40,15 +40,33 @@
         
         $(function(){
             var regionalValue = /*[[${#ctx.getLocale().getLanguage()}]]*/"";
+            var dateFormat = /*[[#{date.date.datePicker.format}]]*/"yy/mm/dd";
             $.datepicker.setDefaults($.datepicker.regional[ "" ]);
-            $('.datepicker').datepicker($.datepicker.regional[regionalValue]);
+            $('.datepicker').each(function (i,elt) {
+                var picker_options = $.extend({},$.datepicker.regional[regionalValue=="en"?"":regionalValue]);
+                picker_options.dateFormat = dateFormat;
+                picker_options.altFormat = $.datepicker.ISO_8601;
+                var elt = $(elt);
+                picker_options.altField = '#'+elt.attr('id').replace('-disp', '');
+                elt.datepicker(picker_options);
+            });
+            
             $('#color').spectrum({
                 showInput: true,
                 showAlpha: true,
                 showPalette: true,
-                showInitial: true
+                showInitial: true,
+                preferredFormat: 'hex'
             });
             $("#model-form").submit(function(e) {
+                $('.datepicker').each(function (i,elt) {
+                    var elt = $(elt);
+                    elt_val = elt.val();
+                    console.log(elt.attr('id'),elt_val);
+                    if(!elt_val) {
+                        $('#'+elt.attr('id').replace('-disp', '')).val("");
+                    }
+                });
                 return userFormSubmit();
             });
             
@@ -90,12 +108,15 @@
          <div th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}" class="form-error"></div>
        </div>       
        <div>
-         <label for="credentialExpirationDate" th:text="#{renkanAdmin.form.credentialExpirationDate}">Credential expiration date: </label> 
-         <input type="text" th:field="*{credentialExpirationDate}" class="datepicker"/> 
+         <label for="credentialExpirationDate" th:text="#{renkanAdmin.form.credentialExpirationDate}">Credential expiration date: </label>
+         <input type="hidden" name="credentialExpirationDate" th:field="*{credentialExpirationDate}"/>
+         <input type="date" id="credentialExpirationDate-disp" class="datepicker" value="01/01/1970" th:value="${user.credentialExpirationDate != null}?${#dates.format(user.credentialExpirationDate, #messages.msg('date.date.format'))}:''"/>
+          
        </div>
        <div>
-         <label for="expirationDate" th:text="#{renkanAdmin.form.expirationDate}">Expiration date: </label> 
-         <input type="text" th:field="*{expirationDate}" class="datepicker"/> 
+         <label for="expirationDate" th:text="#{renkanAdmin.form.expirationDate}">Expiration date: </label>
+         <input type="hidden" name="expirationDate" th:field="*{expirationDate}" />
+         <input type="date" id="expirationDate-disp" class="datepicker" value="01/01/1970" th:value="${user.expirationDate != null}?${#dates.format(user.expirationDate, #messages.msg('date.date.format'))}:''"/> 
        </div>
        <div>
          <label for="email" th:text="#{renkanAdmin.form.email}">Email: </label>