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 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;
33 * Utilities for accessing the Spring Security authentication context.
35 public class SpringAuthNContext implements AuthNContext {
38 * Returns the username of the authenticated user.
40 * @return the username
42 public String getUserId() {
43 Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
45 if (authToken == null) {
46 return ANONYMOUS_USER;
49 return authToken.getName();
53 * Returns the authenticated user.
57 public CSpaceUser getUser() {
58 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
59 Object principal = authentication.getPrincipal();
60 CSpaceUser user = (CSpaceUser) principal;
66 * Returns the id of the primary tenant associated with the authenticated user.
68 * @return the tenant id
70 public String getCurrentTenantId() {
71 String username = getUserId();
73 if (username.equals(ANONYMOUS_USER) || username.equals(SPRING_ADMIN_USER)) {
74 return ANONYMOUS_TENANT_ID;
77 return getCurrentTenant().getId();
81 * Returns the name of the primary tenant associated with the authenticated user.
83 * @return the tenant name
85 public String getCurrentTenantName() {
86 if (getUserId().equals(ANONYMOUS_USER)) {
87 return ANONYMOUS_TENANT_NAME;
90 return getCurrentTenant().getName();
94 * Returns the primary tenant associated with the authenticated user.
98 public CSpaceTenant getCurrentTenant() {
99 return getUser().getPrimaryTenant();