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;
44 * AuthorizationSeed seeds authorizations (permission, role) into authz provider database
47 public class AuthorizationSeed {
49 final Logger logger = LoggerFactory.getLogger(AuthorizationSeed.class);
53 * seedPermissions seed permissions from given files
54 * @param permFileName permisison file name
55 * @param permRoleFileName permission role file name
58 public void seedPermissions(String permFileName, String permRoleFileName) throws Exception {
59 PermissionsRolesList permRoleList =
60 (PermissionsRolesList) fromFile(PermissionsRolesList.class,
62 if (logger.isDebugEnabled()) {
63 logger.debug("read permissions-roles from " + permRoleFileName);
65 PermissionsList permList =
66 (PermissionsList) fromFile(PermissionsList.class,
68 if (logger.isDebugEnabled()) {
69 logger.debug("read permissions from " + permFileName);
72 seedPermissions(permList, permRoleList);
76 * seedPermissions seed permissions from given permisison and permission role lists
81 public void seedPermissions(PermissionsList permList, PermissionsRolesList permRoleList)
84 seedPermissions(permList.getPermission(), permRoleList.getPermissionRole());
88 * seedPermissions seed permissions from given permisison and permission role lists
93 public void seedPermissions(List<Permission> permList, List<PermissionRole> permRoleList)
95 if (logger.isInfoEnabled() == true) {
96 logger.info("Started seeding Spring Security Tables...");
98 int factor = permRoleList.size();
99 int permsToSeed = permList.size() * factor;
102 for (Permission p : permList) {
103 if (permsSeeded++ % 10 == 0 && logger.isInfoEnabled()) {
104 logger.info(String.format("Seeded %d permissions of %d...", permsSeeded * factor, permsToSeed));
106 for (PermissionRole pr : permRoleList) {
107 if (pr.getPermission().get(0).getPermissionId().equals(p.getCsid())) {
108 AuthorizationCommon.addPermissionsForUri(p, pr);
113 if (logger.isInfoEnabled() == true) {
114 logger.info("Finished seeding Spring Security Tables.");
119 * getAction is a convenience method to get corresponding action for
124 private CSpaceAction getAction(ActionType action) {
125 if (ActionType.CREATE.equals(action)) {
126 return CSpaceAction.CREATE;
127 } else if (ActionType.READ.equals(action)) {
128 return CSpaceAction.READ;
129 } else if (ActionType.UPDATE.equals(action)) {
130 return CSpaceAction.UPDATE;
131 } else if (ActionType.DELETE.equals(action)) {
132 return CSpaceAction.DELETE;
133 } else if (ActionType.SEARCH.equals(action)) {
134 return CSpaceAction.SEARCH;
135 } else if (ActionType.ADMIN.equals(action)) {
136 return CSpaceAction.ADMIN;
137 } else if (ActionType.START.equals(action)) {
138 return CSpaceAction.START;
139 } else if (ActionType.STOP.equals(action)) {
140 return CSpaceAction.STOP;
142 throw new IllegalArgumentException("action = " + action.toString());
146 static Object fromFile(Class jaxbClass, String fileName) throws Exception {
147 InputStream is = new FileInputStream(fileName);
149 JAXBContext context = JAXBContext.newInstance(jaxbClass);
150 Unmarshaller unmarshaller = context.createUnmarshaller();
151 //note: setting schema to null will turn validator off
152 unmarshaller.setSchema(null);
153 return jaxbClass.cast(unmarshaller.unmarshal(is));
158 } catch (Exception e) {