]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/ArgeoUserDetails.java
Introduce security LDAP
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / core / 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,
44 Map<String, UserNature> userNatures, String password,
45 GrantedAuthority[] authorities) 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 SimpleArgeoUser
65 .updateUserNaturesWithCheck(userNatures, userNaturesData);
66 }
67
68 public List<String> getRoles() {
69 return roles;
70 }
71
72 /** The provided list, for chaining using {@link Collections} */
73 public static List<String> addAuthoritiesToRoles(
74 GrantedAuthority[] authorities, List<String> roles) {
75 for (GrantedAuthority authority : authorities) {
76 roles.add(authority.getAuthority());
77 }
78 return roles;
79 }
80
81 public static GrantedAuthority[] rolesToAuthorities(List<String> roles) {
82 GrantedAuthority[] arr = new GrantedAuthority[roles.size()];
83 for (int i = 0; i < roles.size(); i++) {
84 String role = roles.get(i);
85 if (log.isTraceEnabled())
86 log.debug("Convert role " + role + " to authority (i=" + i
87 + ")");
88 arr[i] = new GrantedAuthorityImpl(role);
89 }
90 return arr;
91 }
92
93 public static SimpleArgeoUser createSimpleArgeoUser(UserDetails userDetails) {
94 if (userDetails instanceof ArgeoUser) {
95 return new SimpleArgeoUser((ArgeoUser) userDetails);
96 } else {
97 SimpleArgeoUser argeoUser = new SimpleArgeoUser();
98 argeoUser.setUsername(userDetails.getUsername());
99 addAuthoritiesToRoles(userDetails.getAuthorities(),
100 argeoUser.getRoles());
101 return argeoUser;
102 }
103 }
104
105 /** Creates an argeo user based on spring authentication */
106 public static ArgeoUser asArgeoUser(Authentication authentication) {
107 if (authentication == null)
108 return null;
109
110 if (authentication.getPrincipal() instanceof ArgeoUser) {
111 return new SimpleArgeoUser(
112 (ArgeoUser) authentication.getPrincipal());
113 } else {
114 SimpleArgeoUser argeoUser = new SimpleArgeoUser();
115 argeoUser.setUsername(authentication.getName());
116 addAuthoritiesToRoles(authentication.getAuthorities(),
117 argeoUser.getRoles());
118 return argeoUser;
119 }
120 }
121
122 /** The Spring security context as an argeo user */
123 public static ArgeoUser securityContextUser() {
124 Authentication authentication = SecurityContextHolder.getContext()
125 .getAuthentication();
126 ArgeoUser argeoUser = ArgeoUserDetails.asArgeoUser(authentication);
127 return argeoUser;
128 }
129 }