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.spring;
26 import java.util.ArrayList;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.collectionspace.services.authorization.CSpaceAction;
30 import org.collectionspace.services.authorization.CSpaceResource;
31 import org.collectionspace.services.authorization.spi.CSpaceAuthorizationProvider;
32 import org.collectionspace.services.authorization.spi.CSpacePermissionEvaluator;
33 import org.collectionspace.services.authorization.spi.CSpacePermissionManager;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
36 import org.springframework.security.access.PermissionEvaluator;
37 import org.springframework.security.acls.domain.BasePermission;
38 import org.springframework.security.acls.domain.EhCacheBasedAclCache;
39 import org.springframework.security.acls.domain.GrantedAuthoritySid;
40 import org.springframework.security.acls.domain.ObjectIdentityImpl;
41 import org.springframework.security.acls.model.MutableAclService;
42 import org.springframework.security.acls.model.ObjectIdentity;
43 import org.springframework.security.acls.model.Permission;
44 import org.springframework.security.acls.model.Sid;
45 import org.springframework.transaction.TransactionDefinition;
46 import org.springframework.transaction.TransactionStatus;
47 import org.springframework.transaction.support.DefaultTransactionDefinition;
50 * SpringAuthorizationProvider Spring Security provider
53 public class SpringAuthorizationProvider implements CSpaceAuthorizationProvider {
55 final Log log = LogFactory.getLog(SpringAuthorizationProvider.class);
57 private MutableAclService providerAclService;
59 private PermissionEvaluator providerPermissionEvaluator;
61 private DataSourceTransactionManager txManager;
63 private EhCacheBasedAclCache providerAclCache;
64 private SpringPermissionEvaluator permissionEvaluator;
65 private SpringPermissionManager permissionManager;
66 private String version = "1.0";
68 public SpringAuthorizationProvider() {
69 permissionManager = new SpringPermissionManager(this);
70 permissionEvaluator = new SpringPermissionEvaluator(this);
73 MutableAclService getProviderAclService() {
74 return providerAclService;
77 public void setProviderAclService(MutableAclService mutableAclService) {
78 this.providerAclService = mutableAclService;
79 if (log.isDebugEnabled()) {
80 log.debug("mutableAclService set");
85 public String getName() {
86 return this.getClass().getSimpleName();
90 public String getVersion() {
94 PermissionEvaluator getProviderPermissionEvaluator() {
95 return providerPermissionEvaluator;
98 public void setProviderPermissionEvaluator(PermissionEvaluator permEval) {
99 this.providerPermissionEvaluator = permEval;
100 if (log.isDebugEnabled()) {
101 log.debug("permission evaluator set");
106 public CSpacePermissionEvaluator getPermissionEvaluator() {
107 return permissionEvaluator;
111 public CSpacePermissionManager getPermissionManager() {
112 return permissionManager;
115 static Long getObjectIdentityIdentifier(CSpaceResource res) {
116 return res.getHashedId();
117 //return Long.valueOf(res.getId().hashCode());
120 static String getObjectIdentityType(CSpaceResource res) {
121 return res.getType().toString();
124 static ObjectIdentity getObjectIdentity(CSpaceResource res) {
125 return new ObjectIdentityImpl(getObjectIdentityType(res),
126 getObjectIdentityIdentifier(res));
129 static Sid[] getSids(String[] principals) {
130 ArrayList<Sid> sids = new ArrayList<Sid>();
131 for (String principal : principals) {
132 sids.add(new GrantedAuthoritySid(principal));
134 return sids.toArray(new Sid[0]);
137 static Permission getPermission(CSpaceAction perm) {
140 return BasePermission.ADMINISTRATION;
142 return BasePermission.CREATE;
145 return BasePermission.READ;
147 return BasePermission.WRITE;
149 return BasePermission.DELETE;
155 * @return the txManager
157 DataSourceTransactionManager getTxManager() {
162 * @param txManager the txManager to set
164 public void setTxManager(DataSourceTransactionManager txManager) {
165 this.txManager = txManager;
169 * @return the providerAclCache
171 EhCacheBasedAclCache getProviderAclCache() {
172 return providerAclCache;
176 * @param providerAclCache the providerAclCache to set
178 public void setProviderAclCache(EhCacheBasedAclCache providerAclCache) {
179 this.providerAclCache = providerAclCache;
183 * clear the ACL Cache associated with the provider
186 public void clearAclCache() {
187 if(providerAclCache != null) {
188 providerAclCache.clearCache();
189 if (log.isDebugEnabled()) {
190 log.debug("Clearing providerAclCache.");
193 log.error("providerAclCache is NULL!");
198 public TransactionStatus beginTransaction(String name) {
199 DefaultTransactionDefinition def = new DefaultTransactionDefinition();
200 // explicitly setting the transaction name is something that can only be done programmatically
202 def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
203 return getTxManager().getTransaction(def);
207 public void rollbackTransaction(TransactionStatus status) {
208 getTxManager().rollback(status);
212 public void commitTransaction(TransactionStatus status) {
213 getTxManager().commit(status);