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.importer;
26 import java.io.FileInputStream;
27 import java.io.InputStream;
29 import java.util.List;
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.Unmarshaller;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import org.collectionspace.services.authorization.perms.Permission;
38 import org.collectionspace.services.authorization.PermissionRole;
39 import org.collectionspace.services.authorization.perms.PermissionsList;
40 import org.collectionspace.services.authorization.PermissionsRolesList;
41 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
42 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
45 * AuthorizationSeed seeds authorizations (permission, role) into authz provider database
48 public class AuthorizationSeed {
50 final Logger logger = LoggerFactory.getLogger(AuthorizationSeed.class);
54 * seedPermissions seed permissions from given files
55 * @param permFileName permisison file name
56 * @param permRoleFileName permission role file name
59 public void seedPermissions(JPATransactionContext jpaTransactionContext, String permFileName, String permRoleFileName) throws Exception {
60 PermissionsRolesList permRoleList =
61 (PermissionsRolesList) fromFile(PermissionsRolesList.class,
63 if (logger.isDebugEnabled()) {
64 logger.debug("read permissions-roles from " + permRoleFileName);
66 PermissionsList permList =
67 (PermissionsList) fromFile(PermissionsList.class,
69 if (logger.isDebugEnabled()) {
70 logger.debug("read permissions from " + permFileName);
73 seedPermissions(jpaTransactionContext, permList, permRoleList);
77 * seedPermissions seed permissions from given permisison and permission role lists
82 public void seedPermissions(JPATransactionContext jpaTransactionContext, PermissionsList permList, PermissionsRolesList permRoleList)
85 seedPermissions(jpaTransactionContext, permList.getPermission(), permRoleList.getPermissionRole());
89 * seedPermissions seed permissions from given permisison and permission role lists
94 public void seedPermissions(JPATransactionContext jpaTransactionContext, List<Permission> permList, List<PermissionRole> permRoleList)
96 if (logger.isInfoEnabled() == true) {
97 logger.info("Started seeding Spring Security Tables...");
99 int factor = permRoleList.size();
100 int permsToSeed = permList.size() * factor;
103 for (Permission p : permList) {
104 if (permsSeeded++ % 10 == 0 && logger.isInfoEnabled()) {
105 logger.info(String.format("Seeded %d permissions of %d...", permsSeeded * factor, permsToSeed));
107 for (PermissionRole pr : permRoleList) {
108 if (pr.getPermission().get(0).getPermissionId().equals(p.getCsid())) {
109 AuthorizationCommon.addPermissionsForUri(jpaTransactionContext, p, pr);
114 if (logger.isInfoEnabled() == true) {
115 logger.info("Finished seeding Spring Security Tables.");
120 * getAction is a convenience method to get corresponding action for
125 private CSpaceAction getAction(ActionType action) {
126 if (ActionType.CREATE.equals(action)) {
127 return CSpaceAction.CREATE;
128 } else if (ActionType.READ.equals(action)) {
129 return CSpaceAction.READ;
130 } else if (ActionType.UPDATE.equals(action)) {
131 return CSpaceAction.UPDATE;
132 } else if (ActionType.DELETE.equals(action)) {
133 return CSpaceAction.DELETE;
134 } else if (ActionType.SEARCH.equals(action)) {
135 return CSpaceAction.SEARCH;
136 } else if (ActionType.ADMIN.equals(action)) {
137 return CSpaceAction.ADMIN;
138 } else if (ActionType.START.equals(action)) {
139 return CSpaceAction.START;
140 } else if (ActionType.STOP.equals(action)) {
141 return CSpaceAction.STOP;
143 throw new IllegalArgumentException("action = " + action.toString());
147 static Object fromFile(Class jaxbClass, String fileName) throws Exception {
148 InputStream is = new FileInputStream(fileName);
150 JAXBContext context = JAXBContext.newInstance(jaxbClass);
151 Unmarshaller unmarshaller = context.createUnmarshaller();
152 //note: setting schema to null will turn validator off
153 unmarshaller.setSchema(null);
154 return jaxbClass.cast(unmarshaller.unmarshal(is));
159 } catch (Exception e) {