1   package org.apache.turbine.test;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import java.util.List;
25  
26  import org.apache.commons.configuration.Configuration;
27  
28  import org.apache.turbine.om.security.TurbineUser;
29  import org.apache.turbine.om.security.User;
30  import org.apache.turbine.services.security.UserManager;
31  import org.apache.turbine.util.security.DataBackendException;
32  import org.apache.turbine.util.security.EntityExistsException;
33  import org.apache.turbine.util.security.PasswordMismatchException;
34  import org.apache.turbine.util.security.UnknownEntityException;
35  
36  /**
37   * This Mock object is used in testing.
38   *
39   * @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
40   * @version $Id: MockUserManager.java 615328 2008-01-25 20:25:05Z tv $
41   */
42  public class MockUserManager implements UserManager
43  {
44      /**
45       * Initializes the UserManager
46       *
47       * @param conf A Configuration object to init this Manager
48       */
49      public void init(Configuration conf)
50      {
51          // GNDN
52      }
53  
54      /**
55       * Check whether a specified user's account exists.
56       *
57       * The login name is used for looking up the account.
58       *
59       * @param user The user to be checked.
60       * @return true if the specified account exists
61       * @throws DataBackendException if there was an error accessing the data backend.
62       */
63      public boolean accountExists(User user)
64              throws DataBackendException
65      {
66          return true;
67      }
68  
69      /**
70       * Check whether a specified user's account exists.
71       *
72       * The login name is used for looking up the account.
73       *
74       * @param userName The name of the user to be checked.
75       * @return true if the specified account exists
76       * @throws DataBackendException if there was an error accessing the data backend.
77       */
78      public boolean accountExists(String userName)
79              throws DataBackendException
80      {
81          throw new DataBackendException("PassiveUserManager knows no users");
82      }
83  
84      /**
85       * Retrieve a user from persistent storage using username as the
86       * key.
87       *
88       * @param username the name of the user.
89       * @return an User object.
90       * @exception UnknownEntityException if the user's record does not
91       *            exist in the database.
92       * @exception DataBackendException if there is a problem accessing the
93       *            storage.
94       */
95      public User retrieve(String username)
96              throws UnknownEntityException, DataBackendException
97      {
98          throw new DataBackendException("PassiveUserManager knows no users");
99      }
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 }