]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
73633d03df59cb27841af57c2077b5e566468f81
[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.generator;
25
26 import java.io.File;
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;
48
49 /**
50  *
51  * @author 
52  */
53 public class AuthorizationGen {
54
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>();
60
61     public void initialize(String tenantBindingFile) throws Exception {
62         TenantBindingConfigReaderImpl tenantBindingConfigReader =
63                 new TenantBindingConfigReaderImpl(null);
64         tenantBindingConfigReader.read(tenantBindingFile);
65         tenantBindings = tenantBindingConfigReader.getTenantBindings();
66     }
67
68     public void createDefaultServicePermissions() {
69         for (String tenantId : tenantBindings.keySet()) {
70             List<Permission> perms = createDefaultServicePermissions(tenantId);
71             permList.addAll(perms);
72         }
73     }
74
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(),
80                     sbinding.getName());
81             apcList.add(accPerm);
82         }
83         return apcList;
84
85     }
86
87     private Permission buildCommonPermission(String tenantId, String resourceName) {
88         String id = UUID.randomUUID().toString();
89         Permission perm = new Permission();
90         perm.setCsid(id);
91         perm.setResourceName(resourceName.toLowerCase());
92         perm.setEffect(EffectType.PERMIT);
93         perm.setTenantId(tenantId);
94         ArrayList<PermissionAction> pas = new ArrayList<PermissionAction>();
95         perm.setActions(pas);
96
97         PermissionAction pa = new PermissionAction();
98         pa.setName(ActionType.CREATE);
99         pas.add(pa);
100         PermissionAction pa1 = new PermissionAction();
101         pa1.setName(ActionType.READ);
102         pas.add(pa1);
103         PermissionAction pa2 = new PermissionAction();
104         pa2.setName(ActionType.UPDATE);
105         pas.add(pa2);
106         PermissionAction pa3 = new PermissionAction();
107         pa3.setName(ActionType.DELETE);
108         pas.add(pa3);
109         PermissionAction pa4 = new PermissionAction();
110         pa4.setName(ActionType.SEARCH);
111         pas.add(pa4);
112         return perm;
113     }
114
115     public List<Permission> getDefaultServicePermissions() {
116         return permList;
117     }
118
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);
124         }
125     }
126
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);
133         }
134         return permRoles;
135     }
136
137     private PermissionRole buildCommonPermissionRoles(String tenantId, String permId,
138             String resName, String roleName) {
139
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);
148
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);
155
156         return pr;
157     }
158
159     public List<PermissionRole> getDefaultServicePermissionRoles() {
160         return permRoleList;
161     }
162
163     public void exportPermissions(String fileName) {
164         PermissionsList pcList = new PermissionsList();
165         pcList.setPermissions(permList);
166         toFile(pcList, PermissionsList.class,
167                 fileName);
168         logger.info("exported permissions to " + fileName);
169     }
170
171     public void exportPermissionRoles(String fileName) {
172         PermissionsRolesList psrsl = new PermissionsRolesList();
173         psrsl.setPermissionRoles(permRoleList);
174         toFile(psrsl, PermissionsRolesList.class,
175                 fileName);
176         logger.info("exported permissions-roles to " + fileName);
177     }
178
179     private void toFile(Object o, Class jaxbClass, String fileName) {
180         File f = new File(fileName);
181         try {
182             JAXBContext jc = JAXBContext.newInstance(jaxbClass);
183             Marshaller m = jc.createMarshaller();
184             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
185                     Boolean.TRUE);
186             m.marshal(o, f);
187         } catch (Exception e) {
188             e.printStackTrace();
189         }
190     }
191 }