]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/HomeRepository.java
Introduce Argeo Sync
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / 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 private final boolean remote;
43
44 public HomeRepository(Repository repository, boolean remote) {
45 super(repository);
46 this.remote = remote;
47 putDescriptor(NodeConstants.CN, NodeConstants.HOME);
48 if (!remote) {
49 LoginContext lc;
50 try {
51 lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
52 lc.login();
53 } catch (javax.security.auth.login.LoginException e1) {
54 throw new CmsException("Cannot login as systrem", e1);
55 }
56 Subject.doAs(lc.getSubject(), new PrivilegedAction<Void>() {
57
58 @Override
59 public Void run() {
60 try {
61 Session adminSession = getRepository().login();
62 initJcr(adminSession);
63 } catch (RepositoryException e) {
64 throw new CmsException("Cannot init JCR home", e);
65 }
66 return null;
67 }
68
69 });
70 }
71 }
72
73 @Override
74 protected void processNewSession(Session session) {
75 String username = session.getUserID();
76 if (username == null || username.toString().equals(""))
77 return;
78 if (session.getUserID().equals(NodeConstants.ROLE_ANONYMOUS))
79 return;
80
81 if (checkedUsers.contains(username))
82 return;
83 Session adminSession = KernelUtils.openAdminSession(getRepository(), session.getWorkspace().getName());
84 try {
85 syncJcr(adminSession, username);
86 checkedUsers.add(username);
87 } finally {
88 JcrUtils.logoutQuietly(adminSession);
89 }
90 }
91
92 /*
93 * JCR
94 */
95 /** Session is logged out. */
96 private void initJcr(Session adminSession) {
97 try {
98 JcrUtils.mkdirs(adminSession, homeBasePath);
99 JcrUtils.mkdirs(adminSession, groupsBasePath);
100 adminSession.save();
101
102 JcrUtils.addPrivilege(adminSession, homeBasePath, NodeConstants.ROLE_USER_ADMIN, Privilege.JCR_READ);
103 JcrUtils.addPrivilege(adminSession, groupsBasePath, NodeConstants.ROLE_USER_ADMIN, Privilege.JCR_READ);
104 adminSession.save();
105 } catch (RepositoryException e) {
106 throw new CmsException("Cannot initialize home repository", e);
107 } finally {
108 JcrUtils.logoutQuietly(adminSession);
109 }
110 }
111
112 private void syncJcr(Session session, String username) {
113 try {
114 Node userHome = NodeUtils.getUserHome(session, username);
115 if (userHome == null) {
116 String homePath = generateUserPath(username);
117 if (session.itemExists(homePath))// duplicate user id
118 userHome = session.getNode(homePath).getParent().addNode(JcrUtils.lastPathElement(homePath));
119 else
120 userHome = JcrUtils.mkdirs(session, homePath);
121 // userHome = JcrUtils.mkfolders(session, homePath);
122 userHome.addMixin(NodeTypes.NODE_USER_HOME);
123 userHome.addMixin(NodeType.MIX_CREATED);
124 userHome.setProperty(NodeNames.LDAP_UID, username);
125 session.save();
126
127 JcrUtils.clearAccessControList(session, homePath, username);
128 JcrUtils.addPrivilege(session, homePath, username, Privilege.JCR_ALL);
129 }
130 if (session.hasPendingChanges())
131 session.save();
132 } catch (RepositoryException e) {
133 JcrUtils.discardQuietly(session);
134 throw new CmsException("Cannot sync node security model for " + username, e);
135 }
136 }
137
138 /** Generate path for a new user home */
139 private String generateUserPath(String username) {
140 LdapName dn;
141 try {
142 dn = new LdapName(username);
143 } catch (InvalidNameException e) {
144 throw new CmsException("Invalid name " + username, e);
145 }
146 String userId = dn.getRdn(dn.size() - 1).getValue().toString();
147 int atIndex = userId.indexOf('@');
148 if (atIndex < 0) {
149 return homeBasePath + '/' + userId;
150 } else {
151 return usersBasePath + '/' + usersDatePath.format(new Date()) + '/' + userId;
152 }
153 // if (atIndex > 0) {
154 // String domain = userId.substring(0, atIndex);
155 // String name = userId.substring(atIndex + 1);
156 // return base + '/' + domain + '/' + name;
157 // } else if (atIndex == 0 || atIndex == (userId.length() - 1)) {
158 // throw new CmsException("Unsupported username " + userId);
159 // } else {
160 // return base + '/' + userId;
161 // }
162 }
163
164 public void createWorkgroup(LdapName dn) {
165 Session adminSession = KernelUtils.openAdminSession(this);
166 String cn = dn.getRdn(dn.size() - 1).getValue().toString();
167 Node newWorkgroup = NodeUtils.getGroupHome(adminSession, cn);
168 if (newWorkgroup != null) {
169 JcrUtils.logoutQuietly(adminSession);
170 throw new CmsException("Workgroup " + newWorkgroup + " already exists for " + dn);
171 }
172 try {
173 // TODO enhance transformation of cn to a valid node name
174 // String relPath = cn.replaceAll("[^a-zA-Z0-9]", "_");
175 String relPath = JcrUtils.replaceInvalidChars(cn);
176 newWorkgroup = JcrUtils.mkdirs(adminSession.getNode(groupsBasePath), relPath, NodeType.NT_UNSTRUCTURED);
177 newWorkgroup.addMixin(NodeTypes.NODE_GROUP_HOME);
178 newWorkgroup.addMixin(NodeType.MIX_CREATED);
179 newWorkgroup.setProperty(NodeNames.LDAP_CN, cn);
180 adminSession.save();
181 JcrUtils.addPrivilege(adminSession, newWorkgroup.getPath(), dn.toString(), Privilege.JCR_ALL);
182 adminSession.save();
183 } catch (RepositoryException e) {
184 throw new CmsException("Cannot create workgroup", e);
185 } finally {
186 JcrUtils.logoutQuietly(adminSession);
187 }
188
189 }
190
191 }