001    package org.apache.turbine.test;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import java.util.List;
025    
026    import org.apache.commons.configuration.Configuration;
027    
028    import org.apache.turbine.om.security.TurbineUser;
029    import org.apache.turbine.om.security.User;
030    import org.apache.turbine.services.security.UserManager;
031    import org.apache.turbine.util.security.DataBackendException;
032    import org.apache.turbine.util.security.EntityExistsException;
033    import org.apache.turbine.util.security.PasswordMismatchException;
034    import org.apache.turbine.util.security.UnknownEntityException;
035    
036    /**
037     * This Mock object is used in testing.
038     *
039     * @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
040     * @version $Id: MockUserManager.java 615328 2008-01-25 20:25:05Z tv $
041     */
042    public class MockUserManager implements UserManager
043    {
044        /**
045         * Initializes the UserManager
046         *
047         * @param conf A Configuration object to init this Manager
048         */
049        public void init(Configuration conf)
050        {
051            // GNDN
052        }
053    
054        /**
055         * Check whether a specified user's account exists.
056         *
057         * The login name is used for looking up the account.
058         *
059         * @param user The user to be checked.
060         * @return true if the specified account exists
061         * @throws DataBackendException if there was an error accessing the data backend.
062         */
063        public boolean accountExists(User user)
064                throws DataBackendException
065        {
066            return true;
067        }
068    
069        /**
070         * Check whether a specified user's account exists.
071         *
072         * The login name is used for looking up the account.
073         *
074         * @param userName The name of the user to be checked.
075         * @return true if the specified account exists
076         * @throws DataBackendException if there was an error accessing the data backend.
077         */
078        public boolean accountExists(String userName)
079                throws DataBackendException
080        {
081            throw new DataBackendException("PassiveUserManager knows no users");
082        }
083    
084        /**
085         * Retrieve a user from persistent storage using username as the
086         * key.
087         *
088         * @param username the name of the user.
089         * @return an User object.
090         * @exception UnknownEntityException if the user's record does not
091         *            exist in the database.
092         * @exception DataBackendException if there is a problem accessing the
093         *            storage.
094         */
095        public User retrieve(String username)
096                throws UnknownEntityException, DataBackendException
097        {
098            throw new DataBackendException("PassiveUserManager knows no users");
099        }
100    
101        /**
102         * Retrieve a set of users that meet the specified criteria.
103         *
104         * As the keys for the criteria, you should use the constants that
105         * are defined in {@link User} interface, plus the names
106         * of the custom attributes you added to your user representation
107         * in the data storage. Use verbatim names of the attributes -
108         * without table name prefix in case of DB implementation.
109         *
110         * @param criteria The criteria of selection.
111         * @return a List of users meeting the criteria.
112         * @throws DataBackendException if there is a problem accessing the
113         *         storage.
114         * @deprecated Use <a href="#retrieveList">retrieveList</a> instead.
115         */
116        public User[] retrieve(Object criteria)
117                throws DataBackendException
118        {
119            throw new DataBackendException("PassiveUserManager knows no users");
120        }
121    
122        /**
123         * Retrieve a set of users that meet the specified criteria.
124         *
125         * As the keys for the criteria, you should use the constants that
126         * are defined in {@link User} interface, plus the names
127         * of the custom attributes you added to your user representation
128         * in the data storage. Use verbatim names of the attributes -
129         * without table name prefix in case of DB implementation.
130         *
131         * @param criteria The criteria of selection.
132         * @return a List of users meeting the criteria.
133         * @throws DataBackendException if there is a problem accessing the
134         *         storage.
135         */
136        public List retrieveList(Object criteria)
137                throws DataBackendException
138        {
139            throw new DataBackendException("PassiveUserManager knows no users");
140        }
141    
142        /**
143         * Retrieve a user from persistent storage using username as the
144         * key, and authenticate the user. The implementation may chose
145         * to authenticate to the server as the user whose data is being
146         * retrieved.
147         *
148         * @param username the name of the user.
149         * @param password the user supplied password.
150         * @return an User object.
151         * @exception PasswordMismatchException if the supplied password was
152         *            incorrect.
153         * @exception UnknownEntityException if the user's record does not
154         *            exist in the database.
155         * @exception DataBackendException if there is a problem accessing the
156         *            storage.
157         */
158        public User retrieve(String username, String password)
159                throws PasswordMismatchException, UnknownEntityException,
160                DataBackendException
161        {
162            TurbineUser tu = new TurbineUser();
163            tu.setName(username);
164            return tu;
165        }
166    
167        /**
168         * Save an User object to persistent storage. User's record is
169         * required to exist in the storage.
170         *
171         * @param user an User object to store.
172         * @exception UnknownEntityException if the user's record does not
173         *            exist in the database.
174         * @exception DataBackendException if there is a problem accessing the
175         *            storage.
176         */
177        public void store(User user)
178                throws UnknownEntityException, DataBackendException
179        {
180            throw new DataBackendException("PassiveUserManager does not support saving user data");
181        }
182    
183        /**
184         * Saves User data when the session is unbound. The user account is required
185         * to exist in the storage.
186         *
187         * LastLogin, AccessCounter, persistent pull tools, and any data stored
188         * in the permData hashtable that is not mapped to a column will be saved.
189         *
190         * @exception UnknownEntityException if the user's account does not
191         *            exist in the database.
192         * @exception DataBackendException if there is a problem accessing the
193         *            storage.
194         */
195        public void saveOnSessionUnbind(User user)
196                throws UnknownEntityException, DataBackendException
197        {
198            throw new DataBackendException("PassiveUserManager does not support saving user data");
199        }
200    
201        /**
202         * Authenticate an User with the specified password. If authentication
203         * is successful the method returns nothing. If there are any problems,
204         * exception was thrown.
205         *
206         * @param user an User object to authenticate.
207         * @param password the user supplied password.
208         * @exception PasswordMismatchException if the supplied password was
209         *            incorrect.
210         * @exception UnknownEntityException if the user's record does not
211         *            exist in the database.
212         * @exception DataBackendException if there is a problem accessing the
213         *            storage.
214         */
215        public void authenticate(User user, String password)
216                throws PasswordMismatchException, UnknownEntityException,
217                DataBackendException
218        {
219            throw new DataBackendException("PassiveUserManager knows no users");
220        }
221    
222        /**
223         * Creates new user account with specified attributes.
224         *
225         * @param user the object describing account to be created.
226         * @param initialPassword The password to use for the object creation
227         *
228         * @throws DataBackendException if there was an error accessing the data backend.
229         * @throws EntityExistsException if the user account already exists.
230         */
231        public void createAccount(User user, String initialPassword)
232                throws EntityExistsException, DataBackendException
233        {
234            throw new DataBackendException("PassiveUserManager does not support"
235                    + " creating accounts");
236        }
237    
238        /**
239         * Removes an user account from the system.
240         *
241         * @param user the object describing the account to be removed.
242         * @throws DataBackendException if there was an error accessing the data backend.
243         * @throws UnknownEntityException if the user account is not present.
244         */
245        public void removeAccount(User user)
246                throws UnknownEntityException, DataBackendException
247        {
248            throw new DataBackendException("PassiveUserManager does not support removing accounts");
249        }
250    
251        /**
252         * Change the password for an User.
253         *
254         * @param user an User to change password for.
255         * @param oldPassword the current password supplied by the user.
256         * @param newPassword the current password requested by the user.
257         * @exception PasswordMismatchException if the supplied password was
258         *            incorrect.
259         * @exception UnknownEntityException if the user's record does not
260         *            exist in the database.
261         * @exception DataBackendException if there is a problem accessing the
262         *            storage.
263         */
264        public void changePassword(User user, String oldPassword,
265                                   String newPassword)
266                throws PasswordMismatchException, UnknownEntityException,
267                DataBackendException
268        {
269            throw new DataBackendException("PassiveUserManager does not support setting passwords");
270        }
271    
272        /**
273         * Forcibly sets new password for an User.
274         *
275         * This is supposed by the administrator to change the forgotten or
276         * compromised passwords. Certain implementatations of this feature
277         * would require administrative level access to the authenticating
278         * server / program.
279         *
280         * @param user an User to change password for.
281         * @param password the new password.
282         * @exception UnknownEntityException if the user's record does not
283         *            exist in the database.
284         * @exception DataBackendException if there is a problem accessing the
285         *            storage.
286         */
287        public void forcePassword(User user, String password)
288                throws UnknownEntityException, DataBackendException
289        {
290            throw new DataBackendException("PassiveUserManager does not support setting passwords");
291        }
292    }