1 package org
.argeo
.security
.jcr
;
3 import java
.util
.ArrayList
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
10 import org
.argeo
.ArgeoException
;
11 import org
.springframework
.security
.GrantedAuthority
;
12 import org
.springframework
.security
.GrantedAuthorityImpl
;
13 import org
.springframework
.security
.userdetails
.User
;
15 public class JcrUserDetails
extends User
{
16 private static final long serialVersionUID
= -3594542993773402380L;
17 private final String homePath
;
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
;
28 public String
getHomePath() {
32 public static JcrUserDetails
argeoUserToJcrUserDetails(
33 JcrArgeoUser argeoUser
) {
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
);
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());
57 userHome
= userSession
.getNode(jcrUserDetails
.getHomePath());
58 } catch (RepositoryException e
) {
59 throw new ArgeoException("Cannot retrieve user home with path "
60 + jcrUserDetails
.getHomePath(), e
);
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());
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
));
75 return new JcrUserDetails(homePath
, getUsername(), getPassword(),
76 isEnabled(), isAccountNonExpired(), isAccountNonExpired(),
78 authorities
.toArray(new GrantedAuthority
[authorities
.size()]));
81 public JcrUserDetails
cloneWithNewPassword(String password
) {
82 return new JcrUserDetails(homePath
, getUsername(), password
,
83 isEnabled(), isAccountNonExpired(), isAccountNonExpired(),
84 isAccountNonLocked(), getAuthorities());