]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ArgeoUserDetails.java
1948d1252ae24a8d92cb6d81106d524e875bd507
[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 public class ArgeoUserDetails extends User implements ArgeoUser {
37 private static final long serialVersionUID = 1L;
38 private final static Log log = LogFactory.getLog(ArgeoUserDetails.class);
39
40 private final Map<String, UserNature> userNatures;
41 private final List<String> roles;
42
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);
48
49 // Roles
50 this.roles = Collections.unmodifiableList(addAuthoritiesToRoles(
51 getAuthorities(), new ArrayList<String>()));
52 }
53
54 public ArgeoUserDetails(ArgeoUser argeoUser) {
55 this(argeoUser.getUsername(), argeoUser.getUserNatures(), argeoUser
56 .getPassword(), rolesToAuthorities(argeoUser.getRoles()));
57 }
58
59 public Map<String, UserNature> getUserNatures() {
60 return userNatures;
61 }
62
63 public void updateUserNatures(Map<String, UserNature> userNaturesData) {
64 UserNature.updateUserNaturesWithCheck(userNatures, userNaturesData);
65 }
66
67 public List<String> getRoles() {
68 return roles;
69 }
70
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());
76 }
77 return roles;
78 }
79
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
86 + ")");
87 arr[i] = new GrantedAuthorityImpl(role);
88 }
89 return arr;
90 }
91
92 public static SimpleArgeoUser createSimpleArgeoUser(UserDetails userDetails) {
93 if (userDetails instanceof ArgeoUser) {
94 return new SimpleArgeoUser((ArgeoUser) userDetails);
95 } else {
96 SimpleArgeoUser argeoUser = new SimpleArgeoUser();
97 argeoUser.setUsername(userDetails.getUsername());
98 addAuthoritiesToRoles(userDetails.getAuthorities(),
99 argeoUser.getRoles());
100 return argeoUser;
101 }
102 }
103
104 /** Creates an argeo user based on spring authentication */
105 public static ArgeoUser asArgeoUser(Authentication authentication) {
106 if (authentication == null)
107 return null;
108
109 if (authentication.getPrincipal() instanceof ArgeoUser) {
110 return new SimpleArgeoUser(
111 (ArgeoUser) authentication.getPrincipal());
112 } else {
113 SimpleArgeoUser argeoUser = new SimpleArgeoUser();
114 argeoUser.setUsername(authentication.getName());
115 addAuthoritiesToRoles(authentication.getAuthorities(),
116 argeoUser.getRoles());
117 return argeoUser;
118 }
119 }
120
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);
126 return argeoUser;
127 }
128 }