]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5a766a8baf1b38226cafb0d1151950d646b56b33
[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.importer;
25
26 import java.io.FileInputStream;
27 import java.io.InputStream;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.Unmarshaller;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import org.collectionspace.services.authorization.AuthZ;
39 import org.collectionspace.services.authorization.CSpaceAction;
40 import org.collectionspace.services.authorization.perms.EffectType;
41 import org.collectionspace.services.authorization.perms.Permission;
42 import org.collectionspace.services.authorization.perms.PermissionAction;
43 import org.collectionspace.services.authorization.PermissionException;
44 import org.collectionspace.services.authorization.PermissionRole;
45 import org.collectionspace.services.authorization.perms.PermissionsList;
46 import org.collectionspace.services.authorization.PermissionsRolesList;
47 import org.collectionspace.services.authorization.RoleValue;
48 import org.collectionspace.services.authorization.URIResourceImpl;
49 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
50
51 /**
52  * AuthorizationSeed seeds authorizations (permission, role) into authz provider database
53  * @author 
54  */
55 public class AuthorizationSeed {
56
57     final Logger logger = LoggerFactory.getLogger(AuthorizationSeed.class);
58
59
60     /**
61      * seedPermissions seed permissions from given files
62      * @param permFileName permisison file name
63      * @param permRoleFileName permission role file name
64      * @throws Exception
65      */
66     public void seedPermissions(String permFileName, String permRoleFileName) throws Exception {
67         PermissionsRolesList permRoleList =
68                 (PermissionsRolesList) fromFile(PermissionsRolesList.class,
69                 permRoleFileName);
70         if (logger.isDebugEnabled()) {
71             logger.debug("read permissions-roles from " + permRoleFileName);
72         }
73         PermissionsList permList =
74             (PermissionsList) fromFile(PermissionsList.class,
75             permFileName);
76             if (logger.isDebugEnabled()) {
77                 logger.debug("read permissions from " + permFileName);
78             }
79
80         seedPermissions(permList, permRoleList);
81     }
82
83     /**
84      * seedPermissions seed permissions from given permisison and permission role lists
85      * @param permList
86      * @param permRoleList
87      * @throws Exception
88      */
89     public void seedPermissions(PermissionsList permList, PermissionsRolesList permRoleList)
90             throws Exception {
91         
92         seedPermissions(permList.getPermission(), permRoleList.getPermissionRole());
93     }
94     
95     /**
96      * seedPermissions seed permissions from given permisison and permission role lists
97      * @param permList
98      * @param permRoleList
99      * @throws Exception
100      */
101     public void seedPermissions(List<Permission> permList, List<PermissionRole> permRoleList)
102             throws Exception {
103         for (Permission p : permList) {
104             if (logger.isTraceEnabled()) {
105                 logger.trace("adding permission for res=" + p.getResourceName() +
106                         " for tenant=" + p.getTenantId());
107             }
108             for (PermissionRole pr : permRoleList) {
109                 if (pr.getPermission().get(0).getPermissionId().equals(p.getCsid())) {
110                         AuthorizationCommon.addPermissionsForUri(p, pr);
111                 }
112             }
113         }
114     }
115     
116     
117     /**
118      * getAction is a convenience method to get corresponding action for
119      * given ActionType
120      * @param action
121      * @return
122      *
123     private CSpaceAction getAction(ActionType action) {
124         if (ActionType.CREATE.equals(action)) {
125             return CSpaceAction.CREATE;
126         } else if (ActionType.READ.equals(action)) {
127             return CSpaceAction.READ;
128         } else if (ActionType.UPDATE.equals(action)) {
129             return CSpaceAction.UPDATE;
130         } else if (ActionType.DELETE.equals(action)) {
131             return CSpaceAction.DELETE;
132         } else if (ActionType.SEARCH.equals(action)) {
133             return CSpaceAction.SEARCH;
134         } else if (ActionType.ADMIN.equals(action)) {
135             return CSpaceAction.ADMIN;
136         } else if (ActionType.START.equals(action)) {
137             return CSpaceAction.START;
138         } else if (ActionType.STOP.equals(action)) {
139             return CSpaceAction.STOP;
140         }
141         throw new IllegalArgumentException("action = " + action.toString());
142     }
143     */
144
145     static Object fromFile(Class jaxbClass, String fileName) throws Exception {
146         InputStream is = new FileInputStream(fileName);
147         try {
148             JAXBContext context = JAXBContext.newInstance(jaxbClass);
149             Unmarshaller unmarshaller = context.createUnmarshaller();
150             //note: setting schema to null will turn validator off
151             unmarshaller.setSchema(null);
152             return jaxbClass.cast(unmarshaller.unmarshal(is));
153         } finally {
154             if (is != null) {
155                 try {
156                     is.close();
157                 } catch (Exception e) {
158                 }
159             }
160         }
161     }
162 }