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.generator;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
32 import java.util.UUID;
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.Marshaller;
35 import org.collectionspace.services.authorization.ActionType;
36 import org.collectionspace.services.authorization.Permission;
37 import org.collectionspace.services.authorization.EffectType;
38 import org.collectionspace.services.authorization.PermissionAction;
39 import org.collectionspace.services.authorization.PermissionRole;
40 import org.collectionspace.services.authorization.PermissionValue;
41 import org.collectionspace.services.authorization.PermissionsList;
42 import org.collectionspace.services.authorization.PermissionsRolesList;
43 import org.collectionspace.services.authorization.RoleValue;
44 import org.collectionspace.services.authorization.SubjectType;
45 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
46 import org.collectionspace.services.common.service.ServiceBindingType;
47 import org.collectionspace.services.common.tenant.TenantBindingType;
53 public class AuthorizationGen {
55 final Logger logger = LoggerFactory.getLogger(AuthorizationGen.class);
56 private List<Permission> permList = new ArrayList<Permission>();
57 private List<PermissionRole> permRoleList = new ArrayList<PermissionRole>();
58 private Hashtable<String, TenantBindingType> tenantBindings =
59 new Hashtable<String, TenantBindingType>();
61 public void initialize(String tenantBindingFile) throws Exception {
62 TenantBindingConfigReaderImpl tenantBindingConfigReader =
63 new TenantBindingConfigReaderImpl(null);
64 tenantBindingConfigReader.read(tenantBindingFile);
65 tenantBindings = tenantBindingConfigReader.getTenantBindings();
68 public void createDefaultServicePermissions() {
69 for (String tenantId : tenantBindings.keySet()) {
70 List<Permission> perms = createDefaultServicePermissions(tenantId);
71 permList.addAll(perms);
75 public List<Permission> createDefaultServicePermissions(String tenantId) {
76 ArrayList<Permission> apcList = new ArrayList<Permission>();
77 TenantBindingType tbinding = tenantBindings.get(tenantId);
78 for (ServiceBindingType sbinding : tbinding.getServiceBindings()) {
79 Permission accPerm = buildCommonPermission(tbinding.getId(),
87 private Permission buildCommonPermission(String tenantId, String resourceName) {
88 String id = UUID.randomUUID().toString();
89 Permission perm = new Permission();
91 perm.setResourceName(resourceName.toLowerCase());
92 perm.setEffect(EffectType.PERMIT);
93 perm.setTenantId(tenantId);
94 ArrayList<PermissionAction> pas = new ArrayList<PermissionAction>();
97 PermissionAction pa = new PermissionAction();
98 pa.setName(ActionType.CREATE);
100 PermissionAction pa1 = new PermissionAction();
101 pa1.setName(ActionType.READ);
103 PermissionAction pa2 = new PermissionAction();
104 pa2.setName(ActionType.UPDATE);
106 PermissionAction pa3 = new PermissionAction();
107 pa3.setName(ActionType.DELETE);
109 PermissionAction pa4 = new PermissionAction();
110 pa4.setName(ActionType.SEARCH);
115 public List<Permission> getDefaultServicePermissions() {
119 public void createDefaultPermissionsRoles(String roleName) {
120 for (Permission p : permList) {
121 PermissionRole permRole = buildCommonPermissionRoles(p.getTenantId(), p.getCsid(),
122 p.getResourceName(), roleName);
123 permRoleList.add(permRole);
127 public List<PermissionRole> createPermissionsRoles(List<Permission> perms, String roleName) {
128 List<PermissionRole> permRoles = new ArrayList<PermissionRole>();
129 for (Permission p : perms) {
130 PermissionRole permRole = buildCommonPermissionRoles(p.getTenantId(), p.getCsid(),
131 p.getResourceName(), roleName);
132 permRoles.add(permRole);
137 private PermissionRole buildCommonPermissionRoles(String tenantId, String permId,
138 String resName, String roleName) {
140 PermissionRole pr = new PermissionRole();
141 pr.setSubject(SubjectType.ROLE);
142 List<PermissionValue> permValues = new ArrayList<PermissionValue>();
143 pr.setPermissions(permValues);
144 PermissionValue permValue = new PermissionValue();
145 permValue.setPermissionId(permId);
146 permValue.setResourceName(resName.toLowerCase());
147 permValues.add(permValue);
149 List<RoleValue> roleValues = new ArrayList<RoleValue>();
150 RoleValue radmin = new RoleValue();
151 radmin.setRoleName(roleName.toUpperCase());
152 radmin.setRoleId(tenantId);
153 roleValues.add(radmin);
154 pr.setRoles(roleValues);
159 public List<PermissionRole> getDefaultServicePermissionRoles() {
163 public void exportPermissions(String fileName) {
164 PermissionsList pcList = new PermissionsList();
165 pcList.setPermissions(permList);
166 toFile(pcList, PermissionsList.class,
168 logger.info("exported permissions to " + fileName);
171 public void exportPermissionRoles(String fileName) {
172 PermissionsRolesList psrsl = new PermissionsRolesList();
173 psrsl.setPermissionRoles(permRoleList);
174 toFile(psrsl, PermissionsRolesList.class,
176 logger.info("exported permissions-roles to " + fileName);
179 private void toFile(Object o, Class jaxbClass, String fileName) {
180 File f = new File(fileName);
182 JAXBContext jc = JAXBContext.newInstance(jaxbClass);
183 Marshaller m = jc.createMarshaller();
184 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
187 } catch (Exception e) {