]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
365716a1121eaaea8cfae3ed423efc0d0240e8ff
[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 2009 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.HashMap;
27 import java.util.List;
28 import org.collectionspace.services.authorization.PermissionRole;
29 import org.collectionspace.services.authorization.PermissionRoleRel;
30 import org.collectionspace.services.authorization.PermissionValue;
31 import org.collectionspace.services.authorization.RoleValue;
32 import org.collectionspace.services.authorization.SubjectType;
33 import org.collectionspace.services.common.context.ServiceContext;
34 import org.collectionspace.services.common.context.ServiceContextProperties;
35 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
36
37 /**
38  *
39  * @author 
40  */
41 public class PermissionRoleUtil {
42
43     static SubjectType getRelationSubject(ServiceContext ctx) {
44         Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
45         if (o == null) {
46             throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
47                     + " property is missing in context "
48                     + ctx.toString());
49         }
50         return (SubjectType) o;
51     }
52
53     static SubjectType getRelationSubject(ServiceContext ctx, PermissionRole pr) {
54         SubjectType subject = pr.getSubject();
55         if (subject == null) {
56             //it is not required to give subject as URI determines the subject
57             subject = getRelationSubject(ctx);
58         }
59         return subject;
60     }
61
62     /**
63      * buildPermissionRoleRel builds persistent relationship entities from given
64      * permissionrole
65      * @param pr permissionrole
66      * @param subject
67      * @param prrl persistent entities built are inserted into this list
68      */
69     static public void buildPermissionRoleRel(PermissionRole pr, SubjectType subject, List<PermissionRoleRel> prrl) {
70
71         if (subject.equals(SubjectType.ROLE)) {
72             //FIXME: potential index out of bounds exception...negative test needed
73             PermissionValue pv = pr.getPermissions().get(0);
74             for (RoleValue rv : pr.getRoles()) {
75                 PermissionRoleRel prr = buildPermissonRoleRel(pv, rv);
76                 prrl.add(prr);
77             }
78         } else if (SubjectType.PERMISSION.equals(subject)) {
79             //FIXME: potential index out of bounds exception...negative test needed
80             RoleValue rv = pr.getRoles().get(0);
81             for (PermissionValue pv : pr.getPermissions()) {
82                 PermissionRoleRel prr = buildPermissonRoleRel(pv, rv);
83                 prrl.add(prr);
84             }
85         }
86     }
87
88     static private PermissionRoleRel buildPermissonRoleRel(PermissionValue pv, RoleValue rv) {
89         PermissionRoleRel prr = new PermissionRoleRel();
90         prr.setPermissionId(pv.getPermissionId());
91         prr.setPermissionResource(pv.getResourceName());
92         prr.setActionGroup(pv.getActionGroup());
93         prr.setRoleId(rv.getRoleId());
94         prr.setRoleName(rv.getRoleName());
95         return prr;
96     }
97
98     static boolean isInvalidTenant(String tenantId, StringBuilder msgBldr) {
99         boolean invalid = false;
100
101         if (tenantId == null || tenantId.isEmpty()) {
102             invalid = true;
103             msgBldr.append("\n tenant : tenantId is missing");
104         }
105         String whereClause = "where id = :id";
106         HashMap<String, Object> params = new HashMap<String, Object>();
107         params.put("id", tenantId);
108
109         Object tenantFound = JpaStorageUtils.getEntity(
110                 "org.collectionspace.services.account.Tenant", whereClause, params);
111         if (tenantFound == null) {
112             invalid = true;
113             msgBldr.append("\n tenant : tenantId=" + tenantId
114                     + " not found");
115         }
116         return invalid;
117     }
118 }