]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/SecurityUtils.java
Fix: LDIF parser manages last entry properly.
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / SecurityUtils.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;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.springframework.security.authentication.AnonymousAuthenticationToken;
24 import org.springframework.security.core.Authentication;
25 import org.springframework.security.core.GrantedAuthority;
26 import org.springframework.security.core.context.SecurityContext;
27 import org.springframework.security.core.context.SecurityContextHolder;
28
29 /** Static utilities */
30 public final class SecurityUtils {
31 private SecurityUtils() {
32 }
33
34 /** Whether the current thread has the admin role */
35 public static boolean hasCurrentThreadAuthority(String authority) {
36 SecurityContext securityContext = SecurityContextHolder.getContext();
37 if (securityContext != null) {
38 Authentication authentication = securityContext.getAuthentication();
39 if (authentication != null) {
40 for (GrantedAuthority ga : authentication.getAuthorities())
41 if (ga.getAuthority().equals(authority))
42 return true;
43 }
44 }
45 return false;
46 }
47
48 /**
49 * @return the authenticated username or null if not authenticated /
50 * anonymous
51 */
52 public static String getCurrentThreadUsername() {
53 SecurityContext securityContext = SecurityContextHolder.getContext();
54 if (securityContext != null) {
55 Authentication authentication = securityContext.getAuthentication();
56 if (authentication != null) {
57 if (authentication instanceof AnonymousAuthenticationToken) {
58 return null;
59 }
60 return authentication.getName();
61 }
62 }
63 return null;
64 }
65
66 /**
67 * Returns the display name of the user details (by calling toString() on
68 * it)
69 */
70 public static String getUserDetailsDisplayName() {
71 SecurityContext securityContext = SecurityContextHolder.getContext();
72 if (securityContext != null) {
73 Authentication authentication = securityContext.getAuthentication();
74 if (authentication != null) {
75 if (authentication instanceof AnonymousAuthenticationToken) {
76 return null;
77 }
78 Object details = authentication.getDetails();
79 if (details != null)
80 return details.toString();
81 return authentication.getName();
82 }
83 }
84 return null;
85 }
86
87 /**
88 * Converts an array of Spring Security {@link GrantedAuthority} to a
89 * read-only list of strings, for portability and integration
90 */
91 public static List<String> authoritiesToStringList(
92 Collection<? extends GrantedAuthority> authorities) {
93 List<String> lst = new ArrayList<String>();
94 for (GrantedAuthority ga : authorities)
95 lst.add(ga.getAuthority());
96 return Collections.unmodifiableList(lst);
97 }
98 }