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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.authentication.spring;
26 import java.util.ArrayList;
27 import java.util.List;
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;
36 * Utilities for accessing the Spring Security authentication context.
38 public class SpringAuthNContext implements AuthNContext {
41 * Returns the username of the authenticated user.
43 * @return the username
45 public String getUserId() {
46 String result = ANONYMOUS_USER;
48 Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
50 if (authToken != null) {
51 result = authToken.getName();
58 * Returns the authenticated user.
62 public CSpaceUser getUser() {
63 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
64 Object principal = authentication.getPrincipal();
65 CSpaceUser user = (CSpaceUser) principal;
71 * Returns the id of the primary tenant associated with the authenticated user.
73 * @return the tenant id
75 public String getCurrentTenantId() {
76 String username = getUserId();
78 if (username.equals(ANONYMOUS_USER) || username.equals(SPRING_ADMIN_USER)) {
79 return ANONYMOUS_TENANT_ID;
82 return getCurrentTenant().getId();
86 * Returns the name of the primary tenant associated with the authenticated user.
88 * @return the tenant name
90 public String getCurrentTenantName() {
91 if (getUserId().equals(ANONYMOUS_USER)) {
92 return ANONYMOUS_TENANT_NAME;
95 return getCurrentTenant().getName();
99 * Returns the primary tenant associated with the authenticated user.
103 public CSpaceTenant getCurrentTenant() {
104 List<CSpaceTenant> tenants = getTenants();
106 if (tenants.size() < 1) {
107 throw new IllegalStateException("No tenant associated with user " + getUserId());
110 return tenants.get(0);
114 * Returns all tenants associated with the authenticated user.
116 * @return a list of tenants
118 public List<CSpaceTenant> getTenants() {
119 return new ArrayList<CSpaceTenant>(getUser().getTenants());
123 * Returns the ids of all tenants associated with the authenticated user.
125 * @return a list of tenant ids
127 public List<String> getTenantIds() {
128 List<CSpaceTenant> tenants = getTenants();
129 List<String> tenantIds = new ArrayList<String>(tenants.size());
131 for (CSpaceTenant tenant : tenants) {
132 tenantIds.add(tenant.getId());