UserManager.java 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * $RCSfile$
 * $Revision: 1217 $
 * $Date: 2005-04-11 18:11:06 -0300 (Mon, 11 Apr 2005) $
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.user;

14
import org.dom4j.Element;
15 16 17
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;
import org.jivesoftware.util.*;
18 19 20 21 22
import org.jivesoftware.wildfire.IQResultListener;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.event.UserEventDispatcher;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
23 24 25

import java.util.Collection;
import java.util.Collections;
26
import java.util.Iterator;
27 28 29 30 31 32 33 34
import java.util.Set;

/**
 * Manages users, including loading, creating and deleting.
 *
 * @author Matt Tucker
 * @see User
 */
35
public class UserManager implements IQResultListener {
36

37 38 39
    /**
     * Cache of local users.
     */
40
    private static Cache<String, User> userCache;
41 42 43
    /**
     * Cache if a local or remote user exists.
     */
44
    private static Cache<String, Boolean> registeredUsersCache;
45 46 47 48 49
    private static UserProvider provider;
    private static UserManager instance = new UserManager();

    static {
        // Initialize caches.
50 51 52
        userCache = CacheManager.initializeCache("User", "userCache", 512 * 1024);
        registeredUsersCache =
                CacheManager.initializeCache("Users Existence", "registeredUsersCache", 512 * 1024);
53
        CacheManager.initializeCache("Roster", "username2roster", 512 * 1024);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        // Load a user provider.
        String className = JiveGlobals.getXMLProperty("provider.user.className",
                "org.jivesoftware.wildfire.user.DefaultUserProvider");
        try {
            Class c = ClassUtils.forName(className);
            provider = (UserProvider)c.newInstance();
        }
        catch (Exception e) {
            Log.error("Error loading user provider: " + className, e);
            provider = new DefaultUserProvider();
        }
    }

    /**
     * Returns the currently-installed UserProvider. <b>Warning:</b> in virtually all
     * cases the user provider should not be used directly. Instead, the appropriate
     * methods in UserManager should be called. Direct access to the user provider is
     * only provided for special-case logic.
     *
     * @return the current UserProvider.
     */
    public static UserProvider getUserProvider() {
        return provider;
    }

    /**
     * Returns a singleton UserManager instance.
     *
     * @return a UserManager instance.
     */
    public static UserManager getInstance() {
        return instance;
    }

    private UserManager() {

    }

    /**
     * Creates a new User. Required values are username and password. The email address
     * can optionally be <tt>null</tt>.
     *
     * @param username the new and unique username for the account.
     * @param password the password for the account (plain text).
     * @param email the email address to associate with the new account, which can
     *      be <tt>null</tt>.
     * @return a new User.
     * @throws UserAlreadyExistsException if the username already exists in the system.
     * @throws UnsupportedOperationException if the provider does not support the
     *      operation.
     */
    public User createUser(String username, String password, String name, String email)
            throws UserAlreadyExistsException
    {
        if (provider.isReadOnly()) {
            throw new UnsupportedOperationException("User provider is read-only.");
        }
        // Make sure that the username is valid.
        try {
            username = Stringprep.nodeprep(username);
        }
        catch (StringprepException se) {
            throw new IllegalArgumentException("Invalid username: " + username,  se);
        }
        User user = provider.createUser(username, password, name, email);
        userCache.put(username, user);

        // Fire event.
        UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_created,
                Collections.emptyMap());

