]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/HomeRepository.java
1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.security
.PrivilegedAction
;
4 import java
.util
.HashSet
;
8 import javax
.jcr
.Repository
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.Session
;
11 import javax
.jcr
.security
.Privilege
;
12 import javax
.naming
.InvalidNameException
;
13 import javax
.naming
.ldap
.LdapName
;
14 import javax
.security
.auth
.Subject
;
15 import javax
.security
.auth
.login
.LoginContext
;
17 import org
.apache
.jackrabbit
.core
.security
.SecurityConstants
;
18 import org
.argeo
.cms
.CmsException
;
19 import org
.argeo
.cms
.auth
.AuthConstants
;
20 import org
.argeo
.jcr
.ArgeoJcrConstants
;
21 import org
.argeo
.jcr
.ArgeoNames
;
22 import org
.argeo
.jcr
.ArgeoTypes
;
23 import org
.argeo
.jcr
.JcrRepositoryWrapper
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.argeo
.jcr
.UserJcrUtils
;
28 * Make sure each user has a home directory available in the default workspace.
30 class HomeRepository
extends JcrRepositoryWrapper
implements KernelConstants
, ArgeoJcrConstants
{
31 // private final Kernel kernel;
33 /** The home base path. */
34 private String homeBasePath
= "/home";
35 private String peopleBasePath
= ArgeoJcrConstants
.PEOPLE_BASE_PATH
;
37 private Set
<String
> checkedUsers
= new HashSet
<String
>();
39 public HomeRepository(Repository repository
) {
40 // this.kernel = kernel;
41 setRepository(repository
);
44 lc
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
46 } catch (javax
.security
.auth
.login
.LoginException e1
) {
47 throw new CmsException("Cannot login as systrem", e1
);
49 Subject
.doAs(lc
.getSubject(), new PrivilegedAction
<Void
>() {
54 initJcr(getRepository().login());
55 } catch (RepositoryException e
) {
56 throw new CmsException("Cannot init JCR home", e
);
65 // public Session login() throws LoginException, RepositoryException {
66 // Session session = super.login();
67 // String username = session.getUserID();
68 // if (username == null)
70 // if (session.getUserID().equals(AuthConstants.ROLE_ANONYMOUS))
73 // if (checkedUsers.contains(username))
75 // Session adminSession = KernelUtils.openAdminSession(getRepository(),
76 // session.getWorkspace().getName());
78 // syncJcr(adminSession, username);
79 // checkedUsers.add(username);
81 // JcrUtils.logoutQuietly(adminSession);
87 protected void processNewSession(Session session
) {
88 String username
= session
.getUserID();
91 if (session
.getUserID().equals(AuthConstants
.ROLE_ANONYMOUS
))
93 if (session
.getUserID().equals(AuthConstants
.ROLE_KERNEL
))
95 if (session
.getUserID().equals(SecurityConstants
.ADMIN_ID
))
98 if (checkedUsers
.contains(username
))
100 Session adminSession
= KernelUtils
.openAdminSession(getRepository(), session
.getWorkspace().getName());
102 syncJcr(adminSession
, username
);
103 checkedUsers
.add(username
);
105 JcrUtils
.logoutQuietly(adminSession
);
112 /** Session is logged out. */
113 private void initJcr(Session adminSession
) {
115 JcrUtils
.mkdirs(adminSession
, homeBasePath
);
116 JcrUtils
.mkdirs(adminSession
, peopleBasePath
);
119 JcrUtils
.addPrivilege(adminSession
, homeBasePath
, AuthConstants
.ROLE_USER_ADMIN
, Privilege
.JCR_READ
);
120 JcrUtils
.addPrivilege(adminSession
, peopleBasePath
, AuthConstants
.ROLE_USER_ADMIN
, Privilege
.JCR_ALL
);
122 } catch (RepositoryException e
) {
123 throw new CmsException("Cannot initialize node user admin", e
);
125 JcrUtils
.logoutQuietly(adminSession
);
129 private Node
syncJcr(Session session
, String username
) {
131 Node userHome
= UserJcrUtils
.getUserHome(session
, username
);
132 if (userHome
== null) {
133 String homePath
= generateUserPath(homeBasePath
, username
);
134 if (session
.itemExists(homePath
))// duplicate user id
135 userHome
= session
.getNode(homePath
).getParent().addNode(JcrUtils
.lastPathElement(homePath
));
137 userHome
= JcrUtils
.mkdirs(session
, homePath
);
138 // userHome = JcrUtils.mkfolders(session, homePath);
139 userHome
.addMixin(ArgeoTypes
.ARGEO_USER_HOME
);
140 userHome
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
);
143 JcrUtils
.clearAccessControList(session
, homePath
, username
);
144 JcrUtils
.addPrivilege(session
, homePath
, username
, Privilege
.JCR_ALL
);
147 Node userProfile
= UserJcrUtils
.getUserProfile(session
, username
);
149 if (userProfile
== null) {
150 String personPath
= generateUserPath(peopleBasePath
, username
);
152 if (session
.itemExists(personPath
))// duplicate user id
153 personBase
= session
.getNode(personPath
).getParent().addNode(JcrUtils
.lastPathElement(personPath
));
155 personBase
= JcrUtils
.mkdirs(session
, personPath
);
156 userProfile
= personBase
.addNode(ArgeoNames
.ARGEO_PROFILE
);
157 userProfile
.addMixin(ArgeoTypes
.ARGEO_USER_PROFILE
);
158 userProfile
.setProperty(ArgeoNames
.ARGEO_USER_ID
, username
);
159 // userProfile.setProperty(ArgeoNames.ARGEO_ENABLED, true);
160 // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_EXPIRED,
162 // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_LOCKED,
164 // userProfile.setProperty(ArgeoNames.ARGEO_CREDENTIALS_NON_EXPIRED,
168 JcrUtils
.clearAccessControList(session
, userProfile
.getPath(), username
);
169 JcrUtils
.addPrivilege(session
, userProfile
.getPath(), username
, Privilege
.JCR_READ
);
173 // if (roles != null) {
174 // writeRemoteRoles(userProfile, roles);
176 if (session
.hasPendingChanges())
179 } catch (RepositoryException e
) {
180 JcrUtils
.discardQuietly(session
);
181 throw new CmsException("Cannot sync node security model for " + username
, e
);
185 /** Generate path for a new user home */
186 private String
generateUserPath(String base
, String username
) {
189 dn
= new LdapName(username
);
190 } catch (InvalidNameException e
) {
191 throw new CmsException("Invalid name " + username
, e
);
193 String userId
= dn
.getRdn(dn
.size() - 1).getValue().toString();
194 int atIndex
= userId
.indexOf('@');
196 String domain
= userId
.substring(0, atIndex
);
197 String name
= userId
.substring(atIndex
+ 1);
198 return base
+ '/' + JcrUtils
.firstCharsToPath(domain
, 2) + '/' + domain
+ '/'
199 + JcrUtils
.firstCharsToPath(name
, 2) + '/' + name
;
200 } else if (atIndex
== 0 || atIndex
== (userId
.length() - 1)) {
201 throw new CmsException("Unsupported username " + userId
);
203 return base
+ '/' + JcrUtils
.firstCharsToPath(userId
, 2) + '/' + userId
;