001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.dbobj;
066:
067: import com.jcorporate.expresso.core.controller.ControllerRequest;
068: import com.jcorporate.expresso.core.db.DBConnection;
069: import com.jcorporate.expresso.core.db.DBException;
070: import com.jcorporate.expresso.core.dbobj.DBField;
071: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
072: import com.jcorporate.expresso.core.misc.StringUtil;
073: import com.jcorporate.expresso.core.security.User;
074: import com.jcorporate.expresso.core.security.UserListener;
075: import org.apache.log4j.Logger;
076:
077: import java.util.Date;
078: import java.util.Iterator;
079:
080: /**
081: * Group Member definitions table. IMPORTANT: Use getGroups() from the User object
082: * to determine which groups a certain user belongs to, as the implementation may
083: * or may not use this table - LDAP may be used instead, for example, or an external
084: * application
085: *
086: * @author Michael Nash
087: * @since Expresso 1.0
088: */
089: public class GroupMembers extends SecurityDBObject implements
090: UserListener {
091:
092: /* How many levels of nesting do we support? */
093: private static int maxDepth = 20;
094: private static Logger log = Logger.getLogger(GroupMembers.class);
095:
096: public static final String GROUP_NAME = "GroupName";
097: public static final String EXPIRES = "Expires";
098: public static final String EXPUID = "ExpUid";
099: public static final String TABLE = "GROUPMEMBERS";
100:
101: /* Add some user listeners, so that whenever a User object is added/modded/deleted some action can
102: * be taken by these classes. These classes all implement the UserListener interface.
103: */
104:
105: static {
106: try {
107: User.addListener(new GroupMembers());
108: } catch (Exception e) {
109: e.printStackTrace();
110: System.err.println(e.getMessage());
111: }
112: }
113:
114: //
115: // user.addListener((UserListener) new com.jcorporate.expresso.services.dbobj.GroupMembers());
116: // user.addListener((UserListener)new com.jcorporate.expresso.services.dbobj.UserPreference());
117:
118: /**
119: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
120: */
121: public GroupMembers() throws DBException {
122: } /* GroupMembers() */
123:
124: /**
125: * Initializes object with permissions given by user id.
126: *
127: * @param uid The Owner's uid
128: */
129: public GroupMembers(int uid) throws DBException {
130: super (uid);
131: }
132:
133: /**
134: * For using DBObjects within Controllers. Initializes based upon the current
135: * user and the requested db. [Of course this can be modified later]
136: *
137: * @param request - The controller request handed to you by the framework.
138: */
139: public GroupMembers(ControllerRequest request) throws DBException {
140: super (request);
141: }
142:
143: /**
144: * constructor for db transactions
145: *
146: * @param localConnection the DB connection which should be used, typically because of an ongoing transaction
147: */
148: public GroupMembers(DBConnection localConnection)
149: throws DBException {
150: if (localConnection != null) {
151: setConnection(localConnection);
152: }
153: }
154:
155: /**
156: * Extend add to handle nested groups
157: */
158: public void add() throws DBException {
159: add(0);
160: } /* add() */
161:
162: /**
163: * Extend add to handle nested groups
164: *
165: * @param depth The security depth to add nesting
166: * @throws DBException if there's an error nesting group members
167: */
168: private void add(int depth) throws DBException {
169: if (depth > maxDepth) {
170: log.warn("Tried to add security group nesting more than "
171: + maxDepth + " levels. Security was only added to "
172: + maxDepth + " levels.");
173: }
174:
175: GroupNest gnl = new GroupNest(SecuredDBObject.SYSTEM_ACCOUNT);
176: gnl.setDataContext(getDataContext());
177:
178: GroupNest oneNest = null;
179: gnl.setField("GroupName", getField(GROUP_NAME));
180:
181: for (Iterator enl = gnl.searchAndRetrieveList().iterator(); enl
182: .hasNext();) {
183: oneNest = (GroupNest) enl.next();
184:
185: GroupMembers newMember = new GroupMembers(
186: SecuredDBObject.SYSTEM_ACCOUNT);
187: newMember.setDataContext(getDataContext());
188:
189: /* check to see if the user is a member of the nested group. */
190:
191: /* If not, add them */
192: newMember.clear();
193: newMember.setField(EXPUID, getField(EXPUID));
194: newMember
195: .setField(GROUP_NAME, oneNest.getField("MemberOf"));
196:
197: if (!newMember.find()) {
198: newMember.add(depth + 1);
199: }
200: } /* for each nesting entry for this group */
201:
202: super .add();
203: } /* add(int) */
204:
205: public void addedUser(User user) throws DBException {
206: }
207:
208: /**
209: * Extends the checkAllRefs method to check for valid UserGroup
210: *
211: * @throws DBException If a referential integrity violation is found
212: */
213: protected void checkAllRefs() throws DBException {
214: checkRef(GROUP_NAME, new UserGroup(), "Invalid "
215: + getString(getMetaData().getDescription(GROUP_NAME)));
216: } /* checkAllRefs() */
217:
218: /**
219: * Called when a User is deleted from the system.
220: *
221: * @param user The user that is deleted
222: * @throws DBException upon data access error
223: */
224: public void deletedUser(User user) throws DBException {
225: GroupMembers gml = new GroupMembers();
226: gml.setDataContext(user.getDataContext());
227: gml.setField(EXPUID, user.getUid());
228:
229: GroupMembers oneMember = null;
230:
231: for (Iterator e = gml.searchAndRetrieveList().iterator(); e
232: .hasNext();) {
233: oneMember = (GroupMembers) e.next();
234:
235: /* We check this in case it's been removed by the time
236:
237: /* we get to it by group nesting */
238: if (oneMember.find()) {
239: oneMember.delete();
240: }
241: } /* for each group member listing */
242:
243: }
244:
245: public void loggedOffUser(User user) throws DBException {
246: }
247:
248: public void loggedOnUser(User user) throws DBException {
249: }
250:
251: public void modifiedUser(User user) throws DBException {
252: }
253:
254: /**
255: * Checks to see if the current date is past the expiration date.
256: *
257: * @return true if this group membership has expired
258: */
259: public boolean hasExpired() {
260: String expireDateString;
261: Date expireDate;
262:
263: try {
264: expireDateString = StringUtil.notNull(this
265: .getField(EXPIRES));
266:
267: if (expireDateString.equals("")) {
268: return false;
269: }
270:
271: expireDate = this .getFieldDate(EXPIRES);
272: } catch (DBException e) {
273: log.error("Groupnest: Error getting Expiration Field");
274:
275: return false;
276: }
277:
278: Date now = new Date();
279:
280: if (now.after(expireDate)) {
281: return true;
282: } else {
283: return false;
284: }
285: }
286:
287: /**
288: * Set up the fields/tables for this object
289: */
290: protected synchronized void setupFields() throws DBException {
291: setTargetTable(TABLE);
292: setName(TABLE);
293: setDescription("DBgroupMembers");
294: setCharset("ISO-8859-1");
295: addField(GROUP_NAME, DBField.CHAR_TYPE, 10, false, "groupName");
296: addField(EXPIRES, "date", 0, true, "dateExpires");
297: addField(EXPUID, DBField.INTEGER_TYPE, 0, false, "userId");
298: addVirtualField("LoginName", DBField.CHAR_TYPE, 30, "loginName");
299: addKey(EXPUID);
300: addKey(GROUP_NAME);
301: setStringFilter(GROUP_NAME, "stripFilter");
302: setMultiValued(GROUP_NAME);
303: setLookupObject(GROUP_NAME, UserGroup.class.getName());
304: setLookupObject(EXPUID, User.class.getName());
305:
306: addIndex("groupname_idx", GROUP_NAME, false);
307: } /* setupFields() */
308:
309: /**
310: * Extend the normal getField method
311: *
312: * @param fieldName the name of the field to retrieve
313: * @return the value of the field
314: */
315: public String getField(String fieldName) throws DBException {
316: if (fieldName.equals("LoginName")) {
317: User u = new User();
318: String uidStr = getField(EXPUID);
319: if ((uidStr == null) || (uidStr.equals(""))) {
320: u.setUid(0);
321: } else {
322: u.setUid(getFieldInt(EXPUID));
323: }
324: u.setDataContext(this .getDataContext());
325:
326: if (u.find()) {
327: return u.getLoginName();
328: }
329:
330: return "No such user";
331: }
332:
333: return super .getField(fieldName);
334: } /* getField(String) */
335:
336: /**
337: * set group name; convenience method
338: * @param grpname name to set
339: */
340: public void setGroupName(String grpname) throws DBException {
341: setField(GROUP_NAME, grpname);
342: }
343:
344: /**
345: * set uid; convenience method
346: * @param uid uid to set
347: */
348: public void setUID(int uid) throws DBException {
349: setField(EXPUID, uid);
350: }
351: } /* GroupMembers */
|