]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/internal/CurrentUser.java
Move auth related widgets to CMS
[lgpl/argeo-commons.git] / org.argeo.security.ui / src / org / argeo / security / ui / internal / CurrentUser.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.security.ui.internal;
17
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import org.springframework.security.core.Authentication;
23 import org.springframework.security.core.GrantedAuthority;
24 import org.springframework.security.core.context.SecurityContextHolder;
25
26 /**
27 * Retrieves information about the current user. Not an API, can change without
28 * notice.
29 */
30 public class CurrentUser {
31 // public final static String getUsername() {
32 // Subject subject = getSubject();
33 // if (subject == null)
34 // return null;
35 // Principal principal = subject.getPrincipals().iterator().next();
36 // return principal.getName();
37 //
38 // }
39
40 public final static String getUsername() {
41 return getAuthentication().getName();
42 }
43
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());
49 }
50 return Collections.unmodifiableSet(roles);
51 }
52
53 public final static Authentication getAuthentication() {
54 return SecurityContextHolder.getContext().getAuthentication();
55 }
56
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;
64 // }
65 // throw new ArgeoException("No authentication found");
66 // }
67
68 // public final static Subject getSubject() {
69 // Subject subject = Subject.getSubject(AccessController.getContext());
70 // if (subject == null)
71 // throw new ArgeoException("Not authenticated.");
72 // return subject;
73 // }
74 }