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;
29 import org.collectionspace.authentication.AuthN;
30 import org.collectionspace.services.authorization.perms.ActionType;
31 import org.collectionspace.services.authorization.AuthZ;
32 import org.collectionspace.services.authorization.CSpaceAction;
33 import org.collectionspace.services.authorization.CSpaceResource;
34 import org.collectionspace.services.authorization.perms.EffectType;
35 import org.collectionspace.services.authorization.perms.Permission;
36 import org.collectionspace.services.authorization.perms.PermissionAction;
37 import org.collectionspace.services.authorization.PermissionException;
38 import org.collectionspace.services.authorization.PermissionRole;
39 import org.collectionspace.services.authorization.PermissionValue;
40 import org.collectionspace.services.authorization.Role;
41 import org.collectionspace.services.authorization.RoleValue;
42 import org.collectionspace.services.authorization.SubjectType;
43 import org.collectionspace.services.authorization.URIResourceImpl;
44 import org.collectionspace.services.common.authorization_mgt.PermissionRoleUtil;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
47 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * AuthorizationDelegate delegates permissions management to the underlying authorization
53 * service from the RESTful service layer. The authorization service for example
54 * might manage permissions with the help of a provider (e.g. Spring Security ACL)
57 public class AuthorizationDelegate {
59 private static final Logger logger = LoggerFactory.getLogger(AuthorizationDelegate.class);
62 * addPermissions add permissions represented given PermissionRole
64 * @param pr permission role
68 static void addPermissions(ServiceContext ctx, PermissionRole pr) throws Exception {
69 SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
70 AuthZ authz = AuthZ.get();
71 if (subject.equals(SubjectType.ROLE)) {
72 PermissionValue pv = pr.getPermission().get(0);
73 Permission p = getPermission(pv.getPermissionId());
75 String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
77 throw new DocumentNotFoundException(msg);
79 CSpaceResource[] resources = getResources(p);
80 String[] roles = getRoles(pr.getRole());
81 for (CSpaceResource res : resources) {
82 boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
83 authz.addPermissions(res, roles, grant);
85 } else if (SubjectType.PERMISSION.equals(subject)) {
86 RoleValue rv = pr.getRole().get(0);
87 Role r = getRole(rv.getRoleId());
89 String msg = "addPermissions: No role found for id=" + rv.getRoleId();
91 throw new DocumentNotFoundException(msg);
93 //using r not rv ensures we're getting the "ROLE" prefix/qualified name
94 // This needs to use the qualified name, not the display name
95 String[] roles = {r.getRoleName()};
96 for (PermissionValue pv : pr.getPermission()) {
97 Permission p = getPermission(pv.getPermissionId());
99 String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
101 //TODO: would be nice contiue to still send 400 back
104 CSpaceResource[] resources = getResources(p);
105 for (CSpaceResource res : resources) {
106 boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
107 authz.addPermissions(res, roles, grant);
114 * deletePermissions delete all permissions associated with given permission role
116 * @param pr permissionrole
119 static void deletePermissions(ServiceContext ctx, PermissionRole pr)
121 SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
122 AuthZ authz = AuthZ.get();
123 if (subject.equals(SubjectType.ROLE)) {
124 List<PermissionValue> permissionValues = pr.getPermission();
125 if (permissionValues != null & permissionValues.size() > 0) {
126 PermissionValue pv = permissionValues.get(0);
127 Permission p = getPermission(pv.getPermissionId());
129 String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
131 throw new DocumentNotFoundException(msg);
133 CSpaceResource[] resources = getResources(p);
134 String[] roles = getRoles(pr.getRole());
135 for (CSpaceResource res : resources) {
136 authz.deletePermissions(res, roles);
139 } else if (SubjectType.PERMISSION.equals(subject)) {
140 List<RoleValue> roleValues = pr.getRole();
141 if (roleValues != null && roleValues.size() > 0) {
142 RoleValue rv = roleValues.get(0);
143 Role r = getRole(rv.getRoleId());
145 String msg = "deletePermissions: No role found for id=" + rv.getRoleId();
147 throw new DocumentNotFoundException(msg);
149 //using r not rv ensures we're getting the "ROLE" prefix/qualified name
150 // This needs to use the qualified name, not the display name
151 String[] roles = {r.getRoleName()};
152 for (PermissionValue pv : pr.getPermission()) {
153 Permission p = getPermission(pv.getPermissionId());
155 String msg = "deletePermissions: No permission found for id=" + pv.getPermissionId();
157 //TODO: would be nice contiue to still send 400 back
160 CSpaceResource[] resources = getResources(p);
161 for (CSpaceResource res : resources) {
162 authz.deletePermissions(res, roles);
170 * deletePermissions delete permissions associated with given permission id
174 //Non-javadoc comment : this is a very dangerous operation as it deletes
175 //the Spring ACL instead of ACE(s) that is associated with each role
176 //the ACL might be needed for other ACEs (including those for ROLE_ADMINISTRATOR,
177 //ROLE_TENANT_ADMINISTRATOR, etc.)...
178 static public void deletePermissions(String permCsid) throws Exception {
179 Permission p = getPermission(permCsid);
181 String msg = "deletePermissions: No permission found for id=" + permCsid;
183 throw new DocumentNotFoundException(msg);
185 CSpaceResource[] resources = getResources(p);
186 AuthZ authz = AuthZ.get();
188 for (CSpaceResource res : resources) {
190 authz.deletePermissions(res);
191 } catch (PermissionException pe) {
192 //perms are created downthere only if roles are related to the permissions
193 logger.info("no permissions found in authz service provider for "
194 + "permCsid=" + permCsid + " res=" + res.getId());
200 * getRoles get roles (string) array from given RoleValue list
201 * @param rvl rolevalue list
202 * @return string array with role names
205 private static String[] getRoles(List<RoleValue> rvl)
206 throws DocumentNotFoundException {
207 List<String> rvls = new ArrayList<String>();
208 for (RoleValue rv : rvl) {
209 Role r = getRole(rv.getRoleId());
211 String msg = "getRoles: No role found for id=" + rv.getRoleId();
213 //TODO: would be nice contiue to still send 400 back
216 rvls.add(r.getRoleName());
218 return rvls.toArray(new String[0]);
222 * getResources from given PermissionValue
223 * @param permisison csid
224 * @return array of CSpaceResource
225 * @see PermissionValue
226 * @see CSpaceResource
228 private static CSpaceResource[] getResources(Permission p) {
229 List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
231 for (PermissionAction pa : p.getAction()) {
232 CSpaceResource res = null;
233 if (p.getTenantId() == null) {
234 res = new URIResourceImpl(AuthN.get().getCurrentTenantId(), p.getResourceName(),
235 getAction(pa.getName()));
237 res = new URIResourceImpl(p.getTenantId(), p.getResourceName(),
238 getAction(pa.getName()));
242 return rl.toArray(new CSpaceResource[0]);
245 private static Permission getPermission(String permCsid)
246 throws DocumentNotFoundException {
247 Permission p = (Permission) JpaStorageUtils.getEntity(permCsid,
252 private static Role getRole(String roleCsid)
253 throws DocumentNotFoundException {
254 Role r = (Role) JpaStorageUtils.getEntity(roleCsid,
260 * getAction is a convenience method to get corresponding action for
265 public static CSpaceAction getAction(ActionType action) {
266 if (ActionType.CREATE.equals(action)) {
267 return CSpaceAction.CREATE;
268 } else if (ActionType.READ.equals(action)) {
269 return CSpaceAction.READ;
270 } else if (ActionType.UPDATE.equals(action)) {
271 return CSpaceAction.UPDATE;
272 } else if (ActionType.DELETE.equals(action)) {
273 return CSpaceAction.DELETE;
274 } else if (ActionType.SEARCH.equals(action)) {
275 return CSpaceAction.SEARCH;
276 } else if (ActionType.ADMIN.equals(action)) {
277 return CSpaceAction.ADMIN;
278 } else if (ActionType.START.equals(action)) {
279 return CSpaceAction.START;
280 } else if (ActionType.STOP.equals(action)) {
281 return CSpaceAction.STOP;
283 throw new IllegalArgumentException("action = " + action.toString());