001: /*
002: * FlowAuthorizationUtils.java
003: *
004: * Version: $Revision: 1.3 $
005: *
006: * Date: $Date: 2006/07/13 23:20:54 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.aspect.administrative;
042:
043: import java.sql.SQLException;
044: import java.util.List;
045:
046: import org.dspace.app.xmlui.wing.Message;
047: import org.dspace.authorize.AuthorizeException;
048: import org.dspace.authorize.AuthorizeManager;
049: import org.dspace.authorize.PolicySet;
050: import org.dspace.authorize.ResourcePolicy;
051: import org.dspace.content.Bitstream;
052: import org.dspace.content.Bundle;
053: import org.dspace.content.Collection;
054: import org.dspace.content.Community;
055: import org.dspace.content.DSpaceObject;
056: import org.dspace.content.Item;
057: import org.dspace.core.Context;
058: import org.dspace.eperson.Group;
059: import org.dspace.handle.HandleManager;
060:
061: import org.dspace.core.Constants;
062:
063: /**
064: *
065: * FIXME: add documentation
066: *
067: * @author Alexey maslov
068: */
069: public class FlowAuthorizationUtils {
070:
071: /** Language Strings */
072: //example language string
073: // private static final Message T_add_eperson_success_notice =
074: // new Message("default","xmlui.administrative.FlowUtils.add-eperson-success-notice");
075:
076: /**
077: * Resolve an identifier submitted into the item lookup box. If it contains a slash, it's assumed to be a
078: * handle and is resolved by that mechanism into an item, collection or community. Otherwise, it's assumed
079: * to be an item and looked up by ID.
080: *
081: * @param context The current DSpace context.
082: * @param identifier The identifier that is to be resolved.
083: * @return A process result's object.
084: */
085: public static FlowResult resolveItemIdentifier(Context context,
086: String identifier) throws SQLException {
087: FlowResult result = new FlowResult();
088: result.setContinue(false);
089: //Check whether it's a handle or internal id (by check ing if it has a slash in the string)
090: if (identifier.contains("/")) {
091: DSpaceObject dso = HandleManager.resolveToObject(context,
092: identifier);
093:
094: if (dso != null && dso.getType() == Constants.ITEM) {
095: result.setParameter("itemID", dso.getID());
096: result.setParameter("type", Constants.ITEM);
097: result.setContinue(true);
098: return result;
099: } else if (dso != null
100: && dso.getType() == Constants.COLLECTION) {
101: result.setParameter("collectionID", dso.getID());
102: result.setParameter("type", Constants.COLLECTION);
103: result.setContinue(true);
104: return result;
105: } else if (dso != null
106: && dso.getType() == Constants.COMMUNITY) {
107: result.setParameter("communityID", dso.getID());
108: result.setParameter("type", Constants.COMMUNITY);
109: result.setContinue(true);
110: return result;
111: }
112: }
113: // Otherwise, it's assumed to be a DSpace Item
114: else {
115: Item item = null;
116: try {
117: item = Item.find(context, Integer.valueOf(identifier));
118: } catch (NumberFormatException e) {
119: // ignoring the exception in case of a malformed input string
120: }
121:
122: if (item != null) {
123: result.setParameter("itemID", item.getID());
124: result.setParameter("type", Constants.ITEM);
125: result.setContinue(true);
126: return result;
127: }
128: }
129:
130: result.addError("identifier");
131: return result;
132: }
133:
134: /**
135: * Process the editing of an existing or a newly created policy.
136: *
137: * @param context The current DSpace context.
138: * @param objectType The type of the policy's parent object (ITEM, COLLECTION, COMMUNITY)
139: * @param objectID The ID of the policy's parent object
140: * @param policyID The ID of the policy being edited (-1 if a new policy is being created)
141: * @param groupID The ID of the group to be associated with this policy
142: * @param actionID The ID of the action (dependent on the objectType) to be associated with this policy
143: * @return A process result's object.
144: */
145: public static FlowResult processEditPolicy(Context context,
146: int objectType, int objectID, int policyID, int groupID,
147: int actionID) throws SQLException, AuthorizeException {
148: FlowResult result = new FlowResult();
149: boolean added = false;
150:
151: ResourcePolicy policy = ResourcePolicy.find(context, policyID);
152:
153: /* First and foremost, if no group or action was selected, throw an error back to the user */
154: if (actionID == -1) {
155: result.setContinue(false);
156: result.addError("action_id");
157: return result;
158: }
159: if (groupID == -1) {
160: result.setContinue(false);
161: result.addError("group_id");
162: return result;
163: }
164:
165: /* If the policy doesn't exist, cretae a new one and set its parent resource */
166: DSpaceObject policyParent = null;
167: if (policy == null) {
168: policy = ResourcePolicy.create(context);
169:
170: switch (objectType) {
171: case Constants.COMMUNITY:
172: policyParent = Community.find(context, objectID);
173: break;
174: case Constants.COLLECTION:
175: policyParent = Collection.find(context, objectID);
176: break;
177: case Constants.ITEM:
178: policyParent = Item.find(context, objectID);
179: break;
180: case Constants.BUNDLE:
181: policyParent = Bundle.find(context, objectID);
182: break;
183: case Constants.BITSTREAM:
184: policyParent = Bitstream.find(context, objectID);
185: break;
186: }
187: policy.setResource(policyParent);
188: added = true;
189: }
190:
191: Group group = Group.find(context, groupID);
192:
193: // modify the policy
194: policy.setAction(actionID);
195: policy.setGroup(group);
196:
197: // propagate the changes to the logo, which is treated on the same level as the parent object
198: Bitstream logo = null;
199: DSpaceObject logoContainer = null;
200: if (objectType == Constants.COLLECTION) {
201: logoContainer = Collection.find(context, objectID);
202: logo = ((Collection) logoContainer).getLogo();
203: } else if (objectType == Constants.COMMUNITY) {
204: logoContainer = Community.find(context, objectID);
205: logo = ((Community) logoContainer).getLogo();
206: }
207:
208: if (logo != null) {
209: List policySet = AuthorizeManager.getPolicies(context,
210: logoContainer);
211: AuthorizeManager.removeAllPolicies(context, logo);
212: AuthorizeManager.addPolicies(context, policySet, logo);
213: }
214:
215: // Perform the update action
216: policy.update();
217: context.commit();
218:
219: result.setContinue(true);
220: result.setOutcome(true);
221: if (added)
222: result.setMessage(new Message("default",
223: "A new policy was created successfully"));
224: else
225: result.setMessage(new Message("default",
226: "The policy was edited successfully"));
227:
228: result.setParameter("policyID", policy.getID());
229:
230: return result;
231: }
232:
233: /**
234: * Delete the policies specified by the policyIDs parameter. This assumes that the
235: * deletion has been confirmed.
236: *
237: * @param context The current DSpace context
238: * @param policyIDs The unique ids of the policies being deleted.
239: * @return A process result's object.
240: */
241: public static FlowResult processDeletePolicies(Context context,
242: String[] policyIDs) throws NumberFormatException,
243: SQLException {
244: FlowResult result = new FlowResult();
245:
246: for (String id : policyIDs) {
247: ResourcePolicy policyDeleted = ResourcePolicy.find(context,
248: Integer.valueOf(id));
249: policyDeleted.delete();
250: }
251:
252: result.setContinue(true);
253: result.setOutcome(true);
254: result.setMessage(new Message("default",
255: "The policies were deleted successfully"));
256:
257: return result;
258: }
259:
260: /**
261: * Process addition of a several authorizations at once, as entered in the wildcard/advanced authorizations tool
262: *
263: * @param context The current DSpace context.
264: * @param groupIDs The IDs of the groups to be associated with the newly created policies
265: * @param actionID The ID of the action to be associated with the policies
266: * @param resourceID Whether the policies will apply to Items or Bitstreams
267: * @param collectionIDs The IDs of the collections that the policies will be applied to
268: * @return A process result's object.
269: */
270: public static FlowResult processAdvancedPolicyAdd(Context context,
271: String[] groupIDs, int actionID, int resourceID,
272: String[] collectionIDs) throws NumberFormatException,
273: SQLException, AuthorizeException {
274: FlowResult result = new FlowResult();
275:
276: for (String groupID : groupIDs) {
277: for (String collectionID : collectionIDs) {
278: PolicySet.setPolicies(context, Constants.COLLECTION,
279: Integer.valueOf(collectionID), resourceID,
280: actionID, Integer.valueOf(groupID), false,
281: false);
282:
283: // if it's a bitstream, do it to the bundle too
284: if (resourceID == Constants.BITSTREAM) {
285: PolicySet.setPolicies(context,
286: Constants.COLLECTION, Integer
287: .valueOf(collectionID),
288: Constants.BUNDLE, actionID, Integer
289: .valueOf(groupID), false, false);
290: }
291: }
292: }
293:
294: result.setContinue(true);
295: result.setOutcome(true);
296: result.setMessage(new Message("default",
297: "The policies were added successfully!"));
298:
299: return result;
300: }
301:
302: /**
303: * Process the deletion of all authorizations across a set of collections, regardless of associated
304: * actions or groups. This functionality should probably not be used, ever, unless the goal to
305: * completely reset a collection for one reason or another.
306: *
307: * @param context The current DSpace context.
308: * @param resourceID Whether the policies will apply to Items or Bitstreams
309: * @param collectionIDs The IDs of the collections that the policy wipe will be applied to
310: * @return A process result's object.
311: */
312: public static FlowResult processAdvancedPolicyDelete(
313: Context context, int resourceID, String[] collectionIDs)
314: throws NumberFormatException, SQLException,
315: AuthorizeException {
316: FlowResult result = new FlowResult();
317:
318: for (String collectionID : collectionIDs) {
319: PolicySet.setPolicies(context, Constants.COLLECTION,
320: Integer.valueOf(collectionID), resourceID, 0, 0,
321: false, true);
322:
323: // if it's a bitstream, do it to the bundle too
324: if (resourceID == Constants.BITSTREAM) {
325: PolicySet.setPolicies(context, Constants.COLLECTION,
326: Integer.valueOf(collectionID),
327: Constants.BUNDLE, 0, 0, false, true);
328: }
329: }
330:
331: result.setContinue(true);
332: result.setOutcome(true);
333: result
334: .setMessage(new Message("default",
335: "The policies for the selected collections were cleared."));
336:
337: return result;
338: }
339:
340: }
|