]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ArgeoUserDetails.java
eac1d3706a13d2472bc4010562eb7e12713b89f5
[lgpl/argeo-commons.git] / ArgeoUserDetails.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.security.core;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23
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;
35
36 @Deprecated
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);
40
41 private final Map<String, UserNature> userNatures;
42 private final List<String> roles;
43
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);
49
50 // Roles
51 this.roles = Collections.unmodifiableList(addAuthoritiesToRoles(
52 getAuthorities(), new ArrayList<String>()));
53 }
54
55 public ArgeoUserDetails(ArgeoUser argeoUser) {
56 this(argeoUser.getUsername(), argeoUser.getUserNatures(), argeoUser
57 .getPassword(), rolesToAuthorities(argeoUser.getRoles()));
58 }
59
60 public Map<String, UserNature> getUserNatures() {
61 return userNatures;
62 }
63
64 public void updateUserNatures(Map<String, UserNature> userNaturesData) {
65 SimpleArgeoUser
66 .updateUserNaturesWithCheck(userNatures, userNaturesData);
67 }
68
69 public List<String> getRoles() {
70 return roles;
71 }
72
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());
78 }
79 return roles;
80 }
81
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
88 + ")");
89 arr[i] = new GrantedAuthorityImpl(role);
90 }
91 return arr;
92 }
93
94 public static SimpleArgeoUser createSimpleArgeoUser(UserDetails userDetails) {
95 if (userDetails instanceof ArgeoUser) {
96 return new SimpleArgeoUser((ArgeoUser) userDetails);
97 } else {
98 SimpleArgeoUser argeoUser = new SimpleArgeoUser();
99 argeoUser.setUsername(userDetails.getUsername());
100 addAuthoritiesToRoles(userDetails.getAuthorities(),
101 argeoUser.getRoles());
102 return argeoUser;
103 }
104 }
105
106 /** Creates an argeo user based on spring authentication */
107 public static ArgeoUser asArgeoUser(Authentication authentication) {
108 if (authentication == null)
109 return null;
110
111 if (authentication.getPrincipal() instanceof ArgeoUser) {
112 return new SimpleArgeoUser(
113 (ArgeoUser) authentication.getPrincipal());
114 } else {
115 SimpleArgeoUser argeoUser = new SimpleArgeoUser();
116 argeoUser.setUsername(authentication.getName());
117 addAuthoritiesToRoles(authentication.getAuthorities(),
118 argeoUser.getRoles());
119 return argeoUser;
120 }
121 }
122
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);
128 return argeoUser;
129 }
130 }