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
;
36 public class ArgeoUserDetails
extends User
implements ArgeoUser
{
37 private static final long serialVersionUID
= 1L;
38 private final static Log log
= LogFactory
.getLog(ArgeoUserDetails
.class);
40 private final Map
<String
, UserNature
> userNatures
;
41 private final List
<String
> roles
;
43 public ArgeoUserDetails(String username
, Map
<String
, UserNature
> userNatures
,
44 String password
, GrantedAuthority
[] authorities
)
45 throws IllegalArgumentException
{
46 super(username
, password
, true, true, true, true, authorities
);
47 this.userNatures
= Collections
.unmodifiableMap(userNatures
);
50 this.roles
= Collections
.unmodifiableList(addAuthoritiesToRoles(
51 getAuthorities(), new ArrayList
<String
>()));
54 public ArgeoUserDetails(ArgeoUser argeoUser
) {
55 this(argeoUser
.getUsername(), argeoUser
.getUserNatures(), argeoUser
56 .getPassword(), rolesToAuthorities(argeoUser
.getRoles()));
59 public Map
<String
, UserNature
> getUserNatures() {
63 public void updateUserNatures(Map
<String
, UserNature
> userNaturesData
) {
64 UserNature
.updateUserNaturesWithCheck(userNatures
, userNaturesData
);
67 public List
<String
> getRoles() {
71 /** The provided list, for chaining using {@link Collections} */
72 protected static List
<String
> addAuthoritiesToRoles(
73 GrantedAuthority
[] authorities
, List
<String
> roles
) {
74 for (GrantedAuthority authority
: authorities
) {
75 roles
.add(authority
.getAuthority());
80 protected static GrantedAuthority
[] rolesToAuthorities(List
<String
> roles
) {
81 GrantedAuthority
[] arr
= new GrantedAuthority
[roles
.size()];
82 for (int i
= 0; i
< roles
.size(); i
++) {
83 String role
= roles
.get(i
);
84 if (log
.isTraceEnabled())
85 log
.debug("Convert role " + role
+ " to authority (i=" + i
87 arr
[i
] = new GrantedAuthorityImpl(role
);
92 public static SimpleArgeoUser
createSimpleArgeoUser(UserDetails userDetails
) {
93 if (userDetails
instanceof ArgeoUser
) {
94 return new SimpleArgeoUser((ArgeoUser
) userDetails
);
96 SimpleArgeoUser argeoUser
= new SimpleArgeoUser();
97 argeoUser
.setUsername(userDetails
.getUsername());
98 addAuthoritiesToRoles(userDetails
.getAuthorities(),
99 argeoUser
.getRoles());
104 /** Creates an argeo user based on spring authentication */
105 public static ArgeoUser
asArgeoUser(Authentication authentication
) {
106 if (authentication
== null)
109 if (authentication
.getPrincipal() instanceof ArgeoUser
) {
110 return new SimpleArgeoUser(
111 (ArgeoUser
) authentication
.getPrincipal());
113 SimpleArgeoUser argeoUser
= new SimpleArgeoUser();
114 argeoUser
.setUsername(authentication
.getName());
115 addAuthoritiesToRoles(authentication
.getAuthorities(),
116 argeoUser
.getRoles());
121 /** The Spring security context as an argeo user */
122 public static ArgeoUser
securityContextUser() {
123 Authentication authentication
= SecurityContextHolder
.getContext()
124 .getAuthentication();
125 ArgeoUser argeoUser
= ArgeoUserDetails
.asArgeoUser(authentication
);