/** * Creates a new user. * * @param firstName The first name of the user * @param lastName The last name of the user * @param email The email address of the user * @param username The username of the user * @param password The password of the user * @param role The role of the user * @return The created user */ public User createUser( String firstName, String lastName, String email, String username, String password, Role role) { UserDAO userDAO = DAOFactory.getInstance().getUserDAO(); InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO(); EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO(); try { String passwordEncoded = EncodingUtils.md5EncodeString(password); InternalAuth internalAuth = internalAuthDAO.create(username, passwordEncoded); User user = userDAO.create( firstName, lastName, String.valueOf(internalAuth.getId()), getName(), role); // TODO Default contact type? emailDAO.create(user.getContactInfo(), null, Boolean.TRUE, email); return user; } catch (UnsupportedEncodingException e) { throw new SmvcRuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new SmvcRuntimeException(e); } }
@Override public String createCredentials(String username, String password) { InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO(); try { String newPasswordEncoded = EncodingUtils.md5EncodeString(password); InternalAuth internalAuth = internalAuthDAO.create(username, newPasswordEncoded); String externalId = internalAuth.getId().toString(); return externalId; } catch (UnsupportedEncodingException e) { throw new SmvcRuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new SmvcRuntimeException(e); } }
@Override public void updatePassword(String externalId, String password) { InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO(); try { InternalAuth internalAuth = internalAuthDAO.findById(NumberUtils.createLong(externalId)); String newPasswordEncoded = EncodingUtils.md5EncodeString(password); internalAuthDAO.updatePassword(internalAuth, newPasswordEncoded); } catch (UnsupportedEncodingException e) { throw new SmvcRuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new SmvcRuntimeException(e); } }
/** * Returns the user corresponding to the given credentials. If no user cannot be found, returns * <code>null</code>. * * @param username The username * @param password The password * @return The user corresponding to the given credentials, or <code>null</code> if not found */ public User getUser(String username, String password) { UserDAO userDAO = DAOFactory.getInstance().getUserDAO(); InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO(); String passwordEncoded; try { passwordEncoded = EncodingUtils.md5EncodeString(password); } catch (UnsupportedEncodingException e) { throw new SmvcRuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new SmvcRuntimeException(e); } InternalAuth internalAuth = internalAuthDAO.findByUsernameAndPassword(username, passwordEncoded); if (internalAuth != null) { User user = userDAO.findByExternalIdAndAuthProvider(String.valueOf(internalAuth.getId()), getName()); return user; } else { return null; } }