1 64 65 package com.jcorporate.expresso.services.dbobj; 66 67 import com.jcorporate.expresso.core.controller.ControllerRequest; 68 import com.jcorporate.expresso.core.db.DBConnection; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.dbobj.DBField; 71 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 72 import com.jcorporate.expresso.core.misc.StringUtil; 73 import com.jcorporate.expresso.core.security.User; 74 import com.jcorporate.expresso.core.security.UserListener; 75 import org.apache.log4j.Logger; 76 77 import java.util.Date ; 78 import java.util.Iterator ; 79 80 81 90 public class GroupMembers 91 extends SecurityDBObject 92 implements UserListener { 93 94 95 private static int maxDepth = 20; 96 private static Logger log = Logger.getLogger(GroupMembers.class); 97 98 public static final String GROUP_NAME = "GroupName"; 99 public static final String EXPIRES = "Expires"; 100 public static final String EXPUID = "ExpUid"; 101 public static final String TABLE = "GROUPMEMBERS"; 102 103 104 107 108 static { 109 try { 110 User.addListener(new GroupMembers()); 111 } catch (Exception e) { 112 e.printStackTrace(); 113 System.err.println(e.getMessage()); 114 } 115 } 116 117 121 124 public GroupMembers() 125 throws DBException { 126 } 127 128 129 134 public GroupMembers(int uid) 135 throws DBException { 136 super(uid); 137 } 138 139 145 public GroupMembers(ControllerRequest request) 146 throws DBException { 147 super(request); 148 } 149 150 155 public GroupMembers(DBConnection localConnection) throws DBException { 156 if (localConnection != null) { 157 setConnection(localConnection); 158 } 159 } 160 161 164 public void add() 165 throws DBException { 166 add(0); 167 } 168 169 170 176 private void add(int depth) 177 throws DBException { 178 if (depth > maxDepth) { 179 log.warn("Tried to add security group nesting more than " + 180 maxDepth + " levels. Security was only added to " + 181 maxDepth + " levels."); 182 } 183 184 GroupNest gnl = new GroupNest(SecuredDBObject.SYSTEM_ACCOUNT); 185 gnl.setDataContext(getDataContext()); 186 187 GroupNest oneNest = null; 188 gnl.setField("GroupName", getField(GROUP_NAME)); 189 190 for (Iterator enl = gnl.searchAndRetrieveList().iterator(); 191 enl.hasNext();) { 192 oneNest = (GroupNest) enl.next(); 193 194 GroupMembers newMember = new GroupMembers(SecuredDBObject.SYSTEM_ACCOUNT); 195 newMember.setDataContext(getDataContext()); 196 197 198 199 200 newMember.clear(); 201 newMember.setField(EXPUID, getField(EXPUID)); 202 newMember.setField(GROUP_NAME, oneNest.getField("MemberOf")); 203 204 if (!newMember.find()) { 205 newMember.add(depth + 1); 206 } 207 } 208 209 210 super.add(); 211 } 212 213 214 public void addedUser(User user) 215 throws DBException { 216 } 217 218 223 protected void checkAllRefs() 224 throws DBException { 225 checkRef(GROUP_NAME, new UserGroup(), 226 "Invalid " + getString(getMetaData().getDescription(GROUP_NAME))); 227 } 228 229 230 236 public void deletedUser(User user) 237 throws DBException { 238 GroupMembers gml = new GroupMembers(); 239 gml.setDataContext(user.getDataContext()); 240 gml.setField(EXPUID, user.getUid()); 241 242 GroupMembers oneMember = null; 243 244 for (Iterator e = gml.searchAndRetrieveList().iterator(); e.hasNext();) { 245 oneMember = (GroupMembers) e.next(); 246 247 250 if (oneMember.find()) { 251 oneMember.delete(); 252 } 253 } 254 255 } 256 257 public void loggedOffUser(User user) 258 throws DBException { 259 } 260 261 public void loggedOnUser(User user) 262 throws DBException { 263 } 264 265 public void modifiedUser(User user) 266 throws DBException { 267 } 268 269 274 public boolean hasExpired() { 275 String expireDateString; 276 Date expireDate; 277 278 try { 279 expireDateString = StringUtil.notNull(this.getField(EXPIRES)); 280 281 if (expireDateString.equals("")) { 282 return false; 283 } 284 285 expireDate = this.getFieldDate(EXPIRES); 286 } catch (DBException e) { 287 log.error("Groupnest: Error getting Expiration Field"); 288 289 return false; 290 } 291 292 Date now = new Date (); 293 294 if (now.after(expireDate)) { 295 return true; 296 } else { 297 return false; 298 } 299 } 300 301 304 protected synchronized void setupFields() 305 throws DBException { 306 setTargetTable(TABLE); 307 setName(TABLE); 308 setDescription("DBgroupMembers"); 309 setCharset("ISO-8859-1"); 310 addField(GROUP_NAME, DBField.CHAR_TYPE, 10, false, "groupName"); 311 addField(EXPIRES, "date", 0, true, "dateExpires"); 312 addField(EXPUID, DBField.INTEGER_TYPE, 0, false, "userId"); 313 addVirtualField("LoginName", DBField.CHAR_TYPE, 30, "loginName"); 314 addKey(EXPUID); 315 addKey(GROUP_NAME); 316 setStringFilter(GROUP_NAME, "stripFilter"); 317 setMultiValued(GROUP_NAME); 318 setLookupObject(GROUP_NAME, UserGroup.class.getName()); 319 setLookupObject(EXPUID, User.class.getName()); 320 321 addIndex("groupname_idx", GROUP_NAME, false); 322 } 323 324 325 331 public String getField(String fieldName) 332 throws DBException { 333 if (fieldName.equals("LoginName")) { 334 User u = new User(); 335 String uidStr = getField(EXPUID); 336 if ((uidStr == null) || (uidStr.equals(""))) { 337 u.setUid(0); 338 } else { 339 u.setUid(getFieldInt(EXPUID)); 340 } 341 u.setDataContext(this.getDataContext()); 342 343 if (u.find()) { 344 return u.getLoginName(); 345 } 346 347 return "No such user"; 348 } 349 350 return super.getField(fieldName); 351 } 352 353 354 358 public void setGroupName(String grpname) throws DBException { 359 setField(GROUP_NAME, grpname); 360 } 361 362 366 public void setUID(int uid) throws DBException { 367 setField(EXPUID, uid); 368 } 369 } | Popular Tags |