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.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;
34 * Utilities for accessing the Spring Security authentication context.
36 public class SpringAuthNContext implements AuthNContext {
39 * Returns the username of the authenticated user.
41 * @return the username
44 public String getUserId() {
45 Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
47 if (authToken == null) {
48 return AuthN.ANONYMOUS_USER;
51 return authToken.getName();
55 * Returns the authenticated CSpaceUser user.
60 public CSpaceUser getUser() {
61 CSpaceUser result = null;
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;
76 * Returns the id of the primary tenant associated with the authenticated user.
78 * @return the tenant id
81 public String getCurrentTenantId() {
84 CSpaceUser cspaceUser = getUser();
85 if (cspaceUser != null) {
86 result = getCurrentTenant().getId();
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;
100 * Returns the name of the primary tenant associated with the authenticated user.
102 * @return the tenant name
105 public String getCurrentTenantName() {
106 if (getUserId().equals(AuthN.ANONYMOUS_USER)) {
107 return AuthN.ANONYMOUS_TENANT_NAME;
110 return getCurrentTenant().getName();
114 * Returns the primary tenant associated with the authenticated user.
119 public CSpaceTenant getCurrentTenant() {
120 CSpaceTenant result = null;
122 CSpaceUser cspaceUser = getUser();
123 if (cspaceUser != null) {
124 result = getUser().getPrimaryTenant();
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);