2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2010 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.authorization.storage;
26 import java.util.ArrayList;
27 import java.util.List;
28 import org.collectionspace.services.authorization.perms.ActionType;
29 import org.collectionspace.services.authorization.AuthZ;
30 import org.collectionspace.services.authorization.CSpaceAction;
31 import org.collectionspace.services.authorization.CSpaceResource;
32 import org.collectionspace.services.authorization.perms.EffectType;
33 import org.collectionspace.services.authorization.perms.Permission;
34 import org.collectionspace.services.authorization.perms.PermissionAction;
35 import org.collectionspace.services.authorization.PermissionException;
36 import org.collectionspace.services.authorization.PermissionRole;
37 import org.collectionspace.services.authorization.PermissionValue;
38 import org.collectionspace.services.authorization.Role;
39 import org.collectionspace.services.authorization.RoleValue;
40 import org.collectionspace.services.authorization.SubjectType;
41 import org.collectionspace.services.authorization.URIResourceImpl;
42 import org.collectionspace.services.common.context.ServiceContext;
43 import org.collectionspace.services.common.document.DocumentNotFoundException;
44 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * AuthorizationDelegate delegates permissions management to the underlying authorization
50 * service from the RESTful service layer. The authorization service for example
51 * might manage permissions with the help of a provider (e.g. Spring Security ACL)
54 public class AuthorizationDelegate {
56 private static final Logger logger = LoggerFactory.getLogger(AuthorizationDelegate.class);
59 * addPermissions add permissions represented given PermissionRole
61 * @param pr permission role
65 static void addPermissions(ServiceContext ctx, PermissionRole pr) throws Exception {
66 SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
67 AuthZ authz = AuthZ.get();
68 if (subject.equals(SubjectType.ROLE)) {
69 PermissionValue pv = pr.getPermission().get(0);
70 Permission p = getPermission(pv.getPermissionId());
72 String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
74 throw new DocumentNotFoundException(msg);
76 CSpaceResource[] resources = getResources(p);
77 String[] roles = getRoles(pr.getRole());
78 for (CSpaceResource res : resources) {
79 boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
80 authz.addPermissions(res, roles, grant);
82 } else if (SubjectType.PERMISSION.equals(subject)) {
83 RoleValue rv = pr.getRole().get(0);
84 Role r = getRole(rv.getRoleId());
86 String msg = "addPermissions: No role found for id=" + rv.getRoleId();
88 throw new DocumentNotFoundException(msg);
90 //using r not rv ensures we're getting the "ROLE" prefix/qualified name
91 // This needs to use the qualified name, not the display name
92 String[] roles = {r.getRoleName()};
93 for (PermissionValue pv : pr.getPermission()) {
94 Permission p = getPermission(pv.getPermissionId());
96 String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
98 //TODO: would be nice contiue to still send 400 back
101 CSpaceResource[] resources = getResources(p);
102 for (CSpaceResource res : resources) {
103 boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
104 authz.addPermissions(res, roles, grant);
111 * deletePermissions delete all permissions associated with given permission role
113 * @param pr permissionrole
116 static void deletePermissions(ServiceContext ctx, PermissionRole pr)
118 SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
119 AuthZ authz = AuthZ.get();
120 if (subject.equals(SubjectType.ROLE)) {
121 List<PermissionValue> permissionValues = pr.getPermission();
122 if (permissionValues != null & permissionValues.size() > 0) {
123 PermissionValue pv = permissionValues.get(0);
124 Permission p = getPermission(pv.getPermissionId());
126 String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
128 throw new DocumentNotFoundException(msg);
130 CSpaceResource[] resources = getResources(p);
131 String[] roles = getRoles(pr.getRole());
132 for (CSpaceResource res : resources) {
133 authz.deletePermissions(res, roles);
136 } else if (SubjectType.PERMISSION.equals(subject)) {
137 List<RoleValue> roleValues = pr.getRole();
138 if (roleValues != null && roleValues.size() > 0) {
139 RoleValue rv = roleValues.get(0);
140 Role r = getRole(rv.getRoleId());
142 String msg = "deletePermissions: No role found for id=" + rv.getRoleId();
144 throw new DocumentNotFoundException(msg);
146 //using r not rv ensures we're getting the "ROLE" prefix/qualified name
147 // This needs to use the qualified name, not the display name
148 String[] roles = {r.getRoleName()};
149 for (PermissionValue pv : pr.getPermission()) {
150 Permission p = getPermission(pv.getPermissionId());
152 String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
154 //TODO: would be nice contiue to still send 400 back
157 CSpaceResource[] resources = getResources(p);
158 for (CSpaceResource res : resources) {
159 authz.deletePermissions(res, roles);
167 * deletePermissions delete permissions associated with given permission id
171 //Non-javadoc comment : this is a very dangerous operation as it deletes
172 //the Spring ACL instead of ACE(s) that is associated with each role
173 //the ACL might be needed for other ACEs (including those for ROLE_ADMINISTRATOR,
174 //ROLE_TENANT_ADMINISTRATOR, etc.)...
175 static public void deletePermissions(String permCsid) throws Exception {
176 Permission p = getPermission(permCsid);
178 String msg = "deletePermissions: No permission found for id=" + permCsid;
180 throw new DocumentNotFoundException(msg);
182 CSpaceResource[] resources = getResources(p);
183 AuthZ authz = AuthZ.get();
185 for (CSpaceResource res : resources) {
187 authz.deletePermissions(res);
188 } catch (PermissionException pe) {
189 //perms are created downthere only if roles are related to the permissions
190 logger.info("no permissions found in authz service provider for "
191 + "permCsid=" + permCsid + " res=" + res.getId());
197 * getRoles get roles (string) array from given RoleValue list
198 * @param rvl rolevalue list
199 * @return string array with role names
202 private static String[] getRoles(List<RoleValue> rvl)
203 throws DocumentNotFoundException {
204 List<String> rvls = new ArrayList<String>();
205 for (RoleValue rv : rvl) {
206 Role r = getRole(rv.getRoleId());
208 String msg = "getRoles: No role found for id=" + rv.getRoleId();
210 //TODO: would be nice contiue to still send 400 back
213 rvls.add(r.getRoleName());
215 return rvls.toArray(new String[0]);
219 * getResources from given PermissionValue
220 * @param permisison csid
221 * @return array of CSpaceResource
222 * @see PermissionValue
223 * @see CSpaceResource
225 private static CSpaceResource[] getResources(Permission p) {
226 List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
228 for (PermissionAction pa : p.getAction()) {
229 CSpaceResource res = null;
230 if (p.getTenantId() == null) {
231 res = new URIResourceImpl(p.getResourceName(),
232 getAction(pa.getName()));
234 res = new URIResourceImpl(p.getTenantId(), p.getResourceName(),
235 getAction(pa.getName()));
239 return rl.toArray(new CSpaceResource[0]);
242 private static Permission getPermission(String permCsid)
243 throws DocumentNotFoundException {
244 Permission p = (Permission) JpaStorageUtils.getEntity(permCsid,
249 private static Role getRole(String roleCsid)
250 throws DocumentNotFoundException {
251 Role r = (Role) JpaStorageUtils.getEntity(roleCsid,
257 * getAction is a convenience method to get corresponding action for
262 public static CSpaceAction getAction(ActionType action) {
263 if (ActionType.CREATE.equals(action)) {
264 return CSpaceAction.CREATE;
265 } else if (ActionType.READ.equals(action)) {
266 return CSpaceAction.READ;
267 } else if (ActionType.UPDATE.equals(action)) {
268 return CSpaceAction.UPDATE;
269 } else if (ActionType.DELETE.equals(action)) {
270 return CSpaceAction.DELETE;
271 } else if (ActionType.SEARCH.equals(action)) {
272 return CSpaceAction.SEARCH;
273 } else if (ActionType.ADMIN.equals(action)) {
274 return CSpaceAction.ADMIN;
275 } else if (ActionType.START.equals(action)) {
276 return CSpaceAction.START;
277 } else if (ActionType.STOP.equals(action)) {
278 return CSpaceAction.STOP;
280 throw new IllegalArgumentException("action = " + action.toString());