public void setEmail(String email) { if (UserManager.getUserProvider().isReadOnly()) { throw new UnsupportedOperationException("User provider is read-only."); } if (email != null && email.matches("\\s*")) { email = null; } if (UserManager.getUserProvider().isEmailRequired() && !StringUtils.isValidEmailAddress(email)) { throw new IllegalArgumentException("User provider requires email address."); } try { String originalEmail = this.email; UserManager.getUserProvider().setEmail(username, email); this.email = email; // Fire event. Map<String, Object> params = new HashMap<String, Object>(); params.put("type", "emailModified"); params.put("originalValue", originalEmail); UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified, params); } catch (UserNotFoundException unfe) { Log.error(unfe.getMessage(), unfe); } }
public void setName(String name) { if (UserManager.getUserProvider().isReadOnly()) { throw new UnsupportedOperationException("User provider is read-only."); } if (name != null && name.matches("\\s*")) { name = null; } if (name == null && UserManager.getUserProvider().isNameRequired()) { throw new IllegalArgumentException("User provider requires name."); } try { String originalName = this.name; UserManager.getUserProvider().setName(username, name); this.name = name; // Fire event. Map<String, Object> params = new HashMap<String, Object>(); params.put("type", "nameModified"); params.put("originalValue", originalName); UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified, params); } catch (UserNotFoundException unfe) { Log.error(unfe.getMessage(), unfe); } }
public void authenticate(String username, String password) throws UnauthorizedException { if (username.contains("@")) { // Check that the specified domain matches the server's domain int index = username.indexOf("@"); String domain = username.substring(index + 1); if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) { username = username.substring(0, index); } else { // Unknown domain. Return authentication failed. throw new UnauthorizedException(); } } try { // Some native authentication mechanisms appear to not handle high load // very well. Therefore, synchronize access to Shaj to throttle auth checks. synchronized (this) { if (!Shaj.checkPassword(domain, username, password)) { throw new UnauthorizedException(); } } } catch (UnauthorizedException ue) { throw ue; } catch (Exception e) { throw new UnauthorizedException(e); } // See if the user exists in the database. If not, automatically create them. UserManager userManager = UserManager.getInstance(); try { userManager.getUser(username); } catch (UserNotFoundException unfe) { try { Log.debug("Automatically creating new user account for " + username); // Create user; use a random password for better safety in the future. // Note that we have to go to the user provider directly -- because the // provider is read-only, UserManager will usually deny access to createUser. UserProvider provider = UserManager.getUserProvider(); if (!(provider instanceof NativeUserProvider)) { Log.error( "Error: not using NativeUserProvider so authentication with " + "NativeAuthProvider will likely fail. Using: " + provider.getClass().getName()); } UserManager.getUserProvider().createUser(username, StringUtils.randomString(8), null, null); } catch (UserAlreadyExistsException uaee) { // Ignore. } } }
public void setModificationDate(Date modificationDate) { if (UserManager.getUserProvider().isReadOnly()) { throw new UnsupportedOperationException("User provider is read-only."); } try { Date originalModificationDate = this.modificationDate; UserManager.getUserProvider().setCreationDate(username, modificationDate); this.modificationDate = modificationDate; // Fire event. Map<String, Object> params = new HashMap<String, Object>(); params.put("type", "nameModified"); params.put("originalValue", originalModificationDate); UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified, params); } catch (UserNotFoundException unfe) { Log.error(unfe.getMessage(), unfe); } }
/** * Constructs a new user. Normally, all arguments can be <tt>null</tt> except the username. * However, a UserProvider -may- require a name or email address. In those cases, the * isNameRequired or isEmailRequired UserProvider tests indicate whether <tt>null</tt> is allowed. * Typically, User objects should not be constructed by end-users of the API. Instead, user * objects should be retrieved using {@link UserManager#getUser(String)}. * * @param username the username. * @param name the name. * @param email the email address. * @param creationDate the date the user was created. * @param modificationDate the date the user was last modified. */ public User( String username, String name, String email, Date creationDate, Date modificationDate) { if (username == null) { throw new NullPointerException("Username cannot be null"); } this.username = username; if (UserManager.getUserProvider().isNameRequired() && (name == null || "".equals(name.trim()))) { throw new IllegalArgumentException( "Invalid or empty name specified with provider that requires name"); } this.name = name; if (UserManager.getUserProvider().isEmailRequired() && (email == null || "".equals(email.trim()))) { throw new IllegalArgumentException( "Empty email address specified with provider that requires email address. User: "******" Email: " + email); } this.email = email; this.creationDate = creationDate; this.modificationDate = modificationDate; }
/** * Sets a new password for this user. * * @param password the new password for the user. * @throws UnsupportedOperationException exception */ public void setPassword(String password) throws UnsupportedOperationException { if (UserManager.getUserProvider().isReadOnly()) { throw new UnsupportedOperationException("User provider is read-only."); } try { AuthFactory.getAuthProvider().setPassword(username, password); // Fire event. Map<String, Object> params = new HashMap<String, Object>(); params.put("type", "passwordModified"); UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified, params); } catch (UserNotFoundException unfe) { Log.error(unfe.getMessage(), unfe); } }