]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1fa6a01b3bd6d2121bc3539747b2186f1bddf327
[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.springframework.security.acls.model.AlreadyExistsException;
39 import org.collectionspace.services.authorization.AuthZ;
40 import org.collectionspace.services.authorization.CSpaceAction;
41 import org.collectionspace.services.authorization.perms.EffectType;
42 import org.collectionspace.services.authorization.perms.Permission;
43 import org.collectionspace.services.authorization.perms.PermissionAction;
44 import org.collectionspace.services.authorization.PermissionException;
45 import org.collectionspace.services.authorization.PermissionRole;
46 import org.collectionspace.services.authorization.perms.PermissionsList;
47 import org.collectionspace.services.authorization.PermissionsRolesList;
48 import org.collectionspace.services.authorization.RoleValue;
49 import org.collectionspace.services.authorization.URIResourceImpl;
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                     addPermissionsForUri(p, pr);
111                 }
112             }
113         }
114     }
115     
116     /**
117      * addPermissionsForUri add permissions from given permission configuration
118      * with assumption that resource is of type URI
119      * @param permission configuration
120      */
121     private void addPermissionsForUri(Permission perm,
122             PermissionRole permRole) throws PermissionException {
123         List<String> principals = new ArrayList<String>();
124         if (!perm.getCsid().equals(permRole.getPermission().get(0).getPermissionId())) {
125             throw new IllegalArgumentException("permission ids do not"
126                     + " match for role=" + permRole.getRole().get(0).getRoleName()
127                     + " with permissionId=" + permRole.getPermission().get(0).getPermissionId()
128                     + " for permission with csid=" + perm.getCsid());
129         }
130         for (RoleValue roleValue : permRole.getRole()) {
131             principals.add(roleValue.getRoleName());
132         }
133         List<PermissionAction> permActions = perm.getAction();
134         for (PermissionAction permAction : permActions) {
135                 try {
136                     CSpaceAction action = URIResourceImpl.getAction(permAction.getName());
137                     URIResourceImpl uriRes = new URIResourceImpl(perm.getTenantId(),
138                             perm.getResourceName(), action);
139                     boolean grant = perm.getEffect().equals(EffectType.PERMIT) ? true : false;
140                     AuthZ.get().addPermissions(uriRes, principals.toArray(new String[0]), grant);
141                 } catch (PermissionException e) {
142                         //
143                         // Only throw the exception if it is *not* an already-exists exception
144                         //
145                         if (e.getCause() instanceof AlreadyExistsException == false) {
146                                 throw e;
147                         }
148                 }
149         }
150     }
151
152     /**
153      * getAction is a convenience method to get corresponding action for
154      * given ActionType
155      * @param action
156      * @return
157      *
158     private CSpaceAction getAction(ActionType action) {
159         if (ActionType.CREATE.equals(action)) {
160             return CSpaceAction.CREATE;
161         } else if (ActionType.READ.equals(action)) {
162             return CSpaceAction.READ;
163         } else if (ActionType.UPDATE.equals(action)) {
164             return CSpaceAction.UPDATE;
165         } else if (ActionType.DELETE.equals(action)) {
166             return CSpaceAction.DELETE;
167         } else if (ActionType.SEARCH.equals(action)) {
168             return CSpaceAction.SEARCH;
169         } else if (ActionType.ADMIN.equals(action)) {
170             return CSpaceAction.ADMIN;
171         } else if (ActionType.START.equals(action)) {
172             return CSpaceAction.START;
173         } else if (ActionType.STOP.equals(action)) {
174             return CSpaceAction.STOP;
175         }
176         throw new IllegalArgumentException("action = " + action.toString());
177     }
178     */
179
180     static Object fromFile(Class jaxbClass, String fileName) throws Exception {
181         InputStream is = new FileInputStream(fileName);
182         try {
183             JAXBContext context = JAXBContext.newInstance(jaxbClass);
184             Unmarshaller unmarshaller = context.createUnmarshaller();
185             //note: setting schema to null will turn validator off
186             unmarshaller.setSchema(null);
187             return jaxbClass.cast(unmarshaller.unmarshal(is));
188         } finally {
189             if (is != null) {
190                 try {
191                     is.close();
192                 } catch (Exception e) {
193                 }
194             }
195         }
196     }
197 }