Author: fdesbois Date: 2010-06-09 14:42:49 +0000 (Wed, 09 Jun 2010) New Revision: 503 Log: Evo #2325 : Implement connexion page with role using javascript Added: trunk/wao-ui/src/main/resources/fr/ifremer/wao/ui/pages/Connexion.properties trunk/wao-ui/src/main/webapp/img/invalid-16px.png trunk/wao-ui/src/main/webapp/img/valid-16px.png Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceUserImpl.java trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Connexion.java trunk/wao-ui/src/main/webapp/Connexion.tml trunk/wao-ui/src/main/webapp/css/main.css trunk/wao-ui/src/main/webapp/js/wao.js trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ConnectionTest.java Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceUserImpl.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceUserImpl.java 2010-06-09 09:38:26 UTC (rev 502) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceUserImpl.java 2010-06-09 14:42:49 UTC (rev 503) @@ -428,7 +428,7 @@ String password = context.encodeString("password"); WaoUser user = dao.create( - WaoUser.ROLE, UserRole.ADMIN.ordinal(), + WaoUser.ROLE, UserRole.ADMIN.toInt(), WaoUser.ACTIVE, true, WaoUser.LOGIN, "admin", WaoUser.PASSWORD, password, Modified: trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Connexion.java =================================================================== --- trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Connexion.java 2010-06-09 09:38:26 UTC (rev 502) +++ trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Connexion.java 2010-06-09 14:42:49 UTC (rev 503) @@ -4,22 +4,33 @@ import fr.ifremer.wao.WaoBusinessException; import fr.ifremer.wao.WaoBusinessException.Type; import fr.ifremer.wao.WaoException; +import fr.ifremer.wao.bean.UserRole; import fr.ifremer.wao.entity.WaoUser; import fr.ifremer.wao.service.ServiceUser; import fr.ifremer.wao.ui.components.FeedBack; import org.apache.tapestry5.EventContext; import org.apache.tapestry5.Link; +import org.apache.tapestry5.OptionModel; import org.apache.tapestry5.PersistenceConstants; +import org.apache.tapestry5.SelectModel; +import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary; import org.apache.tapestry5.annotations.InjectComponent; import org.apache.tapestry5.annotations.Log; import org.apache.tapestry5.annotations.Persist; import org.apache.tapestry5.annotations.Property; import org.apache.tapestry5.annotations.SessionState; +import org.apache.tapestry5.internal.OptionModelImpl; +import org.apache.tapestry5.internal.SelectModelImpl; import org.apache.tapestry5.ioc.annotations.Inject; +import org.apache.tapestry5.json.JSONArray; +import org.apache.tapestry5.json.JSONObject; import org.apache.tapestry5.services.PageRenderLinkSource; +import org.apache.tapestry5.services.Request; import org.slf4j.Logger; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; /** * Login @@ -29,6 +40,7 @@ * @author fdesbois * $Id$ */ + at IncludeJavaScriptLibrary("context:js/wao.js") public class Connexion { @Inject @@ -62,10 +74,16 @@ @Property private String password; + @Property + private UserRole userRole; + @Persist(PersistenceConstants.FLASH) @Property private String email; + @Property + private SelectModel emptySelectModel = new SelectModelImpl(new OptionModelImpl(null)); + void onActivate(Object... activationContext) { if (logger.isDebugEnabled()) { logger.debug("Activiation context : " + @@ -95,28 +113,74 @@ return null; } +// /** +// * Empty selectModel to initialize userRole select. By default, Tapestry +// * generate the selectModel with the enum values. +// * +// * @return an empty SelectModel +// */ +// SelectModel getEmptySelectModel() { +// if (emptySelectModel == null) { +// emptySelectModel = new SelectModelImpl(new OptionModelImpl(null)); +// } +// return emptySelectModel; +// } + + /** + * ON_BLUR :: Callback method for blur event on Login textfield component. + * Login will be checked to refresh the userRole select using {@link + * JSONObject} that contains data for select options to display in javascript. + * + * @param login User login + * @return a JSONObject with userRole select data to refresh in Ajax + */ + JSONObject onBlurFromLogin(String login) { + JSONObject json = new JSONObject(); + if (serviceUser.existLogin(login)) { + + List<UserRole> roles = serviceUser.getUserRolesByLogin(login); + + JSONArray array = new JSONArray(); + for (UserRole role : roles) { + JSONObject option = new JSONObject(); + option.put("name", role.name()); + option.put("label", role.getLabel()); + array.put(option); + } + json.put("select", array); + } + return json; + } + + @Log void onValidateFormFromConnexionForm() { - try { - if (logger.isDebugEnabled()) { - logger.debug("Login : " + login); - } - if (login != null && password != null) { - currentUser = serviceUser.connect(login, password, null); + if (login != null && !serviceUser.existLogin(login)) { + connexionFeedback.addError("Identifiant de connexion inconnu, " + + "veuillez réessayer."); + } else { + try { if (logger.isDebugEnabled()) { - logger.debug("User connected : " + - currentUser.getFullName()); + logger.debug("Login : " + login); + logger.debug("Role : " + userRole); } + if (login != null && password != null && userRole != null) { + currentUser = serviceUser.connect(login, password, userRole); + if (logger.isDebugEnabled()) { + logger.debug("User connected : " + + currentUser.getFullName()); + } + } + } catch (WaoBusinessException eee) { + if (eee.getType().equals(Type.BAD_CONNECTION)) { + connexionFeedback.addError(eee.getMessage()); + email = login; + } else if (eee.getType().equals(Type.ILLEGAL_CONNECTION)) { + connexionFeedback.addInfo(eee.getMessage()); + } + if (logger.isDebugEnabled()) { + logger.error("WaoBusinessException : " + eee.getMessage(), eee); + } } - } catch (WaoBusinessException eee) { - if (eee.getType().equals(Type.BAD_CONNECTION)) { - connexionFeedback.addError(eee.getMessage()); - email = login; - } else if (eee.getType().equals(Type.ILLEGAL_CONNECTION)) { - connexionFeedback.addInfo(eee.getMessage()); - } - if (logger.isDebugEnabled()) { - logger.error("WaoBusinessException : " + eee.getMessage(), eee); - } } } Added: trunk/wao-ui/src/main/resources/fr/ifremer/wao/ui/pages/Connexion.properties =================================================================== --- trunk/wao-ui/src/main/resources/fr/ifremer/wao/ui/pages/Connexion.properties (rev 0) +++ trunk/wao-ui/src/main/resources/fr/ifremer/wao/ui/pages/Connexion.properties 2010-06-09 14:42:49 UTC (rev 503) @@ -0,0 +1,3 @@ +login-label: Identifiant (email) +password-label: Mot de passe +userRole-label: R\u00f4le \ No newline at end of file Modified: trunk/wao-ui/src/main/webapp/Connexion.tml =================================================================== --- trunk/wao-ui/src/main/webapp/Connexion.tml 2010-06-09 09:38:26 UTC (rev 502) +++ trunk/wao-ui/src/main/webapp/Connexion.tml 2010-06-09 14:42:49 UTC (rev 503) @@ -55,15 +55,23 @@ <form t:id="connexionForm" t:type="form" action="tapestry"> <div class="fleft clearfix" id="so-connexion-form"> <p><t:label for="login" /> : </p> - <p><input t:type="textfield" t:id="login" t:value="login" /></p> + <p> + <input t:type="textfield" t:id="login" t:value="login" + t:mixins="ck/onEvent" t:event="blur" t:onCompleteCallback="refreshUserRoles"/> + <img class="hidden" id="loginValid" src="${asset:context:img/valid-16px.png}" alt="Login valide" title="Login valide" /> + <img class="hidden" id="loginInvalid" src="${asset:context:img/invalid-16px.png}" alt="Login invalide" title="Login invalide" /> + </p> <p><t:label for="password" /> : </p> <p><input t:type="passwordfield" t:id="password" t:value="password" /></p> - <div class="fright" id="so-connexion-form-buttons"> - <input class="ico accept" t:type="submit" value="Connexion" title="Connexion à l'application Wao" /> - </div> + <p><t:label for="userRole" /> : </p> + <p><select t:type="select" t:id="userRole" t:value="userRole" t:model="emptySelectModel"/></p> + <!--<div class="fright" id="so-connexion-form-buttons">--> + <!--<input class="ico accept" t:type="submit" value="Connexion" title="Connexion à l'application Wao" />--> + <!--</div>--> </div> <div class="fright" id="so-connexion-logo"> - <img src="${asset:context:img/logo-wao-48px.png}" alt="Logo Obsmer"/> + <input class="ico" t:type="submit" value="Connexion" title="Connexion à l'application Wao" /> + <!--<img src="${asset:context:img/logo-wao-48px.png}" alt="Logo Obsmer"/>--> </div> </form> </div> Modified: trunk/wao-ui/src/main/webapp/css/main.css =================================================================== --- trunk/wao-ui/src/main/webapp/css/main.css 2010-06-09 09:38:26 UTC (rev 502) +++ trunk/wao-ui/src/main/webapp/css/main.css 2010-06-09 14:42:49 UTC (rev 503) @@ -89,7 +89,7 @@ } div#so-connexion-form { - width: 160px; + width: 180px; } div#so-connexion-form-buttons { @@ -104,6 +104,13 @@ div#so-connexion-logo { margin-top: 80px; } + +div#so-connexion-logo input.ico { + width: 48px; + height: 48px; + background: url(../img/logo-wao-48px.png) no-repeat center center; +} + /* Module SESSION */ div#so-session { width: 80%; Added: trunk/wao-ui/src/main/webapp/img/invalid-16px.png =================================================================== (Binary files differ) Property changes on: trunk/wao-ui/src/main/webapp/img/invalid-16px.png ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: trunk/wao-ui/src/main/webapp/img/valid-16px.png =================================================================== (Binary files differ) Property changes on: trunk/wao-ui/src/main/webapp/img/valid-16px.png ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: trunk/wao-ui/src/main/webapp/js/wao.js =================================================================== --- trunk/wao-ui/src/main/webapp/js/wao.js 2010-06-09 09:38:26 UTC (rev 502) +++ trunk/wao-ui/src/main/webapp/js/wao.js 2010-06-09 14:42:49 UTC (rev 503) @@ -48,3 +48,56 @@ hidden.setValue(newValue); window.close(); } + +/** + * Method used to refresh userRole select from a response. The response contains + * data to create the select options : + * - Array(option) response.select : list of options to create + * - String option.name : name of the option (used as value) + * - String option.label : label to display for the option + * This method uses elements : + * - userRole : select input to use + * - loginValid : img to display in case of login Ok (select is defined) + * - loginInvalid : img to display in case of login Ko (select is undefined) + */ +function refreshUserRoles(response) { + + //Tapestry.debug('reponse.select = ' + response.select); + + if (response.select) { + var content = ''; + + for (i = 0; i < response.select.length; i++) { + var option = response.select[i]; + + //Tapestry.debug('option.name = ' + option.name); + //Tapestry.debug('option.label = ' + option.label); + + content += '<option value="' + option.name + '"'; + // First option is selected + if (i == 0) { + content += ' selected="selected"'; + } + content += '>' + option.label + '</option>\n\t'; + } + // Update select userRole + $('userRole').update(content); + $('userRole').enable(); + // Display valid picture + $('loginValid').toggleClassName('hidden'); + // Hide invalid picture if needed + if (!$('loginInvalid').hasClassName('hidden')) { + $('loginInvalid').toggleClassName('hidden'); + } + } else { + // Update select userRole (empty options) + $('userRole').update('<option value=""></option>'); + $('userRole').disable(); + // Display invalid picture + $('loginInvalid').toggleClassName('hidden'); + // Hide valid picture if needed + if (!$('loginValid').hasClassName('hidden')) { + $('loginValid').toggleClassName('hidden'); + } + } +} Modified: trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ConnectionTest.java =================================================================== --- trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ConnectionTest.java 2010-06-09 09:38:26 UTC (rev 502) +++ trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ConnectionTest.java 2010-06-09 14:42:49 UTC (rev 503) @@ -120,11 +120,13 @@ private Element connectUser(Document page) throws WaoException, WaoBusinessException { // Need to use correct role - when(serviceUser.connect("jmichmuche", "password", null)).thenReturn(user); + when(serviceUser.connect("jmichmuche", "password", UserRole.OBSERVER)).thenReturn(user); + when(serviceUser.existLogin("jmichmuche")).thenReturn(true); Map<String, String> fieldValues = new HashMap<String, String>(); fieldValues.put("login", "jmichmuche"); fieldValues.put("password", "password"); + fieldValues.put("userRole", UserRole.OBSERVER.name()); Document result = tester.submitForm( page.getElementById("connexionForm"), fieldValues);