]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
20c73ce37e55f0e6f976aa88b80a7431c3a25985
[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.spring;
25
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;
48
49 /**
50  * SpringAuthorizationProvider Spring Security provider
51  * @author 
52  */
53 public class SpringAuthorizationProvider implements CSpaceAuthorizationProvider {
54
55     final Log log = LogFactory.getLog(SpringPermissionEvaluator.class);
56     @Autowired
57     private MutableAclService providerAclService;
58     @Autowired
59     private PermissionEvaluator providerPermissionEvaluator;
60     @Autowired
61     private DataSourceTransactionManager txManager;
62                 @Autowired
63                 private EhCacheBasedAclCache providerAclCache;
64     private SpringPermissionEvaluator permissionEvaluator;
65     private SpringPermissionManager permissionManager;
66     private String version = "1.0";
67
68     public SpringAuthorizationProvider() {
69         permissionManager = new SpringPermissionManager(this);
70         permissionEvaluator = new SpringPermissionEvaluator(this);
71     }
72
73     MutableAclService getProviderAclService() {
74         return providerAclService;
75     }
76
77     public void setProviderAclService(MutableAclService mutableAclService) {
78         this.providerAclService = mutableAclService;
79         if (log.isDebugEnabled()) {
80             log.debug("mutableAclService set");
81         }
82     }
83
84     @Override
85     public String getName() {
86         return this.getClass().getSimpleName();
87     }
88
89     @Override
90     public String getVersion() {
91         return version;
92     }
93
94     PermissionEvaluator getProviderPermissionEvaluator() {
95         return providerPermissionEvaluator;
96     }
97
98     public void setProviderPermissionEvaluator(PermissionEvaluator permEval) {
99         this.providerPermissionEvaluator = permEval;
100         if (log.isDebugEnabled()) {
101             log.debug("permission evaluator set");
102         }
103     }
104
105     @Override
106     public CSpacePermissionEvaluator getPermissionEvaluator() {
107         return permissionEvaluator;
108     }
109
110     @Override
111     public CSpacePermissionManager getPermissionManager() {
112         return permissionManager;
113     }
114
115     static Long getObjectIdentityIdentifier(CSpaceResource res) {
116         return Long.valueOf(res.getId().hashCode());
117     }
118
119     static String getObjectIdentityType(CSpaceResource res) {
120         return res.getType().toString();
121     }
122
123     static ObjectIdentity getObjectIdentity(CSpaceResource res) {
124         return new ObjectIdentityImpl(getObjectIdentityType(res),
125                 getObjectIdentityIdentifier(res));
126     }
127
128     static Sid[] getSids(String[] principals) {
129         ArrayList<Sid> sids = new ArrayList<Sid>();
130         for (String principal : principals) {
131             sids.add(new GrantedAuthoritySid(principal));
132         }
133         return sids.toArray(new Sid[0]);
134     }
135
136     static Permission getPermission(CSpaceAction perm) {
137         switch (perm) {
138             case ADMIN:
139                 return BasePermission.ADMINISTRATION;
140             case CREATE:
141                 return BasePermission.CREATE;
142             case READ:
143             case SEARCH:
144                 return BasePermission.READ;
145             case UPDATE:
146                 return BasePermission.WRITE;
147             case DELETE:
148                 return BasePermission.DELETE;
149         }
150         return null;
151     }
152
153     /**
154      * @return the txManager
155      */
156     DataSourceTransactionManager getTxManager() {
157         return txManager;
158     }
159
160     /**
161      * @param txManager the txManager to set
162      */
163     public void setTxManager(DataSourceTransactionManager txManager) {
164         this.txManager = txManager;
165     }
166
167     /**
168      * @return the providerAclCache
169      */
170     EhCacheBasedAclCache getProviderAclCache() {
171         return providerAclCache;
172     }
173
174     /**
175      * @param providerAclCache the providerAclCache to set
176      */
177     public void setProviderAclCache(EhCacheBasedAclCache providerAclCache) {
178         this.providerAclCache = providerAclCache;
179     }
180
181     /**
182      * clear the ACL Cache associated with the provider
183      */
184     public void clearAclCache() {
185         if(providerAclCache != null) {
186                 providerAclCache.clearCache();
187             if (log.isDebugEnabled()) {
188                 log.debug("Clearing providerAclCache.");
189             }
190         } else {
191             log.error("providerAclCache is NULL!");
192         }
193     }
194
195     TransactionStatus beginTransaction(String name) {
196         DefaultTransactionDefinition def = new DefaultTransactionDefinition();
197         // explicitly setting the transaction name is something that can only be done programmatically
198         def.setName(name);
199         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
200         return getTxManager().getTransaction(def);
201     }
202
203     void rollbackTransaction(TransactionStatus status) {
204         getTxManager().rollback(status);
205     }
206
207     void commitTransaction(TransactionStatus status) {
208         getTxManager().commit(status);
209     }
210 }