]> git.argeo.org Git - lgpl/argeo-commons.git/blob - HomeRepository.java
9ab66ffb8f3314b8b727ef70afb3b89ac00e1d72
[lgpl/argeo-commons.git] / HomeRepository.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.security.PrivilegedAction;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import javax.jcr.Node;
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;
19
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;
27
28 /**
29 * Make sure each user has a home directory available in the default workspace.
30 */
31 class HomeRepository extends JcrRepositoryWrapper implements KernelConstants {
32
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;
37
38 private Set<String> checkedUsers = new HashSet<String>();
39
40 private SimpleDateFormat usersDatePath = new SimpleDateFormat("YYYY/MM");
41
42 public HomeRepository(Repository repository) {
43 super(repository);
44 putDescriptor(NodeConstants.CN, NodeConstants.HOME);
45 LoginContext lc;
46 try {
47 lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
48 lc.login();
49 } catch (javax.security.auth.login.LoginException e1) {
50 throw new CmsException("Cannot login as systrem", e1);
51 }
52 Subject.doAs(lc.getSubject(), new PrivilegedAction<Void>() {
53
54 @Override
55 public Void run() {
56 try {
57 Session adminSession = getRepository().login();
58 initJcr(adminSession);
59 } catch (RepositoryException e) {
60 throw new CmsException("Cannot init JCR home", e);
61 }
62 return null;
63 }
64
65 });
66 }
67
68 @Override
69 protected void processNewSession(Session session) {
70 String username = session.getUserID();
71 if (username == null)
72 return;
73 if (session.getUserID().equals(NodeConstants.ROLE_ANONYMOUS))
74 return;
75
76 if (checkedUsers.contains(username))
77 return;
78 Session adminSession = KernelUtils.openAdminSession(getRepository(), session.getWorkspace().getName());
79 try {
80 syncJcr(adminSession, username);
81 checkedUsers.add(username);
82 } finally {
83 JcrUtils.logoutQuietly(adminSession);
84 }
85 }
86
87 /*
88 * JCR
89 */
90 /** Session is logged out. */
91 private void initJcr(Session adminSession) {
92 try {
93 JcrUtils.mkdirs(adminSession, homeBasePath);
94 JcrUtils.mkdirs(adminSession, groupsBasePath);
95 adminSession.save();
96
97 JcrUtils.addPrivilege(adminSession, homeBasePath, NodeConstants.ROLE_USER_ADMIN, Privilege.JCR_READ);
98 JcrUtils.addPrivilege(adminSession, groupsBasePath, NodeConstants.ROLE_USER_ADMIN, Privilege.JCR_READ);
99 adminSession.save();
100 } catch (RepositoryException e) {
101 throw new CmsException("Cannot initialize node user admin", e);
102 } finally {
103 JcrUtils.logoutQuietly(adminSession);
104 }
105 }
106
107 private void syncJcr(Session session, String username) {
108 try {
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));
114 else
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);
119 session.save();
120
121 JcrUtils.clearAccessControList(session, homePath, username);
122 JcrUtils.addPrivilege(session, homePath, username, Privilege.JCR_ALL);
123 }
124 if (session.hasPendingChanges())
125 session.save();
126 } catch (RepositoryException e) {
127 JcrUtils.discardQuietly(session);
128 throw new CmsException("Cannot sync node security model for " + username, e);
129 }
130 }
131
132 /** Generate path for a new user home */
133 private String generateUserPath(String username) {
134 LdapName dn;
135 try {
136 dn = new LdapName(username);
137 } catch (InvalidNameException e) {
138 throw new CmsException("Invalid name " + username, e);
139 }
140 String userId = dn.getRdn(dn.size() - 1).getValue().toString();
141 int atIndex = userId.indexOf('@');
142 if (atIndex < 0) {
143 return homeBasePath + '/' + userId;
144 } else {
145 return usersBasePath + '/' + usersDatePath.format(new Date()) + '/' + userId;
146 }
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);
153 // } else {
154 // return base + '/' + userId;
155 // }
156 }
157
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);
165 }
166 try {
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);
173 adminSession.save();
174 JcrUtils.addPrivilege(adminSession, newWorkgroup.getPath(), dn.toString(), Privilege.JCR_ALL);
175 adminSession.save();
176 } catch (RepositoryException e) {
177 throw new CmsException("Cannot create workgroup", e);
178 } finally {
179 JcrUtils.logoutQuietly(adminSession);
180 }
181
182 }
183
184 }