1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.security
.PrivilegedAction
;
4 import java
.text
.SimpleDateFormat
;
6 import java
.util
.HashSet
;
10 import javax
.jcr
.Repository
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
13 import javax
.jcr
.nodetype
.NodeType
;
14 import javax
.jcr
.security
.Privilege
;
15 import javax
.naming
.InvalidNameException
;
16 import javax
.naming
.ldap
.LdapName
;
17 import javax
.security
.auth
.Subject
;
18 import javax
.security
.auth
.login
.LoginContext
;
20 import org
.argeo
.cms
.CmsException
;
21 import org
.argeo
.jcr
.JcrRepositoryWrapper
;
22 import org
.argeo
.jcr
.JcrUtils
;
23 import org
.argeo
.node
.NodeConstants
;
24 import org
.argeo
.node
.NodeNames
;
25 import org
.argeo
.node
.NodeTypes
;
26 import org
.argeo
.node
.NodeUtils
;
29 * Make sure each user has a home directory available in the default workspace.
31 class HomeRepository
extends JcrRepositoryWrapper
implements KernelConstants
{
33 /** The home base path. */
34 private String homeBasePath
= KernelConstants
.DEFAULT_HOME_BASE_PATH
;
35 private String usersBasePath
= KernelConstants
.DEFAULT_USERS_BASE_PATH
;
36 private String groupsBasePath
= KernelConstants
.DEFAULT_GROUPS_BASE_PATH
;
38 private Set
<String
> checkedUsers
= new HashSet
<String
>();
40 private SimpleDateFormat usersDatePath
= new SimpleDateFormat("YYYY/MM");
42 public HomeRepository(Repository repository
) {
44 putDescriptor(NodeConstants
.CN
, NodeConstants
.HOME
);
47 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_DATA_ADMIN
);
49 } catch (javax
.security
.auth
.login
.LoginException e1
) {
50 throw new CmsException("Cannot login as systrem", e1
);
52 Subject
.doAs(lc
.getSubject(), new PrivilegedAction
<Void
>() {
57 Session adminSession
= getRepository().login();
58 initJcr(adminSession
);
59 } catch (RepositoryException e
) {
60 throw new CmsException("Cannot init JCR home", e
);
69 protected void processNewSession(Session session
) {
70 String username
= session
.getUserID();
73 if (session
.getUserID().equals(NodeConstants
.ROLE_ANONYMOUS
))
76 if (checkedUsers
.contains(username
))
78 Session adminSession
= KernelUtils
.openAdminSession(getRepository(), session
.getWorkspace().getName());
80 syncJcr(adminSession
, username
);
81 checkedUsers
.add(username
);
83 JcrUtils
.logoutQuietly(adminSession
);
90 /** Session is logged out. */
91 private void initJcr(Session adminSession
) {
93 JcrUtils
.mkdirs(adminSession
, homeBasePath
);
94 JcrUtils
.mkdirs(adminSession
, groupsBasePath
);
97 JcrUtils
.addPrivilege(adminSession
, homeBasePath
, NodeConstants
.ROLE_USER_ADMIN
, Privilege
.JCR_READ
);
98 JcrUtils
.addPrivilege(adminSession
, groupsBasePath
, NodeConstants
.ROLE_USER_ADMIN
, Privilege
.JCR_READ
);
100 } catch (RepositoryException e
) {
101 throw new CmsException("Cannot initialize node user admin", e
);
103 JcrUtils
.logoutQuietly(adminSession
);
107 private void syncJcr(Session session
, String username
) {
109 Node userHome
= NodeUtils
.getUserHome(session
, username
);
110 if (userHome
== null) {
111 String homePath
= generateUserPath(username
);
112 if (session
.itemExists(homePath
))// duplicate user id
113 userHome
= session
.getNode(homePath
).getParent().addNode(JcrUtils
.lastPathElement(homePath
));
115 userHome
= JcrUtils
.mkdirs(session
, homePath
);
116 // userHome = JcrUtils.mkfolders(session, homePath);
117 userHome
.addMixin(NodeTypes
.NODE_USER_HOME
);
118 userHome
.setProperty(NodeNames
.LDAP_UID
, username
);
121 JcrUtils
.clearAccessControList(session
, homePath
, username
);
122 JcrUtils
.addPrivilege(session
, homePath
, username
, Privilege
.JCR_ALL
);
124 if (session
.hasPendingChanges())
126 } catch (RepositoryException e
) {
127 JcrUtils
.discardQuietly(session
);
128 throw new CmsException("Cannot sync node security model for " + username
, e
);
132 /** Generate path for a new user home */
133 private String
generateUserPath(String username
) {
136 dn
= new LdapName(username
);
137 } catch (InvalidNameException e
) {
138 throw new CmsException("Invalid name " + username
, e
);
140 String userId
= dn
.getRdn(dn
.size() - 1).getValue().toString();
141 int atIndex
= userId
.indexOf('@');
143 return homeBasePath
+ '/' + userId
;
145 return usersBasePath
+ '/' + usersDatePath
.format(new Date()) + '/' + userId
;
147 // if (atIndex > 0) {
148 // String domain = userId.substring(0, atIndex);
149 // String name = userId.substring(atIndex + 1);
150 // return base + '/' + domain + '/' + name;
151 // } else if (atIndex == 0 || atIndex == (userId.length() - 1)) {
152 // throw new CmsException("Unsupported username " + userId);
154 // return base + '/' + userId;
158 public void createWorkgroup(LdapName dn
) {
159 Session adminSession
= KernelUtils
.openAdminSession(this);
160 String cn
= dn
.getRdn(dn
.size() - 1).getValue().toString();
161 Node newWorkgroup
= NodeUtils
.getGroupHome(adminSession
, cn
);
162 if (newWorkgroup
!= null) {
163 JcrUtils
.logoutQuietly(adminSession
);
164 throw new CmsException("Workgroup " + newWorkgroup
+ " already exists for " + dn
);
167 // TODO enhance transformation of cn to a valid node name
168 // String relPath = cn.replaceAll("[^a-zA-Z0-9]", "_");
169 String relPath
= JcrUtils
.replaceInvalidChars(cn
);
170 newWorkgroup
= JcrUtils
.mkdirs(adminSession
.getNode(groupsBasePath
), relPath
, NodeType
.NT_UNSTRUCTURED
);
171 newWorkgroup
.addMixin(NodeTypes
.NODE_GROUP_HOME
);
172 newWorkgroup
.setProperty(NodeNames
.LDAP_CN
, cn
);
174 JcrUtils
.addPrivilege(adminSession
, newWorkgroup
.getPath(), dn
.toString(), Privilege
.JCR_ALL
);
176 } catch (RepositoryException e
) {
177 throw new CmsException("Cannot create workgroup", e
);
179 JcrUtils
.logoutQuietly(adminSession
);