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