001: package hero.session;
002:
003: /*
004: * 02/01/2002 - 15:24:07
005: *
006: * UserRegistrationEJB.java -
007: * Copyright (C) 2002 Ecoo Team
008: * valdes@loria.fr
009: *
010: *
011: * This program is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public License
013: * as published by the Free Software Foundation; either version 2
014: * of the License, or (at your option) any later version.
015: *
016: * This program is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License
022: * along with this program; if not, write to the Free Software
023: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
024: */
025: import hero.interfaces.BnAuthRoleLocal;
026: import hero.interfaces.BnAuthRoleLocalHome;
027: import hero.interfaces.BnAuthRoleUtil;
028: import hero.interfaces.BnAuthRoleValue;
029: import hero.interfaces.BnRoleLocal;
030: import hero.interfaces.BnRoleLocalHome;
031: import hero.interfaces.BnRoleUtil;
032: import hero.interfaces.BnUserLocal;
033: import hero.interfaces.BnUserLocalHome;
034: import hero.interfaces.BnUserPropertyLocal;
035: import hero.interfaces.BnUserPropertyLocalHome;
036: import hero.interfaces.BnUserPropertyUtil;
037: import hero.interfaces.BnUserUtil;
038: import hero.interfaces.BnUserValue;
039: import hero.interfaces.BnProjectLocal;
040: import hero.interfaces.InvalidValueException;
041: import hero.util.HeroException;
042:
043: import java.rmi.RemoteException;
044: import java.util.Collection;
045: import java.util.Iterator;
046:
047: import javax.ejb.CreateException;
048: import javax.ejb.FinderException;
049: import javax.ejb.RemoveException;
050: import javax.ejb.SessionBean;
051: import javax.ejb.SessionContext;
052:
053: /**
054: * The User Registration Bean, is an stateless session bean that provides the API to create users in
055: * Bonita database (new user account). This API allows some functionalities oriented to user
056: * configuration: set authorization roles, set Bonita roles, set preferences...
057: *
058: * <br><br>
059: * <strong>The following lines shows a sample code to use this API in your application:<br><br></strong>
060: * <br>
061: * First of all you have to import the User Registration files:<br><br>
062: *
063: * import hero.interfaces.UserRegistrationLocalHome;<br>
064: * import hero.interfaces.UserRegistrationLocal;<br>
065: * import hero.interfaces.UserRegistrationHome;<br>
066: * import hero.interfaces.UserRegistration;<br>
067: * import hero.interfaces.UserRegistrationUtil;<br>
068: *
069: * <br>
070: * Now, it is time to create the User Registration instance:<br>
071: * <br>
072: * Like this if you want to use local interfaces:<br><br>
073: *
074: * hero.interfaces.UserRegistrationLocalHome userh = (UserRegistrationLocalHome)hero.interfaces.UserRegistrationUtil.getLocalHome();<br>
075: * hero.interfaces.UserRegistrationLocal user = userh.create();<br><br>
076: *
077: * or like this if you use remote interfaces:<br>
078: * <br>
079: *
080: * hero.interfaces.UserRegistrationHome userh = (UserRegistrationHome)hero.interfaces.UserRegistrationUtil.getHome();<br>
081: * hero.interfaces.UserRegistration user = userh.create();
082: * <br>
083: * <br>
084: * Now, we can create a new user account by using userCreate methods (with or without the instant
085: * messaging address).<br><br>
086: *
087: * user.userCreate(name,password,email);<br>
088: * or
089: * user.userCreate(name,password,email,jabber);<br>
090: *
091: *
092: * @ejb:bean name="UserRegistration"
093: * display-name="UserRegistration Bean"
094: * type="Stateless"
095: * transaction-type="Container"
096: * jndi-name="ejb/hero/UserRegistration"
097: * local-jndi-name="ejb/hero/UserRegistration_L"
098: *
099: * @ejb:ejb-ref ejb-name="UserRegistration"
100: * ref-name="myhero/UserRegistration"
101: * @ejb:transaction type="Required"
102: * @ejb:transaction-type type="Container"
103: * @ejb.permission unchecked="yes"
104: * @ejb.security-identity run-as="SuperAdmin"
105: * @jonas.bean
106: * ejb-name="UserRegistration"
107: * jndi-name="ejb/hero/UserRegistration"
108: *
109: * @jboss.container-configuration name="Standard Stateless SessionBean for Bonita"
110: *
111: * @copyright INRIA
112: * @author Miguel Valdes
113: *
114: **/
115:
116: public class UserRegistrationBean implements SessionBean {
117:
118: // -------------------------------------------------------------------------
119: // Members
120: // -------------------------------------------------------------------------
121:
122: private SessionContext mContext;
123:
124: private BnUserLocalHome userhome;
125: private BnAuthRoleLocalHome rolehome;
126:
127: // -------------------------------------------------------------------------
128: // Methods
129: // -------------------------------------------------------------------------
130:
131: /**
132: * Create the role BONITAUSER if it doesn't exist
133: *
134: * @throws HeroException, (catch : FinderException)
135: */
136: public void assertUserSecurityRoleExists() throws HeroException {
137:
138: String role = hero.interfaces.Constants.USER_SECURITY_ROLE;
139: BnAuthRoleLocal _role;
140:
141: try {
142: _role = rolehome.findByName(role);
143: } catch (javax.ejb.FinderException fe) {
144: roleCreate(role, role);
145: }
146: }
147:
148: /**
149: * Create the User Session Bean. This method is the first one to invoke in order to
150: * creates a new user account.
151: *
152: * @throws CreateException
153: *
154: * @ejb:create-method view-type="both"
155: **/
156: public void ejbCreate() throws CreateException {
157: }
158:
159: /**
160: * Creates user account. You have to call this method after "create" call (this API is an stateles
161: * session bean). This method is oriented to Bonita external users configuration
162: * @param name name of the new user
163: * @ejb:interface-method view-type="local"
164: * @throws HeroException
165: *
166: **/
167: public BnUserLocal userCreate(String name) throws HeroException {
168: try {
169: BnUserLocal ul = userhome.findByName(name);
170: throw new HeroException("User " + name + " already exist ");
171: } catch (javax.ejb.FinderException nn) {
172: try {
173: BnUserValue newUser = new BnUserValue();
174: newUser.setName(name);
175: BnUserLocal user = userhome.create(newUser);
176: assertUserSecurityRoleExists();
177: this .setUserRole(name,
178: hero.interfaces.Constants.USER_SECURITY_ROLE);
179: return user;
180: } catch (InvalidValueException ie) {
181: throw new HeroException(ie.getMessage());
182: } catch (CreateException f) {
183: throw new HeroException(name + " is already exist");
184: }
185: }
186: }
187:
188: /**
189: * Creates user account. You have to call this method after "create" call (this API is an stateles
190: * session bean).
191: * @param name name of the new user
192: * @param password password of the new user
193: * @param email email of the new user
194: * @ejb:interface-method view-type="both"
195: * @throws HeroException
196: *
197: **/
198: public void userCreate(String name, String password, String email)
199: throws HeroException {
200: try {
201: BnUserLocal ul = userhome.findByName(name);
202: throw new HeroException("User " + name + " already exist ");
203: } catch (javax.ejb.FinderException nn) {
204: try {
205: BnUserValue newUser = new BnUserValue();
206: newUser.setName(name);
207: newUser.setPassword(password);
208: newUser.setEmail(email);
209: BnUserLocal user = userhome.create(newUser);
210: assertUserSecurityRoleExists();
211: this .setUserRole(name,
212: hero.interfaces.Constants.USER_SECURITY_ROLE);
213: } catch (InvalidValueException ie) {
214: throw new HeroException(ie.getMessage());
215: } catch (CreateException f) {
216: throw new HeroException(name + " is already exist");
217: }
218: }
219: }
220:
221: /**
222: * Creates user account with an instant messaging address. You have to call this method after "create" call (this API is an stateles
223: * session bean).
224: * @param name name of the new user
225: * @param password password of the new user
226: * @param email email of the new user
227: * @param jabber jabber address of the new user
228: * @ejb:interface-method view-type="both"
229: * @throws HeroException
230: *
231: **/
232: public void userCreate(String name, String password, String email,
233: String jabber) throws HeroException {
234: try {
235: BnUserLocal ul = userhome.findByName(name);
236: throw new HeroException("User " + name + " already exist ");
237: } catch (javax.ejb.FinderException nn) {
238: try {
239: BnUserValue newUser = new BnUserValue();
240: newUser.setName(name);
241: newUser.setPassword(password);
242: newUser.setEmail(email);
243: newUser.setJabber(jabber);
244: BnUserLocal user = userhome.create(newUser);
245: assertUserSecurityRoleExists();
246: this .setUserRole(name,
247: hero.interfaces.Constants.USER_SECURITY_ROLE);
248: } catch (InvalidValueException ie) {
249: throw new HeroException(ie.getMessage());
250: } catch (CreateException f) {
251: throw new HeroException(name + " is already exist");
252: }
253: }
254: }
255:
256: /**
257: * Detete a user from Bonita database. If the user is included in active projects this
258: * methods throws an exception.
259: *
260: * @param userName the name of the user
261: * @ejb:interface-method view-type="both"
262: * @ejb:transaction type="Required"
263: * @throws HeroException
264: *
265: **/
266: public void deleteUser(String userName) throws HeroException {
267:
268: try {
269: BnUserLocal user = userhome.findByName(userName);
270: Collection projects = user.getBnProjects();
271: Iterator i = projects.iterator();
272: while (i.hasNext()) {
273: BnProjectLocal project = (BnProjectLocal) i.next();
274: if (project.getState() != hero.interfaces.Constants.Pj.TERMINATED)
275: throw new HeroException(
276: "Cannot remove user: This user takes part in some workflow projects ");
277: }
278: user.remove();
279: } catch (FinderException fe) {
280: throw new HeroException(fe.getMessage());
281: } catch (RemoveException rm) {
282: throw new HeroException(rm.getMessage());
283: }
284: }
285:
286: /**
287: * Creates a new authorization role to the system. This kind of role is used to control the user
288: * access to different APIs. You have to call this method after "create" call (this API is an stateles
289: * session bean).
290: * @param name name of the new role
291: * @param roleGroup the name of the group
292: * @ejb:interface-method view-type="both"
293: * @throws HeroException
294: *
295: **/
296: public void roleCreate(String name, String roleGroup)
297: throws HeroException {
298: try {
299: BnAuthRoleValue newRole = new BnAuthRoleValue();
300: newRole.setName(name);
301: newRole.setBnRoleGroup(roleGroup);
302: BnAuthRoleLocal role = rolehome.create(newRole);
303: } catch (CreateException f) {
304: throw new HeroException(name + " is already exist");
305: }
306: }
307:
308: /**
309: * Set a new authorization role to the user. You have to call this method after "create" call
310: * (this API is an stateles session bean).
311: * @param username name of the user
312: * @param rolename name of the role
313: * @ejb:interface-method view-type="both"
314: * @throws HeroException
315: *
316: **/
317: public void setUserRole(String userName, String roleName)
318: throws HeroException {
319: try {
320: BnUserLocal user = userhome.findByName(userName);
321: BnAuthRoleLocal role = rolehome.findByName(roleName);
322: Collection roles = user.getBnAuthRoles();
323: roles.add(role);
324: } catch (Exception f) {
325: throw new HeroException(userName
326: + " Error in setUserAuthRole...");
327: }
328: }
329:
330: /**
331: * Set a new property to the user. User properties will be used to define user preferences.
332: * You have to call this method after "create" call (this API is an stateles session bean).
333: *
334: * @param userName the name of the user
335: * @param key the key of user property
336: * @param value the value of user property
337: * @ejb:interface-method view-type="both"
338: * @throws HeroException
339: *
340: **/
341: public void setUserProperty(String userName, String key,
342: String value) throws HeroException {
343:
344: BnUserLocalHome userhome;
345: BnUserLocal mUser;
346: BnUserPropertyLocalHome pHome;
347: BnUserPropertyLocal propertyLocal = null;
348:
349: try {
350: userhome = BnUserUtil.getLocalHome();
351: mUser = userhome.findByName(userName);
352:
353: try {
354: pHome = BnUserPropertyUtil.getLocalHome();
355: } catch (javax.naming.NamingException ne) {
356: throw new HeroException(ne.getMessage());
357: }
358: Collection c = null;
359: try {
360: c = pHome.findByTheKey(mUser.getId(), key);
361: } catch (FinderException fe) {
362: throw new HeroException(fe.getMessage());
363: }
364:
365: if (!c.isEmpty()) {
366:
367: propertyLocal = (BnUserPropertyLocal) (c.toArray())[0];
368: propertyLocal.setTheValue(value);
369:
370: } else {
371: try {
372: propertyLocal = pHome.create(key, value);
373: propertyLocal.setBnUser(mUser);
374:
375: } catch (InvalidValueException ie) {
376: throw new HeroException(ie.getMessage());
377: } catch (javax.ejb.CreateException ce) {
378: throw new HeroException(ce.getMessage());
379: }
380: }
381: /*
382: hero.interfaces.UserSessionLocalHome usersessionhome = (UserSessionLocalHome)hero.interfaces.UserSessionUtil.getLocalHome();
383: UserSessionLocal user = usersessionhome.create();
384: user.setUserProperty(key,value);*/
385:
386: } catch (Exception f) {
387: throw new HeroException("Error in setUserProperty");
388: }
389: }
390:
391: /**
392: * Internal Enterprise Java Beans method.
393: **/
394:
395: public void setSessionContext(javax.ejb.SessionContext context)
396: throws RemoteException {
397: this .mContext = context;
398: try {
399: userhome = BnUserUtil.getLocalHome();
400: rolehome = BnAuthRoleUtil.getLocalHome();
401: } catch (Exception e) {
402: throw new RemoteException(e.getMessage());
403: }
404: }
405:
406: /**
407: * Internal Enterprise Java Beans method.
408: **/
409: public void unsetSessionContext() throws RemoteException {
410: }
411:
412: /**
413: * Internal Enterprise Java Beans method.
414: **/
415: public void ejbRemove() throws java.rmi.RemoteException {
416: }
417:
418: /**
419: * Internal Enterprise Java Beans method.
420: **/
421: public void ejbActivate() throws java.rmi.RemoteException {
422: }
423:
424: /**
425: * Internal Enterprise Java Beans method.
426: **/
427: public void ejbPassivate() throws java.rmi.RemoteException {
428: }
429:
430: }
|