2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.security
.ui
.internal
;
18 import java
.util
.Collections
;
19 import java
.util
.HashSet
;
22 import org
.springframework
.security
.core
.Authentication
;
23 import org
.springframework
.security
.core
.GrantedAuthority
;
24 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
27 * Retrieves information about the current user. Not an API, can change without
30 public class CurrentUser
{
31 // public final static String getUsername() {
32 // Subject subject = getSubject();
33 // if (subject == null)
35 // Principal principal = subject.getPrincipals().iterator().next();
36 // return principal.getName();
40 public final static String
getUsername() {
41 return getAuthentication().getName();
44 public final static Set
<String
> roles() {
45 Set
<String
> roles
= Collections
.synchronizedSet(new HashSet
<String
>());
46 Authentication authentication
= getAuthentication();
47 for (GrantedAuthority ga
: authentication
.getAuthorities()) {
48 roles
.add(ga
.getAuthority());
50 return Collections
.unmodifiableSet(roles
);
53 public final static Authentication
getAuthentication() {
54 return SecurityContextHolder
.getContext().getAuthentication();
57 // public final static Authentication getAuthentication() {
58 // Set<Authentication> authens = getSubject().getPrincipals(
59 // Authentication.class);
60 // if (authens != null && !authens.isEmpty()) {
61 // Principal principal = authens.iterator().next();
62 // Authentication authentication = (Authentication) principal;
63 // return authentication;
65 // throw new ArgeoException("No authentication found");
68 // public final static Subject getSubject() {
69 // Subject subject = Subject.getSubject(AccessController.getContext());
70 // if (subject == null)
71 // throw new ArgeoException("Not authenticated.");