]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JcrUserDetails.java
3815b843b18c447650f2de9be7e45a322528b389
[lgpl/argeo-commons.git] / JcrUserDetails.java
1 package org.argeo.security.jcr;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
10 import org.argeo.ArgeoException;
11 import org.springframework.security.GrantedAuthority;
12 import org.springframework.security.GrantedAuthorityImpl;
13 import org.springframework.security.userdetails.User;
14
15 public class JcrUserDetails extends User {
16 private static final long serialVersionUID = -3594542993773402380L;
17 private final String homePath;
18
19 public JcrUserDetails(String homePath, String username, String password,
20 boolean enabled, boolean accountNonExpired,
21 boolean credentialsNonExpired, boolean accountNonLocked,
22 GrantedAuthority[] authorities) throws IllegalArgumentException {
23 super(username, password, enabled, accountNonExpired,
24 credentialsNonExpired, accountNonLocked, authorities);
25 this.homePath = homePath;
26 }
27
28 public String getHomePath() {
29 return homePath;
30 }
31
32 public static JcrUserDetails argeoUserToJcrUserDetails(
33 JcrArgeoUser argeoUser) {
34 try {
35 List<GrantedAuthority> gas = new ArrayList<GrantedAuthority>();
36 for (String role : argeoUser.getRoles())
37 gas.add(new GrantedAuthorityImpl(role));
38 return new JcrUserDetails(argeoUser.getHome().getPath(),
39 argeoUser.getUsername(), argeoUser.getPassword(),
40 argeoUser.getEnabled(), true, true, true,
41 gas.toArray(new GrantedAuthority[gas.size()]));
42 } catch (Exception e) {
43 throw new ArgeoException("Cannot convert " + argeoUser
44 + " to JCR user details", e);
45 }
46 }
47
48 public static JcrArgeoUser jcrUserDetailsToArgeoUser(Session userSession,
49 JcrUserDetails jcrUserDetails) {
50 if (!userSession.getUserID().equals(jcrUserDetails.getUsername()))
51 throw new ArgeoException("User session has user id "
52 + userSession.getUserID() + " while details has username "
53 + jcrUserDetails.getUsername());
54
55 Node userHome;
56 try {
57 userHome = userSession.getNode(jcrUserDetails.getHomePath());
58 } catch (RepositoryException e) {
59 throw new ArgeoException("Cannot retrieve user home with path "
60 + jcrUserDetails.getHomePath(), e);
61 }
62 List<String> roles = new ArrayList<String>();
63 for (GrantedAuthority ga : jcrUserDetails.getAuthorities())
64 roles.add(ga.getAuthority());
65 return new JcrArgeoUser(userHome, jcrUserDetails.getPassword(), roles,
66 jcrUserDetails.isEnabled());
67
68 }
69
70 public JcrUserDetails cloneWithNewRoles(List<String> roles) {
71 List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
72 for (String role : roles) {
73 authorities.add(new GrantedAuthorityImpl(role));
74 }
75 return new JcrUserDetails(homePath, getUsername(), getPassword(),
76 isEnabled(), isAccountNonExpired(), isAccountNonExpired(),
77 isAccountNonLocked(),
78 authorities.toArray(new GrantedAuthority[authorities.size()]));
79 }
80
81 public JcrUserDetails cloneWithNewPassword(String password) {
82 return new JcrUserDetails(homePath, getUsername(), password,
83 isEnabled(), isAccountNonExpired(), isAccountNonExpired(),
84 isAccountNonLocked(), getAuthorities());
85 }
86 }