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"; }
@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; } }