]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e8fce22f92728434227122fc6a3a32e28da842fa
[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.List;
27 import java.io.Serializable;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.collectionspace.services.authorization.CSpaceAction;
31 import org.collectionspace.services.authorization.spi.CSpacePermissionEvaluator;
32
33 import org.collectionspace.services.authorization.CSpaceResource;
34 import org.springframework.security.access.PermissionEvaluator;
35 import org.springframework.security.acls.model.Permission;
36 import org.springframework.security.core.Authentication;
37 import org.springframework.security.core.GrantedAuthority;
38 import org.springframework.security.core.context.SecurityContextHolder;
39
40 /**
41  * SpringPermissionEvaluator evaluates permissions in Spring Security
42  * @author 
43  */
44 public class SpringPermissionEvaluator implements CSpacePermissionEvaluator {
45
46     final Log log = LogFactory.getLog(SpringPermissionEvaluator.class);  //FIXEME: REM - Use SLF4J interfaces instead of directly using Apache Commons Logging.
47     private SpringAuthorizationProvider provider;
48
49     SpringPermissionEvaluator(SpringAuthorizationProvider provider) {
50         this.provider = provider;
51     }
52
53     @Override
54     public boolean hasPermission(CSpaceResource res, CSpaceAction action) {
55         Permission perm = SpringAuthorizationProvider.getPermission(action);
56         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
57         Serializable objectIdId = SpringAuthorizationProvider.getObjectIdentityIdentifier(res);
58         String objectIdType = SpringAuthorizationProvider.getObjectIdentityType(res);
59         PermissionEvaluator eval = provider.getProviderPermissionEvaluator();
60         
61         debug(res, authToken, objectIdId, objectIdType, perm);
62         return eval.hasPermission(authToken,
63                 objectIdId, objectIdType, perm);
64     }
65     
66     private void debug(CSpaceResource res,
67                 Authentication authToken,
68                 Serializable objectIdId,
69                 String objectIdType,
70                 Permission perm) {
71         if (log.isTraceEnabled() == true) {
72                 log.debug(this.getClass().getCanonicalName() + ":" + this);
73                 String resourceTarget = "[" + res.getId() + "]" + " | " +
74                                 "[" + "objectIdId: " + objectIdType + "(" + objectIdId + ")]";
75                 System.out.println("PERMISSION CHECK FOR: " + resourceTarget);
76                 System.out.println("\tPrincipal: " + authToken.getName() +
77                                 "\tTenant ID: " + res.getTenantId());
78                 System.out.println("\tRoles: " + authToken.getAuthorities());
79                 System.out.println("\tPermission Mask: " + perm.getMask() +
80                                 " - Permission Pattern: " + perm.getPattern());
81                 System.out.println("");
82         }
83     }
84 }