        return user;
    }

    /**
     * Deletes a user (optional operation).
     *
     * @param user the user to delete.
     */
    public void deleteUser(User user) {
        if (provider.isReadOnly()) {
            throw new UnsupportedOperationException("User provider is read-only.");
        }

        String username = user.getUsername();
        // Make sure that the username is valid.
        try {
            username = Stringprep.nodeprep(username);
        }
        catch (StringprepException se) {
            throw new IllegalArgumentException("Invalid username: " + username,  se);
        }

        // Fire event.
        UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_deleting,
                Collections.emptyMap());

        provider.deleteUser(user.getUsername());
        // Remove the user from cache.
        userCache.remove(user.getUsername());
    }

    /**
     * Returns the User specified by username.
     *
     * @param username the username of the user.
     * @return the User that matches <tt>username</tt>.
     * @throws UserNotFoundException if the user does not exist.
     */
    public User getUser(String username) throws UserNotFoundException {
        // Make sure that the username is valid.
        username = username.trim().toLowerCase();
166
        User user = userCache.get(username);
167
        if (user == null) {
168 169
            synchronized (username.intern()) {
                user = userCache.get(username);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                if (user == null) {
                    user = provider.loadUser(username);
                    userCache.put(username, user);
                }
            }
        }
        return user;
    }

    /**
     * Returns the total number of users in the system.
     *
     * @return the total number of users.
     */
    public int getUserCount() {
        return provider.getUserCount();
    }

    /**
     * Returns an unmodifiable Collection of all users in the system.
     *
     * @return an unmodifiable Collection of all users.
     */
    public Collection<User> getUsers() {
        return provider.getUsers();
    }

    /**
     * Returns an unmodifiable Collection of all users starting at <tt>startIndex</tt>
     * with the given number of results. This is useful to support pagination in a GUI
     * where you may only want to display a certain number of results per page. It is
     * possible that the number of results returned will be less than that specified
     * by <tt>numResults</tt> if <tt>numResults</tt> is greater than the number of
     * records left to display.
     *
     * @param startIndex the beginning index to start the results at.
     * @param numResults the total number of results to return.
     * @return a Collection of users in the specified range.
     */
    public Collection<User> getUsers(int startIndex, int numResults) {
        return provider.getUsers(startIndex, numResults);
    }

    /**
     * Returns the set of fields that can be used for searching for users. Each field
     * returned must support wild-card and keyword searching. For example, an
     * implementation might send back the set {"Username", "Name", "Email"}. Any of
     * those three fields can then be used in a search with the
     * {@link #findUsers(Set,String)} method.<p>
     *
     * This method should throw an UnsupportedOperationException if this
     * operation is not supported by the backend user store.
     *
     * @return the valid search fields.
     * @throws UnsupportedOperationException if the provider does not
     *      support the operation (this is an optional operation).
     */
    public Set<String> getSearchFields() throws UnsupportedOperationException {
        return provider.getSearchFields();
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
232
     * Searches for users based on a set of fields and a query string. The fields must
233 234 235 236
     * be taken from the values returned by {@link #getSearchFields()}. The query can
     * include wildcards. For example, a search on the field "Name" with a query of "Ma*"
     * might return user's with the name "Matt", "Martha" and "Madeline".<p>
     *
Matt Tucker's avatar
Matt Tucker committed
237 238
     * This method throws an UnsupportedOperationException if this operation is
     * not supported by the user provider.
239 240 241 242 243 244 245 246 247 248 249 250
     *
     * @param fields the fields to search on.
     * @param query the query string.
     * @return a Collection of users that match the search.
     * @throws UnsupportedOperationException if the provider does not
     *      support the operation (this is an optional operation).
     */
    public Collection<User> findUsers(Set<String> fields, String query)
            throws UnsupportedOperationException
    {
        return provider.findUsers(fields, query);
    }
251

252 253 254 255 256 257 258 259 260 261 262 263
    /**
     * Returns the value of the specified property for the given username. If the user
     * has been loaded into memory then the ask the user to return the value of the
     * property. However, if the user is not present in memory then try to get the property
     * value directly from the database as a way to optimize the performance.
     *
     * @param username the username of the user to get a specific property value.
     * @param propertyName the name of the property to return its value.
     * @return the value of the specified property for the given username.
     */
    public String getUserProperty(String username, String propertyName) {
        username = username.trim().toLowerCase();
264
        User user = userCache.get(username);
265 266 267 268 269 270 271 272 273
        if (user == null) {
            return User.getPropertyValue(username, propertyName);
        }
        else {
            // User is in memory so ask the user for the specified property value
            return user.getProperties().get(propertyName);
        }
    }

274 275 276 277 278 279 280
    /**
     * Returns true if the specified local username belongs to a registered local user.
     *
     * @param username to username of the user to check it it's a registered user.
     * @return true if the specified JID belongs to a local registered user.
     */
    public boolean isRegisteredUser(String username) {
281 282 283
        if (username == null || "".equals(username)) {
            return false;
        }
284
        // Look up in the cache
285
        Boolean isRegistered = registeredUsersCache.get(username);
286 287 288 289 290 291 292 293 294 295 296 297
        if (isRegistered == null) {
            // No information is cached so check user identity and cache it
            try {
                getUser(username);
                isRegistered = Boolean.TRUE;
            }
            catch (UserNotFoundException e) {
                isRegistered = Boolean.FALSE;
            }
            // Cache "discovered" information
            registeredUsersCache.put(username, isRegistered);
        }
298
        return isRegistered;
299 300 301 302 303 304 305 306 307 308 309
    }

    /**
     * Returns true if the specified JID belongs to a local or remote registered user. For
     * remote users (i.e. domain does not match local domain) a disco#info request is going
     * to be sent to the bare JID of the user.
     *
     * @param user to JID of the user to check it it's a registered user.
     * @return true if the specified JID belongs to a local or remote registered user.
     */
    public boolean isRegisteredUser(JID user) {
310
        Boolean isRegistered;
311 312
        XMPPServer server = XMPPServer.getInstance();
        if (server.isLocal(user)) {
313
            isRegistered = registeredUsersCache.get(user.getNode());
314 315 316
        }
        else {
            // Look up in the cache using the full JID
317
            isRegistered = registeredUsersCache.get(user.toString());
318 319
            if (isRegistered == null) {
                // Check if the bare JID of the user is cached
320
                isRegistered = registeredUsersCache.get(user.toBareJID());
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
            }
        }

        if (isRegistered == null) {
            // No information is cached so check user identity and cache it
            if (server.isLocal(user)) {
                // User belongs to local user so no disco is used in this case
                try {
                    getUser(user.getNode());
                    isRegistered = Boolean.TRUE;
                }
                catch (UserNotFoundException e) {
                    isRegistered = Boolean.FALSE;
                }
                // Cache "discovered" information
                registeredUsersCache.put(user.getNode(), isRegistered);
            }
            else {
                // A disco#info is going to be sent to the bare JID of the user. This packet
                // is going to be handled by the remote server.
                IQ iq = new IQ(IQ.Type.get);
                iq.setFrom(server.getServerInfo().getName());
                iq.setTo(user.toBareJID());
                iq.setChildElement("query", "http://jabber.org/protocol/disco#info");
                // Send the disco#info request to the remote server. The reply will be
                // processed by the IQResultListener (interface that this class implements)
                server.getIQRouter().addIQResultListener(iq.getID(), this);
                synchronized (user.toBareJID().intern()) {
                    server.getIQRouter().route(iq);
                    // Wait for the reply to be processed. Time out in 10 minutes.
                    try {
                        user.toBareJID().intern().wait(600000);
                    }
354 355 356
                    catch (InterruptedException e) {
                        // Do nothing
                    }
357 358
                }
                // Get the discovered result
359
                isRegistered = registeredUsersCache.get(user.toBareJID());
360 361 362 363 364 365 366 367
                if (isRegistered == null) {
                    // Disco failed for some reason (i.e. we timed out before getting a result)
                    // so assume that user is not anonymous and cache result
                    isRegistered = Boolean.FALSE;
                    registeredUsersCache.put(user.toString(), isRegistered);
                }
            }
        }
368
        return isRegistered;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    }

    public void receivedAnswer(IQ packet) {
        JID from = packet.getFrom();
        // Assume that the user is not a registered user
        Boolean isRegistered = Boolean.FALSE;
        // Analyze the disco result packet
        if (IQ.Type.result == packet.getType()) {
            Element child = packet.getChildElement();
            for (Iterator it=child.elementIterator("identity"); it.hasNext();) {
                Element identity = (Element) it.next();
                String accountType = identity.attributeValue("type");
                if ("registered".equals(accountType) || "admin".equals(accountType)) {
                    isRegistered = Boolean.TRUE;
                    break;
                }
            }
        }
        // Update cache of remote registered users
        registeredUsersCache.put(from.toBareJID(), isRegistered);

        // Wake up waiting thread
        synchronized (from.toBareJID().intern()) {
            from.toBareJID().intern().notifyAll();
        }
    }
395
}