X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=security%2Fruntime%2Forg.argeo.security.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fsecurity%2Fjcr%2FJcrUserDetails.java;h=11e463d349a7a7f86cf30d953e0636ef342ea8db;hb=0d8a9149227c52245ff9eb20f29ad2b81a99b3e2;hp=ea66b5ff74e529f03d2bb97b766253b51e227f7d;hpb=ee0981fe4c265fd9cd01e1cc47599fd018bf363a;p=lgpl%2Fargeo-commons.git diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java index ea66b5ff7..11e463d34 100644 --- a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java @@ -3,15 +3,11 @@ package org.argeo.security.jcr; import java.util.ArrayList; import java.util.List; -import javax.jcr.Node; -import javax.jcr.RepositoryException; -import javax.jcr.Session; - -import org.argeo.ArgeoException; import org.springframework.security.GrantedAuthority; import org.springframework.security.GrantedAuthorityImpl; import org.springframework.security.userdetails.User; +/** User details wrapping a home node. */ public class JcrUserDetails extends User { private static final long serialVersionUID = -3594542993773402380L; private final String homePath; @@ -25,45 +21,27 @@ public class JcrUserDetails extends User { this.homePath = homePath; } - public String getHomePath() { - return homePath; - } - - public static JcrUserDetails argeoUserToJcrUserDetails( - JcrArgeoUser argeoUser) { - try { - List gas = new ArrayList(); - for (String role : argeoUser.getRoles()) - gas.add(new GrantedAuthorityImpl(role)); - return new JcrUserDetails(argeoUser.getHome().getPath(), - argeoUser.getUsername(), argeoUser.getPassword(), - argeoUser.getEnabled(), true, true, true, - gas.toArray(new GrantedAuthority[gas.size()])); - } catch (Exception e) { - throw new ArgeoException("Cannot convert " + argeoUser - + " to JCR user details", e); + /** Clone immutable with new roles */ + public JcrUserDetails cloneWithNewRoles(List roles) { + List authorities = new ArrayList(); + for (String role : roles) { + authorities.add(new GrantedAuthorityImpl(role)); } + return new JcrUserDetails(homePath, getUsername(), getPassword(), + isEnabled(), isAccountNonExpired(), isAccountNonExpired(), + isAccountNonLocked(), + authorities.toArray(new GrantedAuthority[authorities.size()])); } - public static JcrArgeoUser jcrUserDetailsToArgeoUser(Session userSession, - JcrUserDetails jcrUserDetails) { - if (!userSession.getUserID().equals(jcrUserDetails.getUsername())) - throw new ArgeoException("User session has user id " - + userSession.getUserID() + " while details has username " - + jcrUserDetails.getUsername()); - - Node userHome; - try { - userHome = userSession.getNode(jcrUserDetails.getHomePath()); - } catch (RepositoryException e) { - throw new ArgeoException("Cannot retrieve user home with path " - + jcrUserDetails.getHomePath(), e); - } - List roles = new ArrayList(); - for (GrantedAuthority ga : jcrUserDetails.getAuthorities()) - roles.add(ga.getAuthority()); - return new JcrArgeoUser(userHome, jcrUserDetails.getPassword(), roles, - jcrUserDetails.isEnabled()); + /** Clone immutable with new password */ + public JcrUserDetails cloneWithNewPassword(String password) { + return new JcrUserDetails(homePath, getUsername(), password, + isEnabled(), isAccountNonExpired(), isAccountNonExpired(), + isAccountNonLocked(), getAuthorities()); + } + public String getHomePath() { + return homePath; } + }