2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.security
.core
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collections
;
21 import java
.util
.List
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.security
.ArgeoUser
;
27 import org
.argeo
.security
.SimpleArgeoUser
;
28 import org
.argeo
.security
.UserNature
;
29 import org
.springframework
.security
.Authentication
;
30 import org
.springframework
.security
.GrantedAuthority
;
31 import org
.springframework
.security
.GrantedAuthorityImpl
;
32 import org
.springframework
.security
.context
.SecurityContextHolder
;
33 import org
.springframework
.security
.userdetails
.User
;
34 import org
.springframework
.security
.userdetails
.UserDetails
;
37 public class ArgeoUserDetails
extends User
implements ArgeoUser
{
38 private static final long serialVersionUID
= 1L;
39 private final static Log log
= LogFactory
.getLog(ArgeoUserDetails
.class);
41 private final Map
<String
, UserNature
> userNatures
;
42 private final List
<String
> roles
;
44 public ArgeoUserDetails(String username
,
45 Map
<String
, UserNature
> userNatures
, String password
,
46 GrantedAuthority
[] authorities
) throws IllegalArgumentException
{
47 super(username
, password
, true, true, true, true, authorities
);
48 this.userNatures
= Collections
.unmodifiableMap(userNatures
);
51 this.roles
= Collections
.unmodifiableList(addAuthoritiesToRoles(
52 getAuthorities(), new ArrayList
<String
>()));
55 public ArgeoUserDetails(ArgeoUser argeoUser
) {
56 this(argeoUser
.getUsername(), argeoUser
.getUserNatures(), argeoUser
57 .getPassword(), rolesToAuthorities(argeoUser
.getRoles()));
60 public Map
<String
, UserNature
> getUserNatures() {
64 public void updateUserNatures(Map
<String
, UserNature
> userNaturesData
) {
66 .updateUserNaturesWithCheck(userNatures
, userNaturesData
);
69 public List
<String
> getRoles() {
73 /** The provided list, for chaining using {@link Collections} */
74 public static List
<String
> addAuthoritiesToRoles(
75 GrantedAuthority
[] authorities
, List
<String
> roles
) {
76 for (GrantedAuthority authority
: authorities
) {
77 roles
.add(authority
.getAuthority());
82 public static GrantedAuthority
[] rolesToAuthorities(List
<String
> roles
) {
83 GrantedAuthority
[] arr
= new GrantedAuthority
[roles
.size()];
84 for (int i
= 0; i
< roles
.size(); i
++) {
85 String role
= roles
.get(i
);
86 if (log
.isTraceEnabled())
87 log
.debug("Convert role " + role
+ " to authority (i=" + i
89 arr
[i
] = new GrantedAuthorityImpl(role
);
94 public static SimpleArgeoUser
createSimpleArgeoUser(UserDetails userDetails
) {
95 if (userDetails
instanceof ArgeoUser
) {
96 return new SimpleArgeoUser((ArgeoUser
) userDetails
);
98 SimpleArgeoUser argeoUser
= new SimpleArgeoUser();
99 argeoUser
.setUsername(userDetails
.getUsername());
100 addAuthoritiesToRoles(userDetails
.getAuthorities(),
101 argeoUser
.getRoles());
106 /** Creates an argeo user based on spring authentication */
107 public static ArgeoUser
asArgeoUser(Authentication authentication
) {
108 if (authentication
== null)
111 if (authentication
.getPrincipal() instanceof ArgeoUser
) {
112 return new SimpleArgeoUser(
113 (ArgeoUser
) authentication
.getPrincipal());
115 SimpleArgeoUser argeoUser
= new SimpleArgeoUser();
116 argeoUser
.setUsername(authentication
.getName());
117 addAuthoritiesToRoles(authentication
.getAuthorities(),
118 argeoUser
.getRoles());
123 /** The Spring security context as an argeo user */
124 public static ArgeoUser
securityContextUser() {
125 Authentication authentication
= SecurityContextHolder
.getContext()
126 .getAuthentication();
127 ArgeoUser argeoUser
= ArgeoUserDetails
.asArgeoUser(authentication
);