]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c1dc8dd681dec25d0f8b1dae1282bb15981d2ea8
[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         CSpaceUser result = null;
62
63         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
64         Object principal = null;
65         if (authentication != null) {
66             principal = authentication.getPrincipal();
67             if (principal instanceof CSpaceUser ) {
68                 result = (CSpaceUser) principal;
69             }
70         }        
71         
72         return result;
73     }
74
75     /**
76      * Returns the id of the primary tenant associated with the authenticated user.
77      * 
78      * @return the tenant id
79      */
80     @Override
81         public String getCurrentTenantId() {
82         String result = null;
83         
84         CSpaceUser cspaceUser = getUser();
85         if (cspaceUser != null) {
86             result = getCurrentTenant().getId();
87         } else {
88                 String username = getUserId();        
89                 if (username.equals(AuthN.ANONYMOUS_USER)) {
90                     result = AuthN.ANONYMOUS_TENANT_ID;
91                 } else if (username.equals(AuthN.SPRING_ADMIN_USER)) {
92                     result = AuthN.ADMIN_TENANT_ID;
93                 }
94         }
95         
96         return result;
97     }
98
99     /**
100      * Returns the name of the primary tenant associated with the authenticated user.
101      * 
102      * @return the tenant name
103      */
104     @Override
105         public String getCurrentTenantName() {
106         if (getUserId().equals(AuthN.ANONYMOUS_USER)) {
107             return AuthN.ANONYMOUS_TENANT_NAME;
108         }
109
110         return getCurrentTenant().getName();
111     }
112
113     /**
114      * Returns the primary tenant associated with the authenticated user.
115      * 
116      * @return the tenant
117      */
118     @Override
119         public CSpaceTenant getCurrentTenant() {
120         CSpaceTenant result = null;
121         
122         CSpaceUser cspaceUser = getUser();
123         if (cspaceUser != null) {
124                 result = getUser().getPrimaryTenant();
125         } else {
126                 String username = getUserId();
127                 if (username.equals(AuthN.ANONYMOUS_USER)) {
128                     result = new CSpaceTenant(AuthN.ANONYMOUS_TENANT_ID, AuthN.ANONYMOUS_TENANT_NAME);
129                 } else if (username.equals(AuthN.SPRING_ADMIN_USER)) {
130                     result = new CSpaceTenant(AuthN.ADMIN_TENANT_ID, AuthN.ADMIN_TENANT_NAME);
131                 } 
132         }
133         
134         return result;
135     }
136 }