]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
36b50674eda812b79df984b52d8e6ea36605db66
[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 java.util.ArrayList;
27 import java.util.List;
28
29 import org.collectionspace.authentication.CSpaceTenant;
30 import org.collectionspace.authentication.CSpaceUser;
31 import org.collectionspace.authentication.spi.AuthNContext;
32 import org.springframework.security.core.Authentication;
33 import org.springframework.security.core.context.SecurityContextHolder;
34
35 /**
36  * Utilities for accessing the Spring Security authentication context.
37  */
38 public class SpringAuthNContext implements AuthNContext {
39
40     /**
41      * Returns the username of the authenticated user.
42      * 
43      * @return the username
44      */
45     public String getUserId() {
46         String result = ANONYMOUS_USER;
47         
48         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
49         
50         if (authToken != null) {
51             result = authToken.getName();
52         }
53         
54         return result;
55     }
56
57     /**
58      * Returns the authenticated user.
59      * 
60      * @return the user
61      */
62     public CSpaceUser getUser() {
63         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
64         Object principal = authentication.getPrincipal();
65         CSpaceUser user = (CSpaceUser) principal;
66         
67         return user;
68     }
69
70     /**
71      * Returns the id of the primary tenant associated with the authenticated user.
72      * 
73      * @return the tenant id
74      */
75     public String getCurrentTenantId() {
76         String username = getUserId();
77         
78         if (username.equals(ANONYMOUS_USER) || username.equals(SPRING_ADMIN_USER)) {
79             return ANONYMOUS_TENANT_ID;
80         }
81
82         return getCurrentTenant().getId();
83     }
84
85     /**
86      * Returns the name of the primary tenant associated with the authenticated user.
87      * 
88      * @return the tenant name
89      */
90     public String getCurrentTenantName() {
91         if (getUserId().equals(ANONYMOUS_USER)) {
92             return ANONYMOUS_TENANT_NAME;
93         }
94
95         return getCurrentTenant().getName();
96     }
97
98     /**
99      * Returns the primary tenant associated with the authenticated user.
100      * 
101      * @return the tenant
102      */
103     public CSpaceTenant getCurrentTenant() {
104         List<CSpaceTenant> tenants = getTenants();
105         
106         if (tenants.size() < 1) {
107             throw new IllegalStateException("No tenant associated with user " + getUserId());
108         }
109         
110         return tenants.get(0);
111     }
112
113     /**
114      * Returns all tenants associated with the authenticated user.
115      * 
116      * @return a list of tenants
117      */
118     public List<CSpaceTenant> getTenants() {
119         return new ArrayList<CSpaceTenant>(getUser().getTenants());
120     }
121
122     /**
123      * Returns the ids of all tenants associated with the authenticated user.
124      * 
125      * @return a list of tenant ids
126      */
127     public List<String> getTenantIds() {
128         List<CSpaceTenant> tenants = getTenants();
129         List<String> tenantIds = new ArrayList<String>(tenants.size());
130         
131         for (CSpaceTenant tenant : tenants) {
132             tenantIds.add(tenant.getId());
133         }
134         
135         return tenantIds;
136     }
137 }