]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2d3ef77f6334aeda6e0444cc3e7d453cfed539d2
[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.authentication.spring;
25
26 import org.collectionspace.authentication.AuthN;
27 import org.collectionspace.authentication.CSpaceTenant;
28 import org.collectionspace.authentication.CSpaceUser;
29 import org.collectionspace.authentication.spi.AuthNContext;
30 import org.springframework.security.core.Authentication;
31 import org.springframework.security.core.context.SecurityContextHolder;
32
33 /**
34  * Utilities for accessing the Spring Security authentication context.
35  */
36 public class SpringAuthNContext implements AuthNContext {
37
38     /**
39      * Returns the username of the authenticated user.
40      * 
41      * @return the username
42      */
43     @Override
44         public String getUserId() {
45         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
46         
47         if (authToken == null) {
48             return AuthN.ANONYMOUS_USER;
49         }
50         
51         return authToken.getName();
52     }
53
54     /**
55      * Returns the authenticated CSpaceUser user.
56      * 
57      * @return the user
58      */
59     @Override
60         public CSpaceUser getUser() {
61         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
62         Object principal = authentication.getPrincipal();
63         
64         CSpaceUser user = null;
65         if (principal instanceof CSpaceUser ) {
66                 user = (CSpaceUser) principal;
67         }
68         
69         return user;
70     }
71
72     /**
73      * Returns the id of the primary tenant associated with the authenticated user.
74      * 
75      * @return the tenant id
76      */
77     @Override
78         public String getCurrentTenantId() {
79         String result = null;
80         
81         CSpaceUser cspaceUser = getUser();
82         if (cspaceUser != null) {
83             result = getCurrentTenant().getId();
84         } else {
85                 String username = getUserId();        
86                 if (username.equals(AuthN.ANONYMOUS_USER)) {
87                     result = AuthN.ANONYMOUS_TENANT_ID;
88                 } else if (username.equals(AuthN.SPRING_ADMIN_USER)) {
89                     result = AuthN.ADMIN_TENANT_ID;
90                 }
91         }
92         
93         return result;
94     }
95
96     /**
97      * Returns the name of the primary tenant associated with the authenticated user.
98      * 
99      * @return the tenant name
100      */
101     @Override
102         public String getCurrentTenantName() {
103         if (getUserId().equals(AuthN.ANONYMOUS_USER)) {
104             return AuthN.ANONYMOUS_TENANT_NAME;
105         }
106
107         return getCurrentTenant().getName();
108     }
109
110     /**
111      * Returns the primary tenant associated with the authenticated user.
112      * 
113      * @return the tenant
114      */
115     @Override
116         public CSpaceTenant getCurrentTenant() {
117         return getUser().getPrimaryTenant();
118     }
119 }