]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b103b552a45dfc95a1aff80c4a99d4a37143ff97
[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.RoleValue;
39 import org.collectionspace.services.authorization.SubjectType;
40 import org.collectionspace.services.authorization.URIResourceImpl;
41 import org.collectionspace.services.common.context.ServiceContext;
42 import org.collectionspace.services.common.document.DocumentNotFoundException;
43 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * AuthorizationDelegate delegates permissions management to the underlying authorization
49  * service from the RESTful service layer. The authorization service for example
50  * might manage permissions with the help of a provider (e.g. Spring Security ACL)
51  * @author
52  */
53 public class AuthorizationDelegate {
54
55     private static final Logger logger = LoggerFactory.getLogger(AuthorizationDelegate.class);
56
57     /**
58      * addPermissions add permissions represented given PermissionRole
59      * @param ctx
60      * @param pr permission role
61      * @throws Exception
62      * @see PermissionRole
63      */
64     static void addPermissions(ServiceContext ctx, PermissionRole pr) throws Exception {
65         SubjectType subject = PermissionRoleUtil.getRelationSubject(ctx, pr);
66         AuthZ authz = AuthZ.get();
67         if (subject.equals(SubjectType.ROLE)) {
68             PermissionValue pv = pr.getPermissions().get(0);
69             Permission p = getPermission(pv.getPermissionId());
70             if (p == null) {
71                 String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
72                 logger.error(msg);
73                 throw new DocumentNotFoundException(msg);
74             }
75             CSpaceResource[] resources = getResources(p);
76             String[] roles = getRoles(pr.getRoles());
77             for (CSpaceResource res : resources) {
78                 boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
79                 authz.addPermissions(res, roles, grant);
80             }
81         } else if (SubjectType.PERMISSION.equals(subject)) {
82             RoleValue rv = pr.getRoles().get(0);
83             String[] roles = {rv.getRoleName()};
84             for (PermissionValue pv : pr.getPermissions()) {
85                 Permission p = getPermission(pv.getPermissionId());
86                 if (p == null) {
87                     String msg = "addPermissions: No permission found for id=" + pv.getPermissionId();
88                     logger.error(msg);
89                     continue;
90                 }
91                 CSpaceResource[] resources = getResources(p);
92                 for (CSpaceResource res : resources) {
93                     boolean grant = p.getEffect().equals(EffectType.PERMIT) ? true : false;
94                     authz.addPermissions(res, roles, grant);
95                 }
96             }
97         }
98     }
99
100     /**
101      * deletePermissions delete all permissions associated with given permission role
102      * @param ctx
103      * @param pr permissionrole
104      * @throws Exception
105      */
106     static void deletePermissions(ServiceContext ctx, PermissionRole pr)
107             throws Exception {
108         PermissionValue pv = pr.getPermissions().get(0);
109         deletePermissions(pv);
110     }
111
112     /**
113      * deletePermissions delete permissions associated with given PermissionValue
114      * @param pv permission value
115      * @throws Exception
116      * @see PermissionValue
117      */
118     static void deletePermissions(PermissionValue pv)
119             throws Exception {
120         deletePermissions(pv.getPermissionId());
121     }
122
123     /**
124      * deletePermissions delete permissions associated with given permission id
125      * @param permCsid
126      * @throws Exception
127      */
128     static public void deletePermissions(String permCsid) throws Exception {
129         Permission p = getPermission(permCsid);
130         if (p == null) {
131             String msg = "deletePermissions: No permission found for id=" + permCsid;
132             logger.error(msg);
133             throw new DocumentNotFoundException(msg);
134         }
135         CSpaceResource[] resources = getResources(p);
136         AuthZ authz = AuthZ.get();
137
138         for (CSpaceResource res : resources) {
139             try {
140                 authz.deletePermissions(res);
141             } catch (PermissionException pe) {
142                 //perms are created downthere only if roles are related to the permissions
143                 logger.info("no permissions found in authz service provider for "
144                         + "permCsid=" + permCsid + " res=" + res.getId());
145             }
146         }
147     }
148
149     /**
150      * getRoles get roles (string) array from given RoleValue list
151      * @param rvl rolevalue list
152      * @return string array with role names
153      * @see RoleValue
154      */
155     private static String[] getRoles(List<RoleValue> rvl) {
156         List<String> rvls = new ArrayList<String>();
157         for (RoleValue rv : rvl) {
158             //assumption: rolename is relationship metadata is mandatory
159             if (rv.getRoleName() != null) {
160                 rvls.add(rv.getRoleName());
161             }
162         }
163         return rvls.toArray(new String[0]);
164     }
165
166     /**
167      * getResources from given PermissionValue
168      * @param permisison csid
169      * @return array of CSpaceResource
170      * @see PermissionValue
171      * @see CSpaceResource
172      */
173     private static CSpaceResource[] getResources(Permission p) {
174         List<CSpaceResource> rl = new ArrayList<CSpaceResource>();
175
176         for (PermissionAction pa : p.getActions()) {
177
178             CSpaceResource res = new URIResourceImpl(p.getResourceName(),
179                     getAction(pa.getName()));
180             rl.add(res);
181         }
182         return rl.toArray(new CSpaceResource[0]);
183     }
184
185     private static Permission getPermission(String permCsid) {
186         Permission p = (Permission) JpaStorageUtils.getEntity(permCsid,
187                 Permission.class);
188         return p;
189     }
190
191     /**
192      * getAction is a convenience method to get corresponding action for
193      * given ActionType
194      * @param action
195      * @return
196      */
197     public static CSpaceAction getAction(ActionType action) {
198         if (ActionType.CREATE.equals(action)) {
199             return CSpaceAction.CREATE;
200         } else if (ActionType.READ.equals(action)) {
201             return CSpaceAction.READ;
202         } else if (ActionType.UPDATE.equals(action)) {
203             return CSpaceAction.UPDATE;
204         } else if (ActionType.DELETE.equals(action)) {
205             return CSpaceAction.DELETE;
206         } else if (ActionType.SEARCH.equals(action)) {
207             return CSpaceAction.SEARCH;
208         } else if (ActionType.ADMIN.equals(action)) {
209             return CSpaceAction.ADMIN;
210         } else if (ActionType.START.equals(action)) {
211             return CSpaceAction.START;
212         } else if (ActionType.STOP.equals(action)) {
213             return CSpaceAction.STOP;
214         }
215         throw new IllegalArgumentException("action = " + action.toString());
216     }
217 }