public String doSignup() { // Does the login already exists ? if (em.createNamedQuery(User.FIND_BY_LOGIN, User.class) .setParameter("login", user.getLogin()) .getResultList() .size() > 0) { facesContext.addMessage( null, new FacesMessage( FacesMessage.SEVERITY_WARN, "Login already exists " + user.getLogin(), "You must choose a different login")); return null; } // Everything is ok, we can create the user user.setPassword(password1); em.persist(user); resetPasswords(); facesContext.addMessage( null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Hi " + user.getFirstName(), "Welcome to this website")); loggedIn = true; if (user.getRole().equals(UserRole.ADMIN)) admin = true; return "/main"; }
public String doForgotPassword() { TypedQuery<User> query = em.createNamedQuery(User.FIND_BY_EMAIL, User.class); query.setParameter("email", user.getEmail()); try { user = query.getSingleResult(); String temporaryPassword = Lorem.getWords(1); user.setPassword(PasswordUtils.digestPassword(temporaryPassword)); em.merge(user); facesContext.addMessage( null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Email sent", "An email has been sent to " + user.getEmail() + " with temporary password :"******"dummyPassword" return doLogout(); } catch (NoResultException e) { facesContext.addMessage( null, new FacesMessage( FacesMessage.SEVERITY_WARN, "Unknown email", "This email address is unknonw in our system")); return null; } }
public String doUpdateProfile() { if (password1 != null && !password1.isEmpty()) user.setPassword(PasswordUtils.digestPassword(password1)); em.merge(user); resetPasswords(); facesContext.addMessage( null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Profile has been updated for " + user.getFirstName(), null)); return null; }
public String doLogoutAndRemoveCookie() { removeCookie(); user.setUuid(null); em.merge(user); AlterableContext ctx = (AlterableContext) beanManager.getContext(SessionScoped.class); Bean<?> myBean = beanManager.getBeans(AccountBean.class).iterator().next(); ctx.destroy(myBean); myBean = beanManager.getBeans(ShoppingCartBean.class).iterator().next(); ctx.destroy(myBean); return "/main"; }
@PostConstruct private void checkIfUserHasRememberMeCookie() { String coockieValue = getCookieValue(); if (coockieValue == null) return; TypedQuery<User> query = em.createNamedQuery(User.FIND_BY_UUID, User.class); query.setParameter("uuid", coockieValue); try { user = query.getSingleResult(); // If the user is an administrator if (user.getRole().equals(UserRole.ADMIN)) admin = true; // The user is now logged in loggedIn = true; } catch (NoResultException e) { // The user maybe has an old coockie, let's get rid of it removeCookie(); } }
public String doSignin() { TypedQuery<User> query = em.createNamedQuery(User.FIND_BY_LOGIN_PASSWORD, User.class); query.setParameter("login", user.getLogin()); query.setParameter("password", PasswordUtils.digestPassword(user.getPassword())); try { user = query.getSingleResult(); // If the user is an administrator if (user.getRole().equals(UserRole.ADMIN)) admin = true; // If the user has clicked on remember me if (rememberMe) { String uuid = UUID.randomUUID().toString(); user.setUuid(uuid); addCookie(uuid); } else { user.setUuid(null); removeCookie(); } // The user is now logged in loggedIn = true; facesContext.addMessage( null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Welcome back " + user.getFirstName(), "You can now browse the catalog")); return "/main"; } catch (NoResultException e) { facesContext.addMessage( null, new FacesMessage( FacesMessage.SEVERITY_WARN, "Wrong user/password", "Check your inputs or ask for a new password")); return null; } }