/** * 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 void updateUsername(String externalId, String username) { InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO(); InternalAuth internalAuth = internalAuthDAO.findById(NumberUtils.createLong(externalId)); internalAuthDAO.updateUsername(internalAuth, username); }
/** * Returns the username of a user corresponding to the given identifier, or <code>null</code> if * not found. * * @param externalId The user identifier * @return The username of the user corresponding to the given identifier, or <code>null</code> if * not found */ public String getUsername(String externalId) { InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO(); Long internalAuthId = NumberUtils.createLong(externalId); if (internalAuthId != null && internalAuthId > 0) { InternalAuth internalAuth = internalAuthDAO.findById(internalAuthId); return internalAuth == null ? null : internalAuth.getUsername(); } return null; }
@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; } }