]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
47aa546704e58c174a71d7bd245cb9ca2965ce0d
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2010 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.authorization.storage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import org.collectionspace.services.authorization.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.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionException;
35 import org.collectionspace.services.authorization.PermissionRole;
36 import org.collectionspace.services.authorization.PermissionValue;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.authorization.SubjectType;
39 import org.collectionspace.services.authorization.URIResourceImpl;
40 import org.collectionspace.services.common.context.ServiceContext;
41 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * AuthorizationDelegate delegates permissions management to the authorization
47  * service from the RESTful service
48  * @author
49  */
50 public class AuthorizationDelegate {
51
52     private final Logger logger = LoggerFactory.getLogger(AuthorizationDelegate.class);
53
54     static void addPermissions(ServiceContext ctx, PermissionRole pr) throws Exception {
55         SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
56         AuthZ authz = AuthZ.get();
57         if (subject.equals(SubjectType.ROLE)) {
58             PermissionValue pv = pr.getPermissions().get(0);
59             CSpaceResource[] resources = getResources(pv);
60             String[] roles = getRoles(pr.getRoles());
61             for (CSpaceResource res : resources) {
62                 authz.addPermissions(res, roles);
63             }
64         } else if (SubjectType.PERMISSION.equals(subject)) {
65             RoleValue rv = pr.getRoles().get(0);
66             String[] roles = {rv.getRoleName()};
67             for (PermissionValue pv : pr.getPermissions()) {
68                 CSpaceResource[] resources = getResources(pv);
69                 for (CSpaceResource res : resources) {
70                     authz.addPermissions(res, roles);
71                 }
72             }
73         }
74     }
75
76     static void deletePermissions(ServiceContext ctx, PermissionRole pr)
77             throws Exception {
78         PermissionValue pv = pr.getPermissions().get(0);
79         deletePermissions(pv);
80     }
81
82     static void deletePermissions(PermissionValue pv)
83             throws Exception {
84         CSpaceResource[] resources = getResources(pv);
85         AuthZ authz = AuthZ.get();
86         for (CSpaceResource res : resources) {
87             authz.deletePermissions(res);
88         }
89     }
90
91
92     /**
93      * addPermissionsForUri add permissions from given permission configuration
94      * with assumption that resource is of type URI
95      * @param permission configuration
96      */
97     //FIXME this method should be in the restful web service resource of authz
98     public void addPermissionsForUri(Permission perm,
99             PermissionRole permRole) throws PermissionException {
100         List<String> principals = new ArrayList<String>();
101         if (!perm.getCsid().equals(permRole.getPermissions().get(0).getPermissionId())) {
102             throw new IllegalArgumentException("permission ids do not"
103                     + " match for role=" + permRole.getRoles().get(0).getRoleName()
104                     + " with permissionId=" + permRole.getPermissions().get(0).getPermissionId()
105                     + " for permission with csid=" + perm.getCsid());
106         }
107         for (RoleValue roleValue : permRole.getRoles()) {
108             principals.add(roleValue.getRoleName());
109         }
110         List<PermissionAction> permActions = perm.getActions();
111         for (PermissionAction permAction : permActions) {
112             CSpaceAction action = getAction(permAction.getName());
113             URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(),
114                     perm.getResourceName(), action);
115             AuthZ.get().addPermissions(uriRes, principals.toArray(new String[0]));
116         }
117     }
118
119     /**
120      * getRoles get roles (string) array from given RoleValue list
121      * @param rvl rolevalue list
122      * @return string array with role names
123      * @see RoleValue
124      */
125     private static String[] getRoles(List<RoleValue> rvl) {
126         List<String> rvls = new ArrayList<String>();
127         for (RoleValue rv : rvl) {
128             //assumption: rolename is relationship metadata is mandatory
129             if (rv.getRoleName() != null) {
130                 rvls.add(rv.getRoleName());
131             }
132         }
133         return rvls.toArray(new String[0]);
134     }
135
136     /**
137      * getResources from given PermissionValue
138      * @param pv permission value
139      * @return array of CSpaceResource
140      * @see PermissionValue
141      * @see CSpaceResource
142      */
143     private static CSpaceResource[] getResources(PermissionValue pv) {
144         List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
145         Permission p = (Permission) JpaStorageUtils.getEntity(pv.getPermissionId(),
146                 Permission.class);
147         if (p != null) {
148             for (PermissionAction pa : p.getActions()) {
149
150                 CSpaceResource res = new URIResourceImpl(pv.getResourceName(),
151                         getAction(pa.getName()));
152                 rl.add(res);
153             }
154         }
155         return rl.toArray(new CSpaceResource[0]);
156     }
157
158
159     /**
160      * getAction is a convenience method to get corresponding action for
161      * given ActionType
162      * @param action
163      * @return
164      */
165     public static CSpaceAction getAction(ActionType action) {
166         if (ActionType.CREATE.equals(action)) {
167             return CSpaceAction.CREATE;
168         } else if (ActionType.READ.equals(action)) {
169             return CSpaceAction.READ;
170         } else if (ActionType.UPDATE.equals(action)) {
171             return CSpaceAction.UPDATE;
172         } else if (ActionType.DELETE.equals(action)) {
173             return CSpaceAction.DELETE;
174         } else if (ActionType.SEARCH.equals(action)) {
175             return CSpaceAction.SEARCH;
176         } else if (ActionType.ADMIN.equals(action)) {
177             return CSpaceAction.ADMIN;
178         } else if (ActionType.START.equals(action)) {
179             return CSpaceAction.START;
180         } else if (ActionType.STOP.equals(action)) {
181             return CSpaceAction.STOP;
182         }
183         throw new IllegalArgumentException("action = " + action.toString());
184     }
185
186 }