001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels.groupsmanager.commands;
007:
008: import org.jasig.portal.ChannelStaticData;
009: import org.jasig.portal.channels.groupsmanager.CGroupsManagerSessionData;
010: import org.jasig.portal.channels.groupsmanager.GroupsManagerConstants;
011: import org.jasig.portal.channels.groupsmanager.IGroupsManagerCommand;
012: import org.jasig.portal.channels.groupsmanager.Utility;
013: import org.jasig.portal.groups.IGroupMember;
014: import org.jasig.portal.security.IAuthorizationPrincipal;
015: import org.jasig.portal.security.IPermission;
016: import org.jasig.portal.security.IUpdatingPermissionManager;
017: import org.jasig.portal.services.AuthorizationService;
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import org.w3c.dom.NodeList;
021:
022: /**
023: * This class is the parent class of all other command classes.
024: * @author Don Fracapane
025: * @version $Revision: 35497 $
026: */
027: public abstract class GroupsManagerCommand implements
028: IGroupsManagerCommand, GroupsManagerConstants {
029:
030: /**
031: * GroupsManagerCommand is the parent of all Groups Manager commands. It
032: * hold the commone functionality of all commands.
033: */
034: public GroupsManagerCommand() {
035: }
036:
037: /**
038: * This is the public method
039: * @param sessionData
040: * @throws Exception
041: */
042: public void execute(CGroupsManagerSessionData sessionData)
043: throws Exception {
044: }
045:
046: /**
047: * clear out the selection list
048: * @param sessionData
049: */
050: protected void clearSelected(CGroupsManagerSessionData sessionData) {
051: Element rootElem = getXmlDoc(sessionData).getDocumentElement();
052: NodeList nGroupList = rootElem
053: .getElementsByTagName(GROUP_TAGNAME);
054: NodeList nPersonList = rootElem
055: .getElementsByTagName(ENTITY_TAGNAME);
056: NodeList nList = nGroupList;
057: for (int i = 0; i < nList.getLength(); i++) {
058: Element elem = (org.w3c.dom.Element) nList.item(i);
059: elem.setAttribute("selected", "false");
060: }
061: nList = nPersonList;
062: for (int i = 0; i < nList.getLength(); i++) {
063: Element elem = (org.w3c.dom.Element) nList.item(i);
064: elem.setAttribute("selected", "false");
065: }
066: return;
067: }
068:
069: /**
070: * Removes all of the permissions for a GroupMember. We need to get permissions
071: * for the group as a principal and as a target. I am merging the 2 arrays into a
072: * single array in order to use the transaction management in the RDBMPermissionsImpl.
073: * If an exception is generated, I do not delete the group or anything else.
074: * Possible Exceptions: AuthorizationException and GroupsException
075: * @param grpMbr
076: * @throws Exception
077: */
078: protected void deletePermissions(IGroupMember grpMbr)
079: throws Exception {
080: try {
081: String grpKey = grpMbr.getKey();
082: // first we retrieve all permissions for which the group is the principal
083: IAuthorizationPrincipal iap = AuthorizationService
084: .instance().newPrincipal(grpMbr);
085: IPermission[] perms1 = iap.getPermissions();
086:
087: // next we retrieve all permissions for which the group is the target
088: IUpdatingPermissionManager upm = AuthorizationService
089: .instance().newUpdatingPermissionManager(OWNER);
090: IPermission[] perms2 = upm.getPermissions(null, grpKey);
091:
092: // merge the permissions
093: IPermission[] allPerms = new IPermission[perms1.length
094: + perms2.length];
095: System.arraycopy(perms1, 0, allPerms, 0, perms1.length);
096: System.arraycopy(perms2, 0, allPerms, perms1.length,
097: perms2.length);
098:
099: upm.removePermissions(allPerms);
100: } catch (Exception e) {
101: String errMsg = "DeleteGroup::deletePermissions(): Error removing permissions for "
102: + grpMbr;
103: Utility.logMessage("ERROR", errMsg, e);
104: throw new Exception(errMsg);
105: }
106: }
107:
108: /**
109: * Answers if the parentGroupId has been set. If it has not been set, this
110: * would indicate that Groups Manager is in Servant mode.
111: * @param staticData
112: * @return boolean
113: */
114: protected boolean hasParentId(ChannelStaticData staticData) {
115: String pk = getParentId(staticData);
116: if (pk == null) {
117: return false;
118: }
119: if (pk.equals("")) {
120: return false;
121: }
122: Utility.logMessage("Debug",
123: "GroupsManagerCommand::hasParentId: Value is set to default: "
124: + pk);
125: return true;
126: }
127:
128: /**
129: * Returns the grpCommand parameter from runtimeData
130: * @param runtimeData
131: * @return String
132: */
133: protected String getCommand(
134: org.jasig.portal.ChannelRuntimeData runtimeData) {
135: return (String) runtimeData.getParameter("grpCommand");
136: }
137:
138: /**
139: * Returns the grpCommandIds parameter from runtimeData. The string usually
140: * hold one element ID but could contain a string of delimited ids. (See
141: * RemoveMember command).
142: * @param runtimeData
143: * @return String
144: */
145: protected String getCommandArg(
146: org.jasig.portal.ChannelRuntimeData runtimeData) {
147: return (String) runtimeData.getParameter("grpCommandArg");
148: }
149:
150: /**
151: * Returns the groupParentId parameter from staticData
152: * @param staticData
153: * @return String
154: */
155: protected String getParentId(ChannelStaticData staticData) {
156: return staticData.getParameter("groupParentId");
157: }
158:
159: /**
160: * Returns the userID from the user object
161: * @param sessionData
162: * @return String
163: */
164: protected String getUserID(CGroupsManagerSessionData sessionData) {
165: return String.valueOf(sessionData.user.getID());
166: }
167:
168: /**
169: * Returns the cached xml document from staticData
170: * @param sessionData
171: * @return Document
172: */
173: protected Document getXmlDoc(CGroupsManagerSessionData sessionData) {
174: //return (Document)staticData.get("xmlDoc");sessionData.model
175: return sessionData.model;
176: }
177:
178: /**
179: * Set the CommandArg value, useful for commands which would like to chain
180: * other commands
181: * @param runtimeData
182: * @param arg String
183: */
184: protected void setCommandArg(
185: org.jasig.portal.ChannelRuntimeData runtimeData, String arg) {
186: runtimeData.setParameter("grpCommandArg", arg);
187: }
188:
189: }
|