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 2009 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.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;
41 public class PermissionRoleUtil {
43 static SubjectType getRelationSubject(ServiceContext ctx) {
44 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
46 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
47 + " property is missing in context "
50 return (SubjectType) o;
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);
63 * buildPermissionRoleRel builds persistent relationship entities from given
65 * @param pr permissionrole
67 * @param prrl persistent entities built are inserted into this list
69 static public void buildPermissionRoleRel(PermissionRole pr, SubjectType subject, List<PermissionRoleRel> prrl) {
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);
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);
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());
98 static boolean isInvalidTenant(String tenantId, StringBuilder msgBldr) {
99 boolean invalid = false;
101 if (tenantId == null || tenantId.isEmpty()) {
103 msgBldr.append("\n tenant : tenantId is missing");
105 String whereClause = "where id = :id";
106 HashMap<String, Object> params = new HashMap<String, Object>();
107 params.put("id", tenantId);
109 Object tenantFound = JpaStorageUtils.getEntity(
110 "org.collectionspace.services.account.Tenant", whereClause, params);
111 if (tenantFound == null) {
113 msgBldr.append("\n tenant : tenantId=" + tenantId