]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0b02e44dbedac9a67d5925f7386540d95c85a440
[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.EffectType;
33 import org.collectionspace.services.authorization.Permission;
34 import org.collectionspace.services.authorization.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;
47
48 /**
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)
52  * @author
53  */
54 public class AuthorizationDelegate {
55
56     private static final Logger logger = LoggerFactory.getLogger(AuthorizationDelegate.class);
57
58     /**
59      * addPermissions add permissions represented given PermissionRole
60      * @param ctx
61      * @param pr permission role
62      * @throws Exception
63      * @see PermissionRole
64      */
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.getPermissions().get(0);
70             Permission p = getPermission(pv.getPermissionId());
71             if (p == null) {
72                 String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
73                 logger.error(msg);
74                 throw new DocumentNotFoundException(msg);
75             }
76             CSpaceResource[] resources = getResources(p);
77             String[] roles = getRoles(pr.getRoles());
78             for (CSpaceResource res : resources) {
79                 boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
80                 authz.addPermissions(res, roles, grant);
81             }
82         } else if (SubjectType.PERMISSION.equals(subject)) {
83             RoleValue rv = pr.getRoles().get(0);
84             Role r = getRole(rv.getRoleId());
85             if (r == null) {
86                 String msg = "addPermissions: No role found for id=" + rv.getRoleId();
87                 logger.error(msg);
88                 throw new DocumentNotFoundException(msg);
89             }
90             String[] roles = {rv.getRoleName()};
91             for (PermissionValue pv : pr.getPermissions()) {
92                 Permission p = getPermission(pv.getPermissionId());
93                 if (p == null) {
94                     String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
95                     logger.error(msg);
96                     //TODO: would be nice contiue to still send 400 back
97                     continue;
98                 }
99                 CSpaceResource[] resources = getResources(p);
100                 for (CSpaceResource res : resources) {
101                     boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
102                     authz.addPermissions(res, roles, grant);
103                 }
104             }
105         }
106     }
107
108     /**
109      * deletePermissions delete all permissions associated with given permission role
110      * @param ctx
111      * @param pr permissionrole
112      * @throws Exception
113      */
114     static void deletePermissions(ServiceContext ctx, PermissionRole pr)
115             throws Exception {
116         PermissionValue pv = pr.getPermissions().get(0);
117         deletePermissions(pv);
118     }
119
120     /**
121      * deletePermissions delete permissions associated with given PermissionValue
122      * @param pv permission value
123      * @throws Exception
124      * @see PermissionValue
125      */
126     static void deletePermissions(PermissionValue pv)
127             throws Exception {
128         deletePermissions(pv.getPermissionId());
129     }
130
131     /**
132      * deletePermissions delete permissions associated with given permission id
133      * @param permCsid
134      * @throws Exception
135      */
136     static public void deletePermissions(String permCsid) throws Exception {
137         Permission p = getPermission(permCsid);
138         if (p == null) {
139             String msg = "deletePermissions: No permission found for id=" + permCsid;
140             logger.error(msg);
141             throw new DocumentNotFoundException(msg);
142         }
143         CSpaceResource[] resources = getResources(p);
144         AuthZ authz = AuthZ.get();
145
146         for (CSpaceResource res : resources) {
147             try {
148                 authz.deletePermissions(res);
149             } catch (PermissionException pe) {
150                 //perms are created downthere only if roles are related to the permissions
151                 logger.info("no permissions found in authz service provider for "
152                         + "permCsid=" + permCsid + " res=" + res.getId());
153             }
154         }
155     }
156
157     /**
158      * getRoles get roles (string) array from given RoleValue list
159      * @param rvl rolevalue list
160      * @return string array with role names
161      * @see RoleValue
162      */
163     private static String[] getRoles(List<RoleValue> rvl) {
164         List<String> rvls = new ArrayList<String>();
165         for (RoleValue rv : rvl) {
166             Role r = getRole(rv.getRoleId());
167             if (r == null) {
168                 String msg = "getRoles: No role found for id=" + rv.getRoleId();
169                 logger.error(msg);
170                 //TODO: would be nice contiue to still send 400 back
171                 continue;
172             }
173             rvls.add(r.getRoleName());
174         }
175         return rvls.toArray(new String[0]);
176     }
177
178     /**
179      * getResources from given PermissionValue
180      * @param permisison csid
181      * @return array of CSpaceResource
182      * @see PermissionValue
183      * @see CSpaceResource
184      */
185     private static CSpaceResource[] getResources(Permission p) {
186         List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
187
188         for (PermissionAction pa : p.getActions()) {
189             CSpaceResource res = null;
190             if (p.getTenantId() == null) {
191                 res = new URIResourceImpl(p.getResourceName(),
192                         getAction(pa.getName()));
193             } else {
194                 res = new URIResourceImpl(p.getTenantId(), p.getResourceName(),
195                         getAction(pa.getName()));
196             }
197             rl.add(res);
198         }
199         return rl.toArray(new CSpaceResource[0]);
200     }
201
202     private static Permission getPermission(String permCsid) {
203         Permission p = (Permission) JpaStorageUtils.getEntity(permCsid,
204                 Permission.class);
205         return p;
206     }
207
208     private static Role getRole(String roleCsid) {
209         Role r = (Role) JpaStorageUtils.getEntity(roleCsid,
210                 Role.class);
211         return r;
212     }
213
214     /**
215      * getAction is a convenience method to get corresponding action for
216      * given ActionType
217      * @param action
218      * @return
219      */
220     public static CSpaceAction getAction(ActionType action) {
221         if (ActionType.CREATE.equals(action)) {
222             return CSpaceAction.CREATE;
223         } else if (ActionType.READ.equals(action)) {
224             return CSpaceAction.READ;
225         } else if (ActionType.UPDATE.equals(action)) {
226             return CSpaceAction.UPDATE;
227         } else if (ActionType.DELETE.equals(action)) {
228             return CSpaceAction.DELETE;
229         } else if (ActionType.SEARCH.equals(action)) {
230             return CSpaceAction.SEARCH;
231         } else if (ActionType.ADMIN.equals(action)) {
232             return CSpaceAction.ADMIN;
233         } else if (ActionType.START.equals(action)) {
234             return CSpaceAction.START;
235         } else if (ActionType.STOP.equals(action)) {
236             return CSpaceAction.STOP;
237         }
238         throw new IllegalArgumentException("action = " + action.toString());
239     }
240 }