KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > admin > security > addressbook > AddressBookSecurityImpl


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: AddressBookSecurityImpl.java,v $
31  * Revision 1.4.2.1 2005/10/08 16:14:06 colinmacleod
32  * Fixed bugs when adding a new user.
33  *
34  * Revision 1.4 2005/04/28 18:47:10 colinmacleod
35  * Fixed XHMTL, styles and resin compatibility.
36  * Added support for URL rewriting.
37  *
38  * Revision 1.3 2005/04/10 20:32:02 colinmacleod
39  * Added new themes.
40  * Changed id type to String.
41  * Changed i tag to em and b tag to strong.
42  * Improved PicoContainerFactory with NanoContainer scripts.
43  *
44  * Revision 1.2 2005/04/09 17:19:04 colinmacleod
45  * Changed copyright text to GPL v2 explicitly.
46  *
47  * Revision 1.1.1.1 2005/03/10 17:50:44 colinmacleod
48  * Restructured ivata op around Hibernate/PicoContainer.
49  * Renamed ivata groupware.
50  *
51  * Revision 1.4 2004/11/12 18:17:08 colinmacleod
52  * Ordered imports.
53  *
54  * Revision 1.3 2004/11/12 15:56:43 colinmacleod
55  * Removed dependencies on SSLEXT.
56  * Moved Persistence classes to ivata masks.
57  *
58  * Revision 1.2 2004/11/03 15:27:41 colinmacleod
59  * Added missing persistenceSession.close().
60  *
61  * Revision 1.1 2004/09/30 15:15:32 colinmacleod
62  * Split off address book elements into security subproject.
63  *
64  * Revision 1.2 2004/07/18 21:59:09 colinmacleod
65  * Removed Person from User - now you need to use address book/persistence
66  * manager to find the person (makes the app run faster.)
67  *
68  * Revision 1.1 2004/07/13 19:41:11 colinmacleod
69  * Moved project to POJOs from EJBs.
70  * Applied PicoContainer to services layer (replacing session EJBs).
71  * Applied Hibernate to persistence layer (replacing entity EJBs).
72  * -----------------------------------------------------------------------------
73  */

74 package com.ivata.groupware.admin.security.addressbook;
75
76 import java.util.Arrays JavaDoc;
77 import java.util.Collection JavaDoc;
78 import java.util.Iterator JavaDoc;
79
80 import com.ivata.groupware.admin.security.Security;
81 import com.ivata.groupware.admin.security.SecurityImpl;
82 import com.ivata.groupware.admin.security.server.SecurityServer;
83 import com.ivata.groupware.admin.security.server.SecuritySession;
84 import com.ivata.groupware.admin.security.user.UserDO;
85 import com.ivata.groupware.business.addressbook.person.PersonDO;
86 import com.ivata.groupware.business.addressbook.person.group.GroupConstants;
87 import com.ivata.groupware.business.addressbook.person.group.GroupDO;
88 import com.ivata.groupware.container.persistence.QueryPersistenceManager;
89 import com.ivata.groupware.container.persistence.TimestampDOHandling;
90 import com.ivata.mask.Mask;
91 import com.ivata.mask.MaskFactory;
92 import com.ivata.mask.field.Field;
93 import com.ivata.mask.persistence.FinderException;
94 import com.ivata.mask.persistence.PersistenceSession;
95 import com.ivata.mask.util.StringHandling;
96 import com.ivata.mask.util.SystemException;
97 import com.ivata.mask.validation.ValidationError;
98 import com.ivata.mask.validation.ValidationException;
99
100
101 /**
102  * <p>The security bean is responsible for adding, removing and amending users
103  * to the system, and for logging in in the first place.</p>
104  *
105  * @since 2002-09-08
106  * @author Colin MacLeod
107  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
108  * @version $Revision: 1.4.2.1 $
109  * TODO: finish user right implementation
110  */

