001package org.apache.fulcrum.security.memory;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.fulcrum.security.entity.User;
025import org.apache.fulcrum.security.spi.AbstractUserManager;
026import org.apache.fulcrum.security.util.DataBackendException;
027import org.apache.fulcrum.security.util.UnknownEntityException;
028import org.apache.fulcrum.security.util.UserSet;
029
030/**
031 * This implementation keeps all objects in memory. This is mostly meant to help
032 * with testing and prototyping of ideas.
033 *
034 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
035 * @version $Id$
036 */
037public class MemoryUserManagerImpl extends AbstractUserManager {
038
039        // TODO Need to load up Crypto component and actually encrypt passwords!
040
041        private static List<User> users = new ArrayList<User>();
042        
043        public MemoryUserManagerImpl() {
044        // reset
045            users.clear();
046    }
047
048        /** Our Unique ID counter */
049        // private static int uniqueId = 0;
050
051        /**
052         * Check whether a specified user's account exists.
053         *
054         * The login name is used for looking up the account.
055         *
056         * @param userName The name of the user to be checked.
057         * @return true if the specified account exists
058         * @throws DataBackendException if there was an error accessing the data
059         *                              backend.
060         */
061        public boolean checkExists(String userName) throws DataBackendException {
062                return MemoryHelper.checkExists(users, userName);
063        }
064
065        /**
066         * Retrieves all users defined in the system.
067         *
068         * @return the names of all users defined in the system.
069         * @throws DataBackendException if there was an error accessing the data
070         *                              backend.
071         */
072        public UserSet getAllUsers() throws DataBackendException {
073                return new UserSet(users);
074        }
075
076        /**
077         * Removes an user account from the system.
078         *
079         * @param user the object describing the account to be removed.
080         * @throws DataBackendException   if there was an error accessing the data
081         *                                backend.
082         * @throws UnknownEntityException if the user account is not present.
083         */
084        public void removeUser(User user) throws DataBackendException, UnknownEntityException {
085                users.remove(user);
086        }
087
088        /**
089         * Creates new user account with specified attributes.
090         *
091         * @param user the object describing account to be created.
092         * @throws DataBackendException if there was an error accessing the data
093         *                              backend.
094         */
095        @Override
096        protected <T extends User> T persistNewUser(T user) throws DataBackendException {
097                users.remove(user);
098                user.setId(MemoryHelper.getUniqueId());
099                users.add(user);
100                return user;
101        }
102
103        /**
104         * Stores User attributes. The User is required to exist in the system.
105         *
106         * @param user The User to be stored.
107         * @throws DataBackendException   if there was an error accessing the data
108         *                                backend
109         * @throws UnknownEntityException if the role does not exist.
110         */
111        public void saveUser(User user) throws DataBackendException, UnknownEntityException {
112                boolean userExists = false;
113                userExists = checkExists(user);
114                if (userExists) {
115                        users.remove(user);
116                        users.add(user);
117                } else {
118                        throw new UnknownEntityException("Unknown user '" + user + "'");
119                }
120        }
121}