]> git.argeo.org Git - lgpl/argeo-commons.git/blob - kernel/HomeRepository.java
Prepare next development cycle
[lgpl/argeo-commons.git] / kernel / HomeRepository.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.security.PrivilegedAction;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import javax.jcr.Node;
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;
16
17 import org.argeo.ArgeoException;
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;
26
27 /**
28 * Make sure each user has a home directory available in the default workspace.
29 */
30 class HomeRepository extends JcrRepositoryWrapper implements KernelConstants, ArgeoJcrConstants {
31 // private final Kernel kernel;
32
33 /** The home base path. */
34 private String homeBasePath = "/home";
35 private String peopleBasePath = ArgeoJcrConstants.PEOPLE_BASE_PATH;
36
37 private Set<String> checkedUsers = new HashSet<String>();
38
39 public HomeRepository(Repository repository) {
40 // this.kernel = kernel;
41 setRepository(repository);
42 LoginContext lc;
43 try {
44 lc = new LoginContext(AuthConstants.LOGIN_CONTEXT_DATA_ADMIN);
45 lc.login();
46 } catch (javax.security.auth.login.LoginException e1) {
47 throw new CmsException("Cannot login as systrem", e1);
48 }
49 Subject.doAs(lc.getSubject(), new PrivilegedAction<Void>() {
50
51 @Override
52 public Void run() {
53 try {
54 initJcr(getRepository().login());
55 } catch (RepositoryException e) {
56 throw new CmsException("Cannot init JCR home", e);
57 }
58 return null;
59 }
60
61 });
62 }
63
64 // @Override
65 // public Session login() throws LoginException, RepositoryException {
66 // Session session = super.login();
67 // String username = session.getUserID();
68 // if (username == null)
69 // return session;
70 // if (session.getUserID().equals(AuthConstants.ROLE_ANONYMOUS))
71 // return session;
72 //
73 // if (checkedUsers.contains(username))
74 // return session;
75 // Session adminSession = KernelUtils.openAdminSession(getRepository(),
76 // session.getWorkspace().getName());
77 // try {
78 // syncJcr(adminSession, username);
79 // checkedUsers.add(username);
80 // } finally {
81 // JcrUtils.logoutQuietly(adminSession);
82 // }
83 // return session;
84 // }
85
86 @Override
87 protected void processNewSession(Session session) {
88 String username = session.getUserID();
89 if (username == null)
90 return;
91 if (session.getUserID().equals(AuthConstants.ROLE_ANONYMOUS))
92 return;
93 if (session.getUserID().equals(AuthConstants.ROLE_KERNEL))
94 return;
95
96 if (checkedUsers.contains(username))
97 return;
98 Session adminSession = KernelUtils.openAdminSession(getRepository(), session.getWorkspace().getName());
99 try {
100 syncJcr(adminSession, username);
101 checkedUsers.add(username);
102 } finally {
103 JcrUtils.logoutQuietly(adminSession);
104 }
105 }
106
107 /*
108 * JCR
109 */
110 /** Session is logged out. */
111 private void initJcr(Session adminSession) {
112 try {
113 JcrUtils.mkdirs(adminSession, homeBasePath);
114 JcrUtils.mkdirs(adminSession, peopleBasePath);
115 adminSession.save();
116
117 JcrUtils.addPrivilege(adminSession, homeBasePath, AuthConstants.ROLE_USER_ADMIN, Privilege.JCR_READ);
118 JcrUtils.addPrivilege(adminSession, peopleBasePath, AuthConstants.ROLE_USER_ADMIN, Privilege.JCR_ALL);
119 adminSession.save();
120 } catch (RepositoryException e) {
121 throw new CmsException("Cannot initialize node user admin", e);
122 } finally {
123 JcrUtils.logoutQuietly(adminSession);
124 }
125 }
126
127 private Node syncJcr(Session session, String username) {
128 try {
129 Node userHome = UserJcrUtils.getUserHome(session, username);
130 if (userHome == null) {
131 String homePath = generateUserPath(homeBasePath, username);
132 if (session.itemExists(homePath))// duplicate user id
133 userHome = session.getNode(homePath).getParent().addNode(JcrUtils.lastPathElement(homePath));
134 else
135 userHome = JcrUtils.mkdirs(session, homePath);
136 // userHome = JcrUtils.mkfolders(session, homePath);
137 userHome.addMixin(ArgeoTypes.ARGEO_USER_HOME);
138 userHome.setProperty(ArgeoNames.ARGEO_USER_ID, username);
139 session.save();
140
141 JcrUtils.clearAccessControList(session, homePath, username);
142 JcrUtils.addPrivilege(session, homePath, username, Privilege.JCR_ALL);
143 }
144
145 Node userProfile = UserJcrUtils.getUserProfile(session, username);
146 // new user
147 if (userProfile == null) {
148 String personPath = generateUserPath(peopleBasePath, username);
149 Node personBase;
150 if (session.itemExists(personPath))// duplicate user id
151 personBase = session.getNode(personPath).getParent().addNode(JcrUtils.lastPathElement(personPath));
152 else
153 personBase = JcrUtils.mkdirs(session, personPath);
154 userProfile = personBase.addNode(ArgeoNames.ARGEO_PROFILE);
155 userProfile.addMixin(ArgeoTypes.ARGEO_USER_PROFILE);
156 userProfile.setProperty(ArgeoNames.ARGEO_USER_ID, username);
157 // userProfile.setProperty(ArgeoNames.ARGEO_ENABLED, true);
158 // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_EXPIRED,
159 // true);
160 // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_LOCKED,
161 // true);
162 // userProfile.setProperty(ArgeoNames.ARGEO_CREDENTIALS_NON_EXPIRED,
163 // true);
164 session.save();
165
166 JcrUtils.clearAccessControList(session, userProfile.getPath(), username);
167 JcrUtils.addPrivilege(session, userProfile.getPath(), username, Privilege.JCR_READ);
168 }
169
170 // Remote roles
171 // if (roles != null) {
172 // writeRemoteRoles(userProfile, roles);
173 // }
174 if (session.hasPendingChanges())
175 session.save();
176 return userProfile;
177 } catch (RepositoryException e) {
178 JcrUtils.discardQuietly(session);
179 throw new ArgeoException("Cannot sync node security model for " + username, e);
180 }
181 }
182
183 /** Generate path for a new user home */
184 private String generateUserPath(String base, String username) {
185 LdapName dn;
186 try {
187 dn = new LdapName(username);
188 } catch (InvalidNameException e) {
189 throw new ArgeoException("Invalid name " + username, e);
190 }
191 String userId = dn.getRdn(dn.size() - 1).getValue().toString();
192 int atIndex = userId.indexOf('@');
193 if (atIndex > 0) {
194 String domain = userId.substring(0, atIndex);
195 String name = userId.substring(atIndex + 1);
196 return base + '/' + JcrUtils.firstCharsToPath(domain, 2) + '/' + domain + '/'
197 + JcrUtils.firstCharsToPath(name, 2) + '/' + name;
198 } else if (atIndex == 0 || atIndex == (userId.length() - 1)) {
199 throw new ArgeoException("Unsupported username " + userId);
200 } else {
201 return base + '/' + JcrUtils.firstCharsToPath(userId, 2) + '/' + userId;
202 }
203 }
204
205 }