111 public class AddressBookSecurityImpl extends SecurityImpl implements AddressBookSecurity {
112     private MaskFactory maskFactory;
113     /**
114      * Persistence manger used to store/retrieve data objects, or retrieve a
115      * new persistence session.
116      */

117     private QueryPersistenceManager persistenceManager;
118
119     /**
120      * Security server - used for logging in users.
121      */

122     private SecurityServer securityServer;
123
124     /**
125      * Construct and initialize the Security implementation.
126      *
127      * @param persistenceManager persistence manager used to store/retrieve data
128      * objects.
129      */

130     public AddressBookSecurityImpl(QueryPersistenceManager persistenceManager,
131             SecurityServer securityServer,
132             final MaskFactory maskFactory,
133             final Boolean JavaDoc demoVersion) {
134         super(persistenceManager, securityServer, maskFactory, demoVersion);
135         this.persistenceManager = persistenceManager;
136         this.securityServer = securityServer;
137         this.maskFactory = maskFactory;
138     }
139
140     /**
141      * <p>Add a new user to the system. The person's details must first have
142      * been entered into the address book.</p>
143      *
144      * <p><strong>Note:</strong> This routine will fail if the person has no email
145      * address!</p>
146      *
147      * @param securitySession current security session.
148      * @param personId the id of the person EJB, giving name and address
149      * @param userNameAdd the name the user will use to log into the system. Case
150      * sensitive.
151      * @param enable if <code>true</code>, the user will be initially enabled,
152      * otherwise the user is disabled initially.
153      * @throws InvalidFieldValueException if any of the fields supplied are
154      * <code>null</code>.
155      */

156     public void addUserToPerson(final SecuritySession securitySession,
157             final PersonDO person)
158             throws SystemException {
159         if (isDemoVersion()) {
160             return;
161         }
162         // check we have field values
163
PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
164         Mask userMask = maskFactory.getMask(UserDO.class);
165         Field userNameField = userMask.getField("name");
166         Mask personMask = maskFactory.getMask(PersonDO.class);
167         try {
168             // TODO: there is duplication here between this and the addUser
169
// method - we should have a validation method for user names
170

171             // check we have field values
172
if ((person.getUser() == null)
173                     || StringHandling.isNullOrEmpty(person
174                             .getUser().getName())) {
175                 throw new ValidationException(
176                         new ValidationError(
177                                 "user",
178                                 Security.BUNDLE_PATH,
179                                 userNameField,
180                                 "errors.required"));
181             }
182
183             // check the user has an email address
184
// only if user is enabled
185
UserDO user = person.getUser();
186             // if the user is deleted, it is naturally also disabled!
187
if (user.isDeleted()) {
188                 user.setEnabled(false);
189             }
190
191             if (user.isEnabled() &&
192                     StringHandling.isNullOrEmpty(person.getEmailAddress())) {
193                 throw new ValidationException(
194                         new ValidationError(
195                                 "errors.addressBook.user.emailAddress",
196                                 Arrays.asList(new Object JavaDoc [] {
197                                         person.getFileAs()
198                                 })));
199
200             }
201             // first check this user name is not already taken
202
if (isUser(securitySession, user.getName())) {
203                 throw new ValidationException(
204                             new ValidationError(
205                                     "user",
206                                     Security.BUNDLE_PATH,
207                                     userNameField,
208                                     "errors.unique"
209                                             ));
210             }
211             // user id is always the same as person id (1-1 relationship)
212
user.setId(person.getId());
213             persistenceManager.add(persistenceSession, user);
214
215             // if the user is enabled, add it to the security server
216
if (user.isEnabled()) {
217                 securityServer.addUser(securitySession,
218                         user.getName(), getRealName(
219                         persistenceSession, user));
220             }
221
222             // each user has to be in USER group
223
// so put this new user to USER group
224
GroupDO userGroup = (GroupDO) persistenceManager.findByPrimaryKey(
225                     persistenceSession,
226                     GroupDO.class,
227                     GroupConstants.USER_GROUP);
228
229             // each user is a member of userGroup
230
user.getGroups().add(userGroup);
231             persistenceManager.amend(persistenceSession, user);
232
233             // create group in private_user group for this new user
234
createPrivateGroups(persistenceSession, securitySession, user);
235
236             TimestampDOHandling.amend(securitySession, person);
237             persistenceManager.amend(persistenceSession, person);
238         } catch (Exception JavaDoc e) {
239             persistenceSession.cancel();
240             throw new SystemException(e);
241         } finally {
242             persistenceSession.close();
243         }
244     }
245     /**
246      * <p>This method creates private userGroup and private AddressBook group.
247      * It sets rights for those new groups.</p>
248      *
249      * @param securitySession current security session.
250      * @param user user for whom to set the new groups
251      */

252     public void createPrivateGroups(
253             final PersistenceSession persistenceSession,
254             final SecuritySession securitySession,
255             final UserDO user)
256             throws SystemException {
257         if (isDemoVersion()) {
258             return;
259         }
260         // find private_user group which contain all private user groups
261
GroupDO privateUser = (GroupDO) persistenceManager.findByPrimaryKey(
262                 persistenceSession,
263                 GroupDO.class,
264                 GroupConstants.USER_GROUP_PRIVATE);
265         GroupDO privateAddressBook = (GroupDO) persistenceManager
266             .findByPrimaryKey(persistenceSession,
267                     GroupDO.class,
268                     GroupConstants.ADDRESS_BOOK_PRIVATE);
269
270         // create user's private group
271
GroupDO privateUserGroup = new GroupDO();
272         privateUserGroup.setName(user.getName());
273         privateUserGroup.setParent(privateUser);
274
275         // user is a member of her private group, only she is a member
276
privateUserGroup.getUsers().add(user);
277         privateUserGroup.setDescription("Private group for user id "
278             + user.getId());
279         privateUserGroup = (GroupDO) persistenceManager.add(persistenceSession,
280                 privateUserGroup);
281
282
283         // create user's private addressBook - it's the user's private group in
284
// group address_book/private - where she stores her contacts
285
GroupDO privateUserAddressBookGroup = new GroupDO();
286         privateUserAddressBookGroup.setParent(privateAddressBook);
287         privateUserAddressBookGroup.setName(user.getName());
288         privateUserAddressBookGroup.setDescription(
289                 "Private address book for user id "
290                 + user.getId());
291         privateUserAddressBookGroup = (GroupDO) persistenceManager.add(persistenceSession,
292             privateUserAddressBookGroup);
293
294     }
295
296     /**
297      * <p>
298      * Overriden to provide the real life name for the user.
299      * </p>
300      *
301      * @param persistenceSession Valid persistence session.
302      * @param user The user for whom to return the name.
303      * @return Real-life name for this user.
304      * @throws SystemException
305      */

306     protected String JavaDoc getRealName(final PersistenceSession persistenceSession,
307             final UserDO user)
308             throws SystemException {
309         // TODO: should really be file as
310
return user.getName();
311     }
312     /**
313      * Refer to {@link }.
314      *
315      * @param securitySessionParam
316      * @param userNameParam
317      * @return
318      * @throws SystemException
319      * @see com.ivata.groupware.admin.security.Security#isUser(com.ivata.groupware.admin.security.server.SecuritySession, java.lang.String)
320      */

321     public boolean isUser(SecuritySession securitySessionParam,
322             String JavaDoc userNameParam) throws SystemException {
323         if (isDemoVersion()) {
324             return false;
325         }
326         boolean isUser = super.isUser(securitySessionParam, userNameParam);
327         if (!isUser) {
328             PersistenceSession persistenceSession = persistenceManager
329                 .openSession(securitySessionParam);
330             try {
331                 persistenceManager.findInstance(persistenceSession,
332                     "securityUserByName",
333                     new Object JavaDoc[] { userNameParam });
334                 isUser = true;
335             } catch (FinderException thatsGood) {
336                 // that's good!
337
} finally {
338                 persistenceSession.close();
339             }
340         }
341         return isUser;
342     }
343
344     /**
345      * <p>if userName emergency is trying login -> find first admin .</p>
346      * @param userName
347      * @return
348      */

349     public String JavaDoc loginAgain(final SecuritySession securitySession,
350             final String JavaDoc userNameParam)
351             throws SystemException {
352         String JavaDoc userNameReturn = userNameParam;
353         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
354         try {
355             if (userNameReturn.equals("emergency")) {
356                 GroupDO group = (GroupDO) persistenceManager.findByPrimaryKey(
357                         persistenceSession,
358                         GroupDO.class,
359                         GroupConstants.GROUP_ADMINISTRATOR);
360                 Collection JavaDoc admins = group.getUsers();
361                 Iterator JavaDoc iterator = admins.iterator();
362                 UserDO firstAdmin = (UserDO) iterator.next();
363                 userNameReturn = firstAdmin.getName();
364             }
365         } catch (Exception JavaDoc e) {
366             persistenceSession.cancel();
367             throw new SystemException(e);
368         } finally {
369             persistenceSession.close();
370         }
371
372         return userNameReturn;
373     }
374     protected void onAmendUserName(final SecuritySession securitySession,
375             final PersistenceSession persistenceSession,
376             final UserDO user,
377             final String JavaDoc oldUserName)
378             throws SystemException {
379         if (isDemoVersion()) {
380             return;
381         }
382         // if everyThing is OK update users private groups
383
// if user has some contacts in Personal addressBook don't delete those
384
GroupDO group = (GroupDO) persistenceManager.findInstance(persistenceSession,
385             "addressBookGroupsInGroupByName",
386             new Object JavaDoc[] { GroupConstants.ADDRESS_BOOK_PRIVATE, oldUserName });
387         group.setName(user.getName());
388         persistenceManager.amend(persistenceSession, group);
389
390         group = (GroupDO) persistenceManager.findInstance(persistenceSession,
391             "addressBookGroupsInGroupByName",
392             new Object JavaDoc[] { GroupConstants.USER_GROUP_PRIVATE, oldUserName });
393         group.setName(user.getName());
394         persistenceManager.amend(persistenceSession, group);
395         super.onAmendUserName(securitySession,
396                 persistenceSession, user, oldUserName);
397     }
398 }
399
Popular Tags