]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - HomeRepository.java
270a542144c613f019366fa0f4bb279ce17267d0
1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.security
.PrivilegedAction
;
4 import java
.util
.HashSet
;
7 import javax
.jcr
.LoginException
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.security
.Privilege
;
13 import javax
.naming
.InvalidNameException
;
14 import javax
.naming
.ldap
.LdapName
;
15 import javax
.security
.auth
.Subject
;
16 import javax
.security
.auth
.login
.LoginContext
;
18 import org
.argeo
.ArgeoException
;
19 import org
.argeo
.cms
.CmsException
;
20 import org
.argeo
.cms
.auth
.AuthConstants
;
21 import org
.argeo
.jcr
.ArgeoJcrConstants
;
22 import org
.argeo
.jcr
.ArgeoNames
;
23 import org
.argeo
.jcr
.ArgeoTypes
;
24 import org
.argeo
.jcr
.JcrRepositoryWrapper
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.argeo
.jcr
.UserJcrUtils
;
29 * Make sure each user has a home directory available in the default workspace.
31 class HomeRepository
extends JcrRepositoryWrapper
implements KernelConstants
, ArgeoJcrConstants
{
32 // private final Kernel kernel;
34 /** The home base path. */
35 private String homeBasePath
= "/home";
36 private String peopleBasePath
= ArgeoJcrConstants
.PEOPLE_BASE_PATH
;
38 private Set
<String
> checkedUsers
= new HashSet
<String
>();
40 public HomeRepository(Repository repository
) {
41 // this.kernel = kernel;
42 setRepository(repository
);
45 lc
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
47 } catch (javax
.security
.auth
.login
.LoginException e1
) {
48 throw new CmsException("Cannot login as systrem", e1
);
50 Subject
.doAs(lc
.getSubject(), new PrivilegedAction
<Void
>() {
55 initJcr(getRepository().login());
56 } catch (RepositoryException e
) {
57 throw new CmsException("Cannot init JCR home", e
);
66 public Session
login() throws LoginException
, RepositoryException
{
67 Session session
= super.login();
68 String username
= session
.getUserID();
71 if (session
.getUserID().equals(AuthConstants
.ROLE_ANONYMOUS
))
74 if (checkedUsers
.contains(username
))
76 Session adminSession
= KernelUtils
.openAdminSession(getRepository(), session
.getWorkspace().getName());
78 syncJcr(adminSession
, username
);
79 checkedUsers
.add(username
);
81 JcrUtils
.logoutQuietly(adminSession
);
89 /** Session is logged out. */
90 private void initJcr(Session adminSession
) {
92 JcrUtils
.mkdirs(adminSession
, homeBasePath
);
93 JcrUtils
.mkdirs(adminSession
, peopleBasePath
);
96 JcrUtils
.addPrivilege(adminSession
, homeBasePath
, AuthConstants
.ROLE_USER_ADMIN
, Privilege
.JCR_READ
);
97 JcrUtils
.addPrivilege(adminSession
, peopleBasePath
, AuthConstants
.ROLE_USER_ADMIN
, Privilege
.JCR_ALL
);
99 } catch (RepositoryException e
) {
100 throw new CmsException("Cannot initialize node user admin", e
);
102 JcrUtils
.logoutQuietly(adminSession
);
106 private Node
syncJcr(Session session
, String username
) {
108 Node userHome
= UserJcrUtils
.getUserHome(session
, username
);
109 if (userHome
== null) {
110 String homePath
= generateUserPath(homeBasePath
, username
);
111 if (session
.itemExists(homePath
))// duplicate user id
112 userHome
= session
.getNode(homePath
).getParent().addNode(JcrUtils
.lastPathElement(homePath
));
114 userHome
= JcrUtils
.mkdirs(session
, homePath
);
115 // userHome = JcrUtils.mkfolders(session, homePath);
116 userHome
.addMixin(ArgeoTypes
.ARGEO_USER_HOME
);
117 userHome
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
);
120 JcrUtils
.clearAccessControList(session
, homePath
, username
);
121 JcrUtils
.addPrivilege(session
, homePath
, username
, Privilege
.JCR_ALL
);
124 Node userProfile
= UserJcrUtils
.getUserProfile(session
, username
);
126 if (userProfile
== null) {
127 String personPath
= generateUserPath(peopleBasePath
, username
);
129 if (session
.itemExists(personPath
))// duplicate user id
130 personBase
= session
.getNode(personPath
).getParent().addNode(JcrUtils
.lastPathElement(personPath
));
132 personBase
= JcrUtils
.mkdirs(session
, personPath
);
133 userProfile
= personBase
.addNode(ArgeoNames
.ARGEO_PROFILE
);
134 userProfile
.addMixin(ArgeoTypes
.ARGEO_USER_PROFILE
);
135 userProfile
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
);
136 // userProfile.setProperty(ArgeoNames.ARGEO_ENABLED, true);
137 // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_EXPIRED, true);
138 // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_LOCKED, true);
139 // userProfile.setProperty(ArgeoNames.ARGEO_CREDENTIALS_NON_EXPIRED, true);
142 JcrUtils
.clearAccessControList(session
, userProfile
.getPath(), username
);
143 JcrUtils
.addPrivilege(session
, userProfile
.getPath(), username
, Privilege
.JCR_READ
);
147 // if (roles != null) {
148 // writeRemoteRoles(userProfile, roles);
150 if (session
.hasPendingChanges())
153 } catch (RepositoryException e
) {
154 JcrUtils
.discardQuietly(session
);
155 throw new ArgeoException("Cannot sync node security model for " + username
, e
);
159 /** Generate path for a new user home */
160 private String
generateUserPath(String base
, String username
) {
163 dn
= new LdapName(username
);
164 } catch (InvalidNameException e
) {
165 throw new ArgeoException("Invalid name " + username
, e
);
167 String userId
= dn
.getRdn(dn
.size() - 1).getValue().toString();
168 int atIndex
= userId
.indexOf('@');
170 String domain
= userId
.substring(0, atIndex
);
171 String name
= userId
.substring(atIndex
+ 1);
172 return base
+ '/' + JcrUtils
.firstCharsToPath(domain
, 2) + '/' + domain
+ '/'
173 + JcrUtils
.firstCharsToPath(name
, 2) + '/' + name
;
174 } else if (atIndex
== 0 || atIndex
== (userId
.length() - 1)) {
175 throw new ArgeoException("Unsupported username " + userId
);
177 return base
+ '/' + JcrUtils
.firstCharsToPath(userId
, 2) + '/' + userId
;