0001: package org.enhydra.shark.usergroup;
0002:
0003: import java.util.ArrayList;
0004: import java.util.Arrays;
0005: import java.util.List;
0006:
0007: import org.enhydra.dods.DODS;
0008: import org.enhydra.shark.api.admin.UserGroupManagerAdmin;
0009: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
0010: import org.enhydra.shark.api.internal.working.CallbackUtilities;
0011: import org.enhydra.shark.usergroup.data.GroupDO;
0012: import org.enhydra.shark.usergroup.data.GroupLinkDO;
0013: import org.enhydra.shark.usergroup.data.GroupLinkQuery;
0014: import org.enhydra.shark.usergroup.data.GroupQuery;
0015: import org.enhydra.shark.usergroup.data.UserDO;
0016: import org.enhydra.shark.usergroup.data.UserLinkDO;
0017: import org.enhydra.shark.usergroup.data.UserLinkQuery;
0018: import org.enhydra.shark.usergroup.data.UserQuery;
0019: import org.enhydra.shark.utilities.MiscUtilities;
0020: import org.enhydra.shark.utilities.dods.DODSUtilities;
0021:
0022: import com.lutris.appserver.server.sql.DBTransaction;
0023: import com.lutris.appserver.server.sql.DatabaseManagerException;
0024: import com.lutris.appserver.server.sql.LogicalDatabase;
0025:
0026: /**
0027: * Used for managing users, groups and their relations.
0028: *
0029: * @author Sasa Bojanic, Vladimir Puskas, Tanja Jovanovic
0030: * @version 0.1
0031: */
0032: public class DODSUserGroupManagerAdmin implements UserGroupManagerAdmin {
0033:
0034: private static final String LDB_PARAM_NAME = "DODSUserGroupManagerAdmin.DatabaseName";
0035:
0036: private static LogicalDatabase db;
0037:
0038: private static String digestAlgorithm = "SHA-1";
0039:
0040: /**
0041: * Method configure is called at Shark start up, to configure implementation of
0042: * DODSUserGroupManager.
0043: *
0044: * @param cus an instance of CallbackUtilities used to get properties for configuring
0045: * user group manager in Shark.
0046: * @exception Exception thrown if configuring doesn't succeed.
0047: */
0048: public void configure(CallbackUtilities cus) throws Exception {
0049: if (null == cus) {
0050: throw new Exception(
0051: "Cannot configure without call back impl.");
0052: }
0053: DODSUtilities.init(cus.getProperties());
0054: String dbName = cus.getProperty(LDB_PARAM_NAME, DODS
0055: .getDatabaseManager().getDefaultDB());
0056: try {
0057: db = DODS.getDatabaseManager().findLogicalDatabase(dbName);
0058:
0059: } catch (Exception e) {
0060: cus.error(null, "Couldn't find logical database " + dbName,
0061: e);
0062: throw e;
0063: }
0064: setLDBName();
0065: cus.debug(null, "DODSUserGroupManagerAdmin configured");
0066: }
0067:
0068: /**
0069: * Returns Ids of all user groups.
0070: *
0071: * @return List of user group Ids.
0072: * @throws Exception If something unexpected happens.
0073: */
0074: public String[] getAllGroupnames(WMSessionHandle shandle)
0075: throws Exception {
0076: List ret = new ArrayList();
0077:
0078: DBTransaction dbt = getDBTransaction();
0079:
0080: GroupQuery qry = new GroupQuery(dbt);
0081:
0082: while (true) {
0083: GroupDO group = qry.getNextDO();
0084: if (null == group)
0085: break;
0086: ret.add(group.getGroupid());
0087: }
0088:
0089: String[] retArr = new String[ret.size()];
0090: ret.toArray(retArr);
0091: return retArr;
0092: }
0093:
0094: /**
0095: * Returns Ids of all groups for the given user.
0096: *
0097: * @param userName name of the given user.
0098: * @return List of all group ids for the given user.
0099: * @throws Exception If something unexpected happens.
0100: */
0101: public String[] getAllGroupnamesForUser(WMSessionHandle shandle,
0102: String userName) throws Exception {
0103:
0104: List ret = new ArrayList();
0105:
0106: DBTransaction dbt = getDBTransaction();
0107:
0108: UserQuery uqry = new UserQuery(dbt);
0109:
0110: uqry.setQueryUserid(userName);
0111: uqry.requireUniqueInstance();
0112: UserDO ud = uqry.getNextDO();
0113:
0114: if (ud == null)
0115: throw new Exception("User " + userName + "does not exist.");
0116:
0117: UserLinkQuery lqry = new UserLinkQuery(dbt);
0118: lqry.setQueryUserid(ud);
0119: while (true) {
0120: UserLinkDO group = lqry.getNextDO();
0121: if (null == group) {
0122: break;
0123: }
0124: ret.add(group.getGroupid().getGroupid());
0125: }
0126: String[] retArr = new String[ret.size()];
0127: ret.toArray(retArr);
0128: return retArr;
0129: }
0130:
0131: /**
0132: * Returns Ids of all users.
0133: *
0134: * @return List of user Ids.
0135: * @throws Exception If something unexpected happens.
0136: */
0137: public String[] getAllUsers(WMSessionHandle shandle)
0138: throws Exception {
0139: List ret = new ArrayList();
0140:
0141: DBTransaction dbt = getDBTransaction();
0142:
0143: UserQuery qry = new UserQuery(dbt);
0144: while (true) {
0145: UserDO user = qry.getNextDO();
0146: if (null == user) {
0147: break;
0148: }
0149: ret.add(user.getUserid());
0150: }
0151:
0152: String[] retArr = new String[ret.size()];
0153: ret.toArray(retArr);
0154: return retArr;
0155: }
0156:
0157: /**
0158: * Returns all usernames that belong to the given group.
0159: *
0160: * @param groupName name of the given group.
0161: * @return List of all usernames that belong to given group.
0162: * @throws Exception If something unexpected happens.
0163: */
0164: protected String[] getAllUsersForGroup(WMSessionHandle shandle,
0165: String groupName) throws Exception {
0166: List ret = new ArrayList();
0167:
0168: DBTransaction dbt = getDBTransaction();
0169:
0170: GroupQuery gqry = new GroupQuery(dbt);
0171:
0172: gqry.setQueryGroupid(groupName);
0173: gqry.requireUniqueInstance();
0174: GroupDO gd = gqry.getNextDO();
0175:
0176: if (gd == null)
0177: throw new Exception("Group " + groupName
0178: + "does not exist.");
0179:
0180: UserLinkQuery lqry = new UserLinkQuery(dbt);
0181: lqry.setQueryGroupid(gd);
0182: while (true) {
0183: UserLinkDO user = lqry.getNextDO();
0184: if (null == user) {
0185: break;
0186: }
0187: ret.add(user.getUserid().getUserid());
0188: }
0189:
0190: GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
0191: groupLinkQ.setQueryGroupid(gd);
0192: while (true) {
0193: GroupLinkDO subGroup = groupLinkQ.getNextDO();
0194: if (null == subGroup) {
0195: break;
0196: }
0197: List l = Arrays.asList(getAllUsersForGroup(shandle,
0198: subGroup.getSub_gid().getGroupid()));
0199: if (!l.isEmpty()) {
0200: for (int i = 0; i < l.size(); i++) {
0201: if (!ret.contains(l.get(i))) {
0202: ret.add(l.get(i));
0203: }
0204: }
0205: }
0206: }
0207:
0208: String[] retArr = new String[ret.size()];
0209: ret.toArray(retArr);
0210: return retArr;
0211: }
0212:
0213: /**
0214: * Returns all users that belong to the given groups.
0215: *
0216: * @param groupNames names of the given groups.
0217: * @return List of all users that belong to given groups.
0218: * @throws Exception If something unexpected happens.
0219: */
0220: public String[] getAllUsersForGroups(WMSessionHandle shandle,
0221: String[] groupNames) throws Exception {
0222: List ret = new ArrayList();
0223: for (int i = 0; i < groupNames.length; i++) {
0224: ret.addAll(Arrays.asList(getAllUsersForGroup(shandle,
0225: groupNames[i])));
0226: }
0227: String[] retArr = new String[ret.size()];
0228: ret.toArray(retArr);
0229: return retArr;
0230: }
0231:
0232: /**
0233: * Returns all users that are immediate children of the given group.
0234: *
0235: * @param groupName name of the given group.
0236: * @return List of all immediate (direct) users that belong to the given group.
0237: * @throws Exception If something unexpected happens.
0238: */
0239: public String[] getAllImmediateUsersForGroup(
0240: WMSessionHandle shandle, String groupName) throws Exception {
0241: List ret = new ArrayList();
0242:
0243: DBTransaction dbt = getDBTransaction();
0244:
0245: GroupQuery gqry = new GroupQuery(dbt);
0246:
0247: gqry.setQueryGroupid(groupName);
0248: gqry.requireUniqueInstance();
0249: GroupDO gd = gqry.getNextDO();
0250:
0251: if (gd == null)
0252: throw new Exception("Group " + groupName
0253: + "does not exist.");
0254:
0255: UserLinkQuery lqry = new UserLinkQuery(dbt);
0256: lqry.setQueryGroupid(gd);
0257: while (true) {
0258: UserLinkDO user = lqry.getNextDO();
0259: if (null == user) {
0260: break;
0261: }
0262: ret.add(user.getUserid().getUserid());
0263: }
0264:
0265: String[] retArr = new String[ret.size()];
0266: ret.toArray(retArr);
0267: return retArr;
0268: }
0269:
0270: /**
0271: * Returns all groups that belong to the given group.
0272: *
0273: * @param groupName name of the given group.
0274: * @return List of all groups that belong to the given group.
0275: * @throws Exception If something unexpected happens.
0276: */
0277: protected String[] getAllSubgroupsForGroup(WMSessionHandle shandle,
0278: String groupName) throws Exception {
0279: List ret = new ArrayList();
0280:
0281: DBTransaction dbt = getDBTransaction();
0282:
0283: GroupQuery gqry = new GroupQuery(dbt);
0284:
0285: gqry.setQueryGroupid(groupName);
0286: gqry.requireUniqueInstance();
0287: GroupDO gd = gqry.getNextDO();
0288:
0289: if (gd == null)
0290: throw new Exception("Group " + groupName
0291: + "does not exist.");
0292:
0293: GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
0294: groupLinkQ.setQueryGroupid(gd);
0295: while (true) {
0296: GroupLinkDO subGroup = groupLinkQ.getNextDO();
0297: if (null == subGroup) {
0298: break;
0299: }
0300: String subId = subGroup.getSub_gid().getGroupid();
0301: if (!ret.contains(subId)) {
0302: ret.add(subId);
0303: List l = Arrays.asList(getAllSubgroupsForGroup(shandle,
0304: subGroup.getSub_gid().getGroupid()));
0305: if (!l.isEmpty()) {
0306: for (int i = 0; i < l.size(); i++) {
0307: if (!ret.contains(l.get(i))) {
0308: ret.add(l.get(i));
0309: }
0310: }
0311: }
0312: }
0313: }
0314:
0315: String[] retArr = new String[ret.size()];
0316: ret.toArray(retArr);
0317: return retArr;
0318: }
0319:
0320: /**
0321: * Returns all groups that belong to the given groups.
0322: *
0323: * @param groupNames names of the given groups.
0324: * @return List of all groups that belong to the given groups.
0325: * @throws Exception If something unexpected happens.
0326: */
0327: public String[] getAllSubgroupsForGroups(WMSessionHandle shandle,
0328: String[] groupNames) throws Exception {
0329: List ret = new ArrayList();
0330: for (int i = 0; i < groupNames.length; i++) {
0331: ret.addAll(Arrays.asList(getAllSubgroupsForGroup(shandle,
0332: groupNames[i])));
0333: }
0334: String[] retArr = new String[ret.size()];
0335: ret.toArray(retArr);
0336: return retArr;
0337: }
0338:
0339: /**
0340: * Returns all groups that are immediate children of the given group (which are on the
0341: * first level bellow the level of the given group).
0342: *
0343: * @param groupName name of the given group.
0344: * @return List of all groups that are immediate children of the given group.
0345: * @throws Exception If something unexpected happens.
0346: */
0347: public String[] getAllImmediateSubgroupsForGroup(
0348: WMSessionHandle shandle, String groupName) throws Exception {
0349:
0350: List ret = new ArrayList();
0351:
0352: DBTransaction dbt = getDBTransaction();
0353:
0354: GroupQuery gqry = new GroupQuery(dbt);
0355:
0356: gqry.setQueryGroupid(groupName);
0357: gqry.requireUniqueInstance();
0358: GroupDO gd = gqry.getNextDO();
0359:
0360: if (gd == null)
0361: throw new Exception("Group " + groupName
0362: + "does not exist.");
0363:
0364: GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
0365: groupLinkQ.setQueryGroupid(gd);
0366: while (true) {
0367: GroupLinkDO subGroup = groupLinkQ.getNextDO();
0368: if (null == subGroup) {
0369: break;
0370: }
0371: ret.add(subGroup.getSub_gid().getGroupid());
0372: }
0373:
0374: String[] retArr = new String[ret.size()];
0375: ret.toArray(retArr);
0376: return retArr;
0377: }
0378:
0379: /**
0380: * Returns true if user group with given name exists.
0381: *
0382: * @param groupName name of the given group.
0383: * @return true if user group exists, otherwise false.
0384: * @throws Exception If something unexpected happens.
0385: */
0386: public boolean doesGroupExist(WMSessionHandle shandle,
0387: String groupName) throws Exception {
0388: return doesGroupExist(groupName);
0389: }
0390:
0391: /**
0392: * Returns true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>.
0393: *
0394: * @param groupName name of the given group.
0395: * @param subgroupName name of the given subgroup.
0396: * @return true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>,
0397: * otherwise false.
0398: * @throws Exception If something unexpected happens.
0399: */
0400: public boolean doesGroupBelongToGroup(WMSessionHandle shandle,
0401: String groupName, String subgroupName) throws Exception {
0402: boolean ret = false;
0403:
0404: DBTransaction dbt = getDBTransaction();
0405:
0406: GroupQuery gQry = new GroupQuery(dbt);
0407: gQry.setQueryGroupid(groupName);
0408: gQry.requireUniqueInstance();
0409: GroupDO group = gQry.getNextDO();
0410:
0411: if (group == null)
0412: throw new Exception("Group " + groupName
0413: + "does not exist.");
0414:
0415: GroupQuery gQry1 = new GroupQuery(dbt);
0416: gQry1.setQueryGroupid(subgroupName);
0417: gQry1.requireUniqueInstance();
0418: GroupDO subgroup = gQry1.getNextDO();
0419:
0420: if (subgroup == null)
0421: throw new Exception("Group " + subgroupName
0422: + "does not exist.");
0423:
0424: GroupLinkQuery lQry = new GroupLinkQuery(dbt);
0425: lQry.setQueryGroupid(group);
0426: lQry.setQuerySub_gid(subgroup);
0427: lQry.requireUniqueInstance();
0428: GroupLinkDO result = lQry.getNextDO();
0429:
0430: if (result != null)
0431: ret = true;
0432: else {
0433: GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
0434: lQry1.setQueryGroupid(group);
0435: result = lQry1.getNextDO();
0436: while ((!ret) && (result != null)) {
0437: ret = doesGroupBelongToGroup(shandle, result
0438: .getSub_gid(), subgroup);
0439: result = lQry1.getNextDO();
0440: }
0441: }
0442:
0443: return ret;
0444: }
0445:
0446: private boolean doesGroupBelongToGroup(WMSessionHandle shandle,
0447: GroupDO group, GroupDO subgroup) throws Exception {
0448: boolean ret = false;
0449:
0450: DBTransaction dbt = getDBTransaction();
0451:
0452: GroupLinkQuery lQry = new GroupLinkQuery(dbt);
0453: lQry.setQueryGroupid(group);
0454: lQry.setQuerySub_gid(subgroup);
0455: lQry.requireUniqueInstance();
0456: GroupLinkDO result = lQry.getNextDO();
0457:
0458: if (result != null)
0459: ret = true;
0460: else {
0461: GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
0462: lQry1.setQueryGroupid(group);
0463: result = lQry1.getNextDO();
0464: while ((!ret) && (result != null)) {
0465: ret = doesGroupBelongToGroup(shandle, result
0466: .getSub_gid(), subgroup);
0467: result = lQry1.getNextDO();
0468: }
0469: }
0470:
0471: return ret;
0472: }
0473:
0474: /**
0475: * Returns a group description.
0476: *
0477: * @param groupName name of the given group.
0478: * @return Group description.
0479: * @throws Exception If something unexpected happens.
0480: */
0481: public String getGroupDescription(WMSessionHandle shandle,
0482: String groupName) throws Exception {
0483: String ret = null;
0484:
0485: DBTransaction dbt = getDBTransaction();
0486:
0487: GroupQuery qry = new GroupQuery(dbt);
0488: qry.setQueryGroupid(groupName);
0489: qry.requireUniqueInstance();
0490: ret = qry.getNextDO().getDescription();
0491: return ret;
0492: }
0493:
0494: /**
0495: * Returns true if the given user belongs to the given group.
0496: *
0497: * @param groupName name of the given group.
0498: * @param username username used to uniquely identify shark user.
0499: * @return true if the given user belongs to the given group, otherwise false.
0500: * @throws Exception If something unexpected happens.
0501: */
0502: public boolean doesUserBelongToGroup(WMSessionHandle shandle,
0503: String groupName, String username) throws Exception {
0504: boolean ret = false;
0505:
0506: DBTransaction dbt = getDBTransaction();
0507:
0508: GroupQuery gQry = new GroupQuery(dbt);
0509: gQry.setQueryGroupid(groupName);
0510: gQry.requireUniqueInstance();
0511: GroupDO group = gQry.getNextDO();
0512:
0513: if (group == null)
0514: throw new Exception("Group " + groupName
0515: + "does not exist.");
0516:
0517: UserQuery uQry = new UserQuery(dbt);
0518: uQry.setQueryUserid(username);
0519: uQry.requireUniqueInstance();
0520: UserDO user = uQry.getNextDO();
0521:
0522: if (user == null)
0523: throw new Exception("User " + username + "does not exist.");
0524:
0525: UserLinkQuery lQry = new UserLinkQuery(dbt);
0526: lQry.setQueryUserid(user);
0527: lQry.setQueryGroupid(group);
0528: lQry.requireUniqueInstance();
0529: // ret = null != lQry.getNextDO();
0530: UserLinkDO result = lQry.getNextDO();
0531:
0532: if (result != null)
0533: ret = true;
0534: else {
0535: GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
0536: lQry1.setQueryGroupid(group);
0537: GroupLinkDO result1 = lQry1.getNextDO();
0538: while ((!ret) && (result1 != null)) {
0539: ret = doesUserBelongToGroup(result1.getSub_gid(), user);
0540: result1 = lQry1.getNextDO();
0541: }
0542: }
0543:
0544: return ret;
0545: }
0546:
0547: /**
0548: * Returns true if user with given username exists.
0549: *
0550: * @param username username of the shark user.
0551: * @return true if the user with the given username exists, otherwise false.
0552: * @throws Exception If something unexpected happens.
0553: */
0554: public boolean doesUserExist(WMSessionHandle shandle,
0555: String username) throws Exception {
0556: return doesUserExist(username);
0557: }
0558:
0559: public String getUserPassword(WMSessionHandle shandle,
0560: String username) throws Exception {
0561: DBTransaction dbt = getDBTransaction();
0562:
0563: UserQuery uQry = new UserQuery(dbt);
0564: uQry.setQueryUserid(username);
0565: uQry.requireUniqueInstance();
0566: UserDO resultDO = uQry.getNextDO();
0567: return resultDO.getPasswd();
0568: }
0569:
0570: /**
0571: * Returns string representing the real name for the shark user with the given username
0572: * (first and last name).
0573: *
0574: * @param username username of the shark user.
0575: * @return Real name of the shark user.
0576: * @throws Exception If something unexpected happens.
0577: */
0578: public String getUserRealName(WMSessionHandle shandle,
0579: String username) throws Exception {
0580: String ret = null;
0581:
0582: DBTransaction dbt = getDBTransaction();
0583:
0584: UserQuery uQry = new UserQuery(dbt);
0585: uQry.setQueryUserid(username);
0586: uQry.requireUniqueInstance();
0587: UserDO resultDO = uQry.getNextDO();
0588: ret = resultDO.getFirstname();
0589: String lastName = resultDO.getLastname();
0590: if (ret != null) {
0591: if (lastName != null)
0592: ret = ret + " " + lastName;
0593: } else {
0594: if (lastName != null)
0595: ret = lastName;
0596: }
0597:
0598: return ret;
0599: }
0600:
0601: /**
0602: * Returns string representing user's first name.
0603: *
0604: * @param username username of the shark user.
0605: * @return First name of the shark user.
0606: * @throws Exception If something unexpected happens.
0607: */
0608: public String getUserFirstName(WMSessionHandle shandle,
0609: String username) throws Exception {
0610: String ret = null;
0611:
0612: DBTransaction dbt = getDBTransaction();
0613:
0614: UserQuery uQry = new UserQuery(dbt);
0615: uQry.setQueryUserid(username);
0616: uQry.requireUniqueInstance();
0617: ret = uQry.getNextDO().getFirstname();
0618:
0619: return ret;
0620: }
0621:
0622: /**
0623: * Returns string representing user's last name.
0624: *
0625: * @param username username of the shark user.
0626: * @return Last name of the shark user.
0627: * @throws Exception If something unexpected happens.
0628: */
0629: public String getUserLastName(WMSessionHandle shandle,
0630: String username) throws Exception {
0631: String ret = null;
0632:
0633: DBTransaction dbt = getDBTransaction();
0634:
0635: UserQuery uQry = new UserQuery(dbt);
0636: uQry.setQueryUserid(username);
0637: uQry.requireUniqueInstance();
0638: ret = uQry.getNextDO().getLastname();
0639:
0640: return ret;
0641: }
0642:
0643: /**
0644: * Returns string representing email address for the user with the given username.
0645: *
0646: * @param username username of the shark user.
0647: * @return Email of the shark user.
0648: * @throws Exception If something unexpected happens.
0649: */
0650: public String getUserEMailAddress(WMSessionHandle shandle,
0651: String username) throws Exception {
0652: String ret = null;
0653:
0654: DBTransaction dbt = getDBTransaction();
0655:
0656: UserQuery uQry = new UserQuery(dbt);
0657: uQry.setQueryUserid(username);
0658: uQry.requireUniqueInstance();
0659: ret = uQry.getNextDO().getEmail();
0660:
0661: return ret;
0662: }
0663:
0664: /**
0665: * Creates a new user group.
0666: *
0667: * @param groupName name of the given group.
0668: * @param description group description.
0669: * @throws Exception If something unexpected happens.
0670: */
0671: public void createGroup(String groupName, String description)
0672: throws Exception {
0673:
0674: if (doesGroupExist(groupName)) {
0675: throw new Exception("Group already exists");
0676: }
0677:
0678: DBTransaction dbt = getDBTransaction();
0679:
0680: GroupDO group = GroupDO.createVirgin(dbt);
0681: group.setGroupid(groupName);
0682: group.setDescription(description);
0683: group.save();
0684: }
0685:
0686: /**
0687: * Removes user group.
0688: *
0689: * @param groupName name of the given group.
0690: * @throws Exception If something unexpected happens.
0691: */
0692: public void removeGroup(String groupName) throws Exception {
0693:
0694: DBTransaction dbt = getDBTransaction();
0695:
0696: GroupQuery qry = new GroupQuery(dbt);
0697: qry.setQueryGroupid(groupName);
0698: qry.requireUniqueInstance();
0699: qry.getNextDO().delete();
0700: }
0701:
0702: /**
0703: * Allows administrator to update data about group.
0704: *
0705: * @param groupName name of the given group.
0706: * @param description group description.
0707: * @throws Exception If something unexpected happens.
0708: */
0709: public void updateGroup(String groupName, String description)
0710: throws Exception {
0711:
0712: DBTransaction dbt = getDBTransaction();
0713:
0714: GroupQuery qry = new GroupQuery(dbt);
0715: qry.setQueryGroupid(groupName);
0716: qry.requireUniqueInstance();
0717: GroupDO group = qry.getNextDO();
0718:
0719: if (group == null)
0720: throw new Exception("Group " + groupName
0721: + "does not exist.");
0722:
0723: group.setDescription(description);
0724: group.save();
0725: }
0726:
0727: /**
0728: * Adds an existing group <i>subgroupName</i> to the group <i>groupName</i>.
0729: *
0730: * @param groupName name of the given group.
0731: * @param subgroupName name of the given subgroup to be added.
0732: * @throws Exception If something unexpected happens.
0733: */
0734: public void addGroupToGroup(String groupName, String subgroupName)
0735: throws Exception {
0736:
0737: DBTransaction dbt = getDBTransaction();
0738:
0739: GroupQuery gQry = new GroupQuery(dbt);
0740: gQry.setQueryGroupid(groupName);
0741: gQry.requireUniqueInstance();
0742: GroupDO group = gQry.getNextDO();
0743:
0744: if (group == null)
0745: throw new Exception("Group " + groupName
0746: + "does not exist.");
0747:
0748: GroupQuery gQry1 = new GroupQuery(dbt);
0749: gQry1.setQueryGroupid(subgroupName);
0750: gQry1.requireUniqueInstance();
0751: GroupDO subgroup = gQry1.getNextDO();
0752:
0753: if (subgroup == null)
0754: throw new Exception("Group " + subgroupName
0755: + "does not exist.");
0756:
0757: GroupLinkDO GroupLink = GroupLinkDO.createVirgin(dbt);
0758: GroupLink.setSub_gid(subgroup);
0759: GroupLink.setGroupid(group);
0760: GroupLink.save();
0761: }
0762:
0763: /**
0764: * Removes group <i>subgroupName</i> from the group <i>groupName</i>.
0765: *
0766: * @param groupName name of the given group.
0767: * @param subgroupName name of the given subgroup to be removed.
0768: * @throws Exception If something unexpected happens.
0769: */
0770: public void removeGroupFromGroup(String groupName,
0771: String subgroupName) throws Exception {
0772:
0773: DBTransaction dbt = getDBTransaction();
0774:
0775: GroupQuery gQry = new GroupQuery(dbt);
0776: gQry.setQueryGroupid(groupName);
0777: gQry.requireUniqueInstance();
0778: GroupDO group = gQry.getNextDO();
0779:
0780: if (group == null)
0781: throw new Exception("Group " + groupName
0782: + "does not exist.");
0783:
0784: GroupQuery gQry1 = new GroupQuery(dbt);
0785: gQry1.setQueryGroupid(subgroupName);
0786: gQry1.requireUniqueInstance();
0787: GroupDO subgroup = gQry1.getNextDO();
0788:
0789: if (subgroup == null)
0790: throw new Exception("Group " + subgroupName
0791: + "does not exist.");
0792:
0793: GroupLinkQuery lQry = new GroupLinkQuery(dbt);
0794: lQry.setQuerySub_gid(subgroup);
0795: lQry.setQueryGroupid(group);
0796: lQry.requireUniqueInstance();
0797: lQry.getNextDO().delete();
0798: }
0799:
0800: /**
0801: * Deletes group <i>groupName</i> and all its child groups that don't belong to any
0802: * other group except this one.
0803: *
0804: * @param groupName name of the given group.
0805: * @throws Exception If something unexpected happens.
0806: */
0807: public void removeGroupTree(String groupName) throws Exception {
0808:
0809: DBTransaction dbt = getDBTransaction();
0810:
0811: GroupQuery gQry = new GroupQuery(dbt);
0812: gQry.setQueryGroupid(groupName);
0813: gQry.requireUniqueInstance();
0814: GroupDO group = gQry.getNextDO();
0815:
0816: if (group == null)
0817: throw new Exception("Group " + groupName
0818: + "does not exist.");
0819:
0820: GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
0821: groupLinkQ.setQueryGroupid(group);
0822:
0823: while (true) {
0824: GroupLinkDO groupLinkDo = groupLinkQ.getNextDO();
0825: if (null == groupLinkDo) {
0826: break;
0827: }
0828: removeChildTree(groupLinkDo);
0829: }
0830: removeUsersFromGroup(group);
0831: group.delete();
0832: }
0833:
0834: private void removeChildTree(GroupLinkDO groupLink)
0835: throws Exception {
0836:
0837: DBTransaction dbt = getDBTransaction();
0838:
0839: GroupDO group = groupLink.getSub_gid();
0840:
0841: GroupLinkQuery gQry = new GroupLinkQuery(dbt);
0842: gQry.setQueryGroupid(group);
0843: GroupLinkDO subgroupLink = gQry.getNextDO();
0844:
0845: while (subgroupLink != null) {
0846: removeChildTree(subgroupLink);
0847: subgroupLink = gQry.getNextDO();
0848: }
0849:
0850: GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
0851: groupLinkQ.setQuerySub_gid(group);
0852:
0853: if (groupLinkQ.getCount() > 1) {
0854: groupLink.delete();
0855: } else {
0856: removeUsersFromGroup(group);
0857: group.delete();
0858: }
0859: }
0860:
0861: private void removeUsersFromGroup(GroupDO group) throws Exception {
0862:
0863: DBTransaction dbt = getDBTransaction();
0864:
0865: UserLinkQuery userLinkQ = new UserLinkQuery(dbt);
0866: userLinkQ.setQueryGroupid(group);
0867:
0868: UserLinkDO[] userLink_dos = userLinkQ.getDOArray();
0869: for (int i = 0; i < userLink_dos.length; i++) {
0870: UserLinkDO userLink_do = userLink_dos[i];
0871: UserDO user_do = userLink_do.getUserid();
0872:
0873: UserLinkQuery lQry = new UserLinkQuery(dbt);
0874: lQry.setQueryUserid(user_do);
0875:
0876: if (lQry.getCount() == 1) {
0877: user_do.delete();
0878: } else {
0879: userLink_do.delete();
0880: }
0881: }
0882: }
0883:
0884: /**
0885: * Removes all users from group <i>group</i> that don't belong to any other group
0886: * except this one.
0887: *
0888: * @param groupName name of the given group.
0889: * @throws Exception If something unexpected happens.
0890: */
0891: public void removeUsersFromGroupTree(String groupName)
0892: throws Exception {
0893:
0894: DBTransaction dbt = getDBTransaction();
0895:
0896: GroupQuery gQry = new GroupQuery(dbt);
0897: gQry.setQueryGroupid(groupName);
0898: gQry.requireUniqueInstance();
0899: GroupDO group = gQry.getNextDO();
0900:
0901: if (group == null)
0902: throw new Exception("Group " + groupName
0903: + "does not exist.");
0904:
0905: GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
0906: groupLinkQ.setQueryGroupid(group);
0907:
0908: while (true) {
0909: GroupLinkDO groupLink_do = groupLinkQ.getNextDO();
0910: if (null == groupLink_do) {
0911: break;
0912: }
0913: removeUsersFromGroupTree(groupLink_do.getSub_gid());
0914: }
0915:
0916: removeUsersFromGroup(group);
0917: }
0918:
0919: private void removeUsersFromGroupTree(GroupDO group)
0920: throws Exception {
0921:
0922: DBTransaction dbt = getDBTransaction();
0923:
0924: GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
0925: groupLinkQ.setQueryGroupid(group);
0926:
0927: while (true) {
0928: GroupLinkDO groupLink_do = groupLinkQ.getNextDO();
0929: if (null == groupLink_do) {
0930: break;
0931: }
0932: removeUsersFromGroupTree(groupLink_do.getSub_gid());
0933: }
0934: removeUsersFromGroup(group);
0935: }
0936:
0937: /**
0938: * Moves group <i>subgroupName</i> from the group <i>currentParentGroup</i> to group
0939: * <i>newParentGroup</i>.
0940: *
0941: * @param currentParentGroup current group that contains group subgroupName.
0942: * @param newParentGroup new group where group subgroupName will be moved to.
0943: * @param subgroupName subgroup that will be moved.
0944: * @throws Exception If something unexpected happens.
0945: */
0946: public void moveGroup(String currentParentGroup,
0947: String newParentGroup, String subgroupName)
0948: throws Exception {
0949:
0950: DBTransaction dbt = getDBTransaction();
0951:
0952: GroupQuery gQry = new GroupQuery(dbt);
0953: gQry.setQueryGroupid(subgroupName);
0954: gQry.requireUniqueInstance();
0955: GroupDO subgroup = gQry.getNextDO();
0956:
0957: if (subgroup == null)
0958: throw new Exception("Group " + subgroupName
0959: + "does not exist.");
0960:
0961: GroupQuery gQry1 = new GroupQuery(dbt);
0962: gQry1.setQueryGroupid(currentParentGroup);
0963: gQry1.requireUniqueInstance();
0964: GroupDO currentGroup = gQry1.getNextDO();
0965:
0966: if (currentGroup == null)
0967: throw new Exception("Group " + currentParentGroup
0968: + "does not exist.");
0969:
0970: GroupQuery gQry2 = new GroupQuery(dbt);
0971: gQry2.setQueryGroupid(newParentGroup);
0972: gQry2.requireUniqueInstance();
0973: GroupDO newGroup = gQry2.getNextDO();
0974:
0975: if (newGroup == null)
0976: throw new Exception("Group " + newParentGroup
0977: + "does not exist.");
0978:
0979: GroupLinkQuery lQry = new GroupLinkQuery(dbt);
0980: lQry.setQuerySub_gid(subgroup);
0981: lQry.setQueryGroupid(currentGroup);
0982: lQry.requireUniqueInstance();
0983: lQry.getNextDO().delete();
0984:
0985: GroupLinkDO GroupLink = GroupLinkDO.createVirgin(dbt);
0986: GroupLink.setSub_gid(subgroup);
0987: GroupLink.setGroupid(newGroup);
0988: GroupLink.save();
0989: }
0990:
0991: /**
0992: * Adds an existing user with a given username to the given group.
0993: *
0994: * @param groupName name of the given group.
0995: * @param username username used to uniquely identify shark user.
0996: * @throws Exception If something unexpected happens.
0997: */
0998: public void addUserToGroup(String groupName, String username)
0999: throws Exception {
1000:
1001: DBTransaction dbt = getDBTransaction();
1002:
1003: GroupQuery gQry = new GroupQuery(dbt);
1004: gQry.setQueryGroupid(groupName);
1005: gQry.requireUniqueInstance();
1006: GroupDO group = gQry.getNextDO();
1007:
1008: if (group == null)
1009: throw new Exception("Group " + groupName
1010: + "does not exist.");
1011:
1012: UserQuery uQry = new UserQuery(dbt);
1013: uQry.setQueryUserid(username);
1014: uQry.requireUniqueInstance();
1015: UserDO user = uQry.getNextDO();
1016:
1017: if (user == null)
1018: throw new Exception("User " + username + "does not exist.");
1019:
1020: UserLinkDO UserLink = UserLinkDO.createVirgin(dbt);
1021: UserLink.setUserid(user);
1022: UserLink.setGroupid(group);
1023: UserLink.save();
1024: }
1025:
1026: /**
1027: * Removes the user from the group.
1028: *
1029: * @param groupName name of the given group.
1030: * @param username username used to uniquely identify shark user.
1031: * @throws Exception If something unexpected happens.
1032: */
1033: public void removeUserFromGroup(String groupName, String username)
1034: throws Exception {
1035:
1036: DBTransaction dbt = getDBTransaction();
1037:
1038: GroupQuery gQry = new GroupQuery(dbt);
1039: gQry.setQueryGroupid(groupName);
1040: gQry.requireUniqueInstance();
1041: GroupDO group = gQry.getNextDO();
1042:
1043: if (group == null)
1044: throw new Exception("Group " + groupName
1045: + "does not exist.");
1046:
1047: UserQuery uQry = new UserQuery(dbt);
1048: uQry.setQueryUserid(username);
1049: uQry.requireUniqueInstance();
1050: UserDO user = uQry.getNextDO();
1051:
1052: if (user == null)
1053: throw new Exception("User " + username + "does not exist.");
1054:
1055: UserLinkQuery lQry = new UserLinkQuery(dbt);
1056: lQry.setQueryUserid(user);
1057: lQry.setQueryGroupid(group);
1058: lQry.requireUniqueInstance();
1059: lQry.getNextDO().delete();
1060: }
1061:
1062: /**
1063: * Moves user <i>username</i> from the group <i>currentGroup</i> to group <i>newGroup</i>.
1064: *
1065: * @param currentGroup current group that contains the user.
1066: * @param newGroup new group where the user will be moved to.
1067: * @param username the user that will be moved.
1068: * @throws Exception If something unexpected happens.
1069: */
1070: public void moveUser(String currentGroup, String newGroup,
1071: String username) throws Exception {
1072:
1073: DBTransaction dbt = getDBTransaction();
1074:
1075: GroupQuery gQry1 = new GroupQuery(dbt);
1076: gQry1.setQueryGroupid(currentGroup);
1077: gQry1.requireUniqueInstance();
1078: GroupDO currentG = gQry1.getNextDO();
1079:
1080: if (currentG == null)
1081: throw new Exception("Group " + currentGroup
1082: + "does not exist.");
1083:
1084: GroupQuery gQry2 = new GroupQuery(dbt);
1085: gQry2.setQueryGroupid(newGroup);
1086: gQry2.requireUniqueInstance();
1087: GroupDO newG = gQry2.getNextDO();
1088:
1089: if (newG == null)
1090: throw new Exception("Group " + newGroup + "does not exist.");
1091:
1092: UserQuery uQry = new UserQuery(dbt);
1093: uQry.setQueryUserid(username);
1094: uQry.requireUniqueInstance();
1095: UserDO user = uQry.getNextDO();
1096:
1097: if (user == null)
1098: throw new Exception("User " + username + "does not exist.");
1099:
1100: UserLinkQuery lQry = new UserLinkQuery(dbt);
1101: lQry.setQueryUserid(user);
1102: lQry.setQueryGroupid(currentG);
1103: lQry.requireUniqueInstance();
1104: lQry.getNextDO().delete();
1105:
1106: UserLinkDO UserLink = UserLinkDO.createVirgin(dbt);
1107: UserLink.setUserid(user);
1108: UserLink.setGroupid(newG);
1109: UserLink.save();
1110:
1111: }
1112:
1113: private boolean doesUserBelongToGroup(GroupDO group, UserDO user)
1114: throws Exception {
1115: boolean ret = false;
1116:
1117: DBTransaction dbt = getDBTransaction();
1118:
1119: UserLinkQuery lQry = new UserLinkQuery(dbt);
1120: lQry.setQueryGroupid(group);
1121: lQry.setQueryUserid(user);
1122: lQry.requireUniqueInstance();
1123: UserLinkDO result = lQry.getNextDO();
1124:
1125: if (result != null)
1126: ret = true;
1127: else {
1128: GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
1129: lQry1.setQueryGroupid(group);
1130: GroupLinkDO result1 = lQry1.getNextDO();
1131: while ((!ret) && (result1 != null)) {
1132: ret = doesUserBelongToGroup(result1.getSub_gid(), user);
1133: result1 = lQry1.getNextDO();
1134: }
1135: }
1136:
1137: return ret;
1138: }
1139:
1140: public String[] getObjects(WMSessionHandle sessionHandle,
1141: String expression) throws Exception {
1142: // TODO Auto-generated method stub
1143: return null;
1144: }
1145:
1146: public String[] getGroups(WMSessionHandle sessionHandle,
1147: String expression) throws Exception {
1148: // TODO Auto-generated method stub
1149: return null;
1150: }
1151:
1152: /**
1153: * Allows administrator to create new user. After its creation, the client application
1154: * will always be able to log onto shark using username and password defined for the
1155: * user.
1156: *
1157: * @param groupName groupName used to uniquely identify group - this parameter is
1158: * mandatory.
1159: * @param username username used to uniquely identify user - this parameter is
1160: * mandatory.
1161: * @param password password used to authenticate - this parameter is mandatory.
1162: * @param firstName the user's first name.
1163: * @param lastName the user's last name.
1164: * @param emailAddress email address of the user.
1165: * @throws Exception If something unexpected happens (i.e the user with given username
1166: * already exists).
1167: */
1168: public void createUser(String groupName, String username,
1169: String password, String firstName, String lastName,
1170: String emailAddress) throws Exception {
1171: if (doesUserExist(username)) {
1172: throw new Exception("User already exists");
1173: }
1174:
1175: DBTransaction dbt = getDBTransaction();
1176:
1177: GroupQuery gQry = new GroupQuery(dbt);
1178: gQry.setQueryGroupid(groupName);
1179: gQry.requireUniqueInstance();
1180: GroupDO group = gQry.getNextDO();
1181:
1182: UserDO user = UserDO.createVirgin(dbt);
1183: user.setUserid(username);
1184: user.setPasswd(MiscUtilities.passwordDigest(password,
1185: digestAlgorithm));
1186: user.setFirstname(firstName);
1187: user.setLastname(lastName);
1188: user.setEmail(emailAddress);
1189: user.save();
1190:
1191: UserLinkDO UserLink = UserLinkDO.createVirgin(dbt);
1192: UserLink.setUserid(user);
1193: UserLink.setGroupid(group);
1194: UserLink.save();
1195: }
1196:
1197: /**
1198: * Allows administrator to update data about user.
1199: *
1200: * @param username username used to uniquely identify user - this parameter is
1201: * mandatory.
1202: * @param firstName the user's first name.
1203: * @param lastName the user's last name.
1204: * @param emailAddress email address of the user.
1205: * @throws Exception If something unexpected happens (i.e the user with given username
1206: * does not exist).
1207: */
1208: public void updateUser(String username, String firstName,
1209: String lastName, String emailAddress) throws Exception {
1210: DBTransaction dbt = getDBTransaction();
1211:
1212: UserQuery uQry = new UserQuery(dbt);
1213: uQry.setQueryUserid(username);
1214: uQry.requireUniqueInstance();
1215: UserDO user = uQry.getNextDO();
1216:
1217: if (user == null)
1218: throw new Exception("User " + username + "does not exist.");
1219:
1220: user.setFirstname(firstName);
1221: user.setLastname(lastName);
1222: user.setEmail(emailAddress);
1223: user.save();
1224: }
1225:
1226: /**
1227: * Allows administrator to remove the user.
1228: *
1229: * @param username username used to uniquely identify user.
1230: * @throws Exception If something unexpected happens (i.e the user with given username
1231: * does not exist, or this is a user that can't be removed).
1232: */
1233: public void removeUser(String username) throws Exception {
1234: DBTransaction dbt = getDBTransaction();
1235:
1236: UserQuery uQry = new UserQuery(dbt);
1237: uQry.setQueryUserid(username);
1238: uQry.requireUniqueInstance();
1239: uQry.getNextDO().delete();
1240: }
1241:
1242: /**
1243: * Sets user password.
1244: *
1245: * @param username username of the shark user.
1246: * @param password new password of the shark user.
1247: * @throws Exception If something unexpected happens.
1248: */
1249: public void setPassword(String username, String password)
1250: throws Exception {
1251: DBTransaction dbt = getDBTransaction();
1252:
1253: UserQuery uQry = new UserQuery(dbt);
1254: uQry.setQueryUserid(username);
1255: uQry.requireUniqueInstance();
1256: UserDO user = uQry.getNextDO();
1257: user.setPasswd(MiscUtilities.passwordDigest(password,
1258: digestAlgorithm));
1259: user.save();
1260: }
1261:
1262: /**
1263: * Validates user.
1264: *
1265: * @param username user name.
1266: * @param password user password.
1267: * @return true if user is validated, otherwise false.
1268: * @exception Exception If something unexpected happens.
1269: */
1270: public boolean validateUser(String username, String password)
1271: throws Exception {
1272: boolean ret = false;
1273: DBTransaction dbt = getDBTransaction();
1274: UserQuery uQry = new UserQuery(dbt);
1275: uQry.setQueryUserid(username);
1276: uQry.requireUniqueInstance();
1277: UserDO user = uQry.getNextDO();
1278:
1279: ret = (null != user) ? MiscUtilities.passwordDigest(password,
1280: digestAlgorithm).equals(user.getPasswd()) : false;
1281:
1282: return ret;
1283: }
1284:
1285: /**
1286: * Returns true if user group with given name exists.
1287: *
1288: * @param groupName name of the given group.
1289: * @return true if user group exists, otherwise false.
1290: * @throws Exception If something unexpected happens.
1291: */
1292: protected boolean doesGroupExist(String groupName) throws Exception {
1293: boolean ret = false;
1294:
1295: DBTransaction dbt = getDBTransaction();
1296:
1297: GroupQuery qry = new GroupQuery(dbt);
1298: qry.setQueryGroupid(groupName);
1299: qry.requireUniqueInstance();
1300: ret = null != qry.getNextDO();
1301:
1302: return ret;
1303: }
1304:
1305: /**
1306: * Returns true if user with given username exists.
1307: *
1308: * @param username username of the shark user.
1309: * @return true if the user with the given username exists, otherwise false.
1310: * @throws Exception If something unexpected happens.
1311: */
1312: protected boolean doesUserExist(String username) throws Exception {
1313: boolean ret = false;
1314:
1315: DBTransaction dbt = getDBTransaction();
1316:
1317: UserQuery uQry = new UserQuery(dbt);
1318: uQry.setQueryUserid(username);
1319: uQry.requireUniqueInstance();
1320: ret = null != uQry.getNextDO();
1321:
1322: return ret;
1323: }
1324:
1325: protected DBTransaction getDBTransaction() throws Exception {
1326: return db.createTransaction();
1327: }
1328:
1329: protected void setLDBName() {
1330: String defDBName = DODS.getDatabaseManager().getDefaultDB();
1331: try {
1332: String dbName = db.getName();
1333: DODS.getDatabaseManager().setDefaultDatabase(dbName);
1334: refreshDOCaches();
1335: } catch (Exception _) {
1336: _.printStackTrace();
1337: } finally {
1338: try {
1339: DODS.getDatabaseManager().setDefaultDatabase(defDBName);
1340: } catch (DatabaseManagerException e) {
1341: e.printStackTrace();
1342: }
1343: }
1344: }
1345:
1346: protected void refreshDOCaches() throws Exception {
1347: UserLinkDO.refreshCache();
1348: GroupLinkDO.refreshCache();
1349: GroupDO.refreshCache();
1350: UserDO.refreshCache();
1351: }
1352:
1353: }
|