]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5afd0f01aa0fff0c99268997a1656300e30a7b7c
[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(SpringAuthorizationProvider.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 res.getHashedId();
117         //return Long.valueOf(res.getId().hashCode());
118     }
119
120     static String getObjectIdentityType(CSpaceResource res) {
121         return res.getType().toString();
122     }
123
124     static ObjectIdentity getObjectIdentity(CSpaceResource res) {
125         return new ObjectIdentityImpl(getObjectIdentityType(res),
126                 getObjectIdentityIdentifier(res));
127     }
128
129     static Sid[] getSids(String[] principals) {
130         ArrayList<Sid> sids = new ArrayList<Sid>();
131         for (String principal : principals) {
132             sids.add(new GrantedAuthoritySid(principal));
133         }
134         return sids.toArray(new Sid[0]);
135     }
136
137     static Permission getPermission(CSpaceAction perm) {
138         switch (perm) {
139             case ADMIN:
140                 return BasePermission.ADMINISTRATION;
141             case CREATE:
142                 return BasePermission.CREATE;
143             case READ:
144             case SEARCH:
145                 return BasePermission.READ;
146             case UPDATE:
147                 return BasePermission.WRITE;
148             case DELETE:
149                 return BasePermission.DELETE;
150         }
151         return null;
152     }
153
154     /**
155      * @return the txManager
156      */
157     DataSourceTransactionManager getTxManager() {
158         return txManager;
159     }
160
161     /**
162      * @param txManager the txManager to set
163      */
164     public void setTxManager(DataSourceTransactionManager txManager) {
165         this.txManager = txManager;
166     }
167
168     /**
169      * @return the providerAclCache
170      */
171     EhCacheBasedAclCache getProviderAclCache() {
172         return providerAclCache;
173     }
174
175     /**
176      * @param providerAclCache the providerAclCache to set
177      */
178     public void setProviderAclCache(EhCacheBasedAclCache providerAclCache) {
179         this.providerAclCache = providerAclCache;
180     }
181
182     /**
183      * clear the ACL Cache associated with the provider
184      */
185     @Override
186         public void clearAclCache() {
187         if(providerAclCache != null) {
188                 providerAclCache.clearCache();
189             if (log.isDebugEnabled()) {
190                 log.debug("Clearing providerAclCache.");
191             }
192         } else {
193             log.error("providerAclCache is NULL!");
194         }
195     }
196
197     @Override
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
201         def.setName(name);
202         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
203         return getTxManager().getTransaction(def);
204     }
205
206     @Override
207     public void rollbackTransaction(TransactionStatus status) {
208         getTxManager().rollback(status);
209     }
210
211     @Override
212     public void commitTransaction(TransactionStatus status) {
213         getTxManager().commit(status);
214     }
215 }