]> git.argeo.org Git - lgpl/argeo-commons.git/blob - HomeRepository.java
5c6f419969eed607856e092d66b5e583a256dd8b
[lgpl/argeo-commons.git] / 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.cms.CmsException;
18 import org.argeo.jcr.JcrRepositoryWrapper;
19 import org.argeo.jcr.JcrUtils;
20 import org.argeo.node.NodeConstants;
21 import org.argeo.node.NodeNames;
22 import org.argeo.node.NodeTypes;
23 import org.argeo.node.NodeUtils;
24
25 /**
26 * Make sure each user has a home directory available in the default workspace.
27 */
28 class HomeRepository extends JcrRepositoryWrapper implements KernelConstants {
29 /** The home base path. */
30 private String homeBasePath = "/home";
31 // private String peopleBasePath = NodeConstants.PEOPLE_BASE_PATH;
32
33 private Set<String> checkedUsers = new HashSet<String>();
34
35 public HomeRepository(Repository repository) {
36 super(repository);
37 putDescriptor(NodeConstants.CN, NodeConstants.HOME);
38 LoginContext lc;
39 try {
40 lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_DATA_ADMIN);
41 lc.login();
42 } catch (javax.security.auth.login.LoginException e1) {
43 throw new CmsException("Cannot login as systrem", e1);
44 }
45 Subject.doAs(lc.getSubject(), new PrivilegedAction<Void>() {
46
47 @Override
48 public Void run() {
49 try {
50 Session adminSession = getRepository().login();
51 initJcr(adminSession);
52 } catch (RepositoryException e) {
53 throw new CmsException("Cannot init JCR home", e);
54 }
55 return null;
56 }
57
58 });
59 }
60
61 // @Override
62 // public Session login() throws LoginException, RepositoryException {
63 // Session session = super.login();
64 // String username = session.getUserID();
65 // if (username == null)
66 // return session;
67 // if (session.getUserID().equals(AuthConstants.ROLE_ANONYMOUS))
68 // return session;
69 //
70 // if (checkedUsers.contains(username))
71 // return session;
72 // Session adminSession = KernelUtils.openAdminSession(getRepository(),
73 // session.getWorkspace().getName());
74 // try {
75 // syncJcr(adminSession, username);
76 // checkedUsers.add(username);
77 // } finally {
78 // JcrUtils.logoutQuietly(adminSession);
79 // }
80 // return session;
81 // }
82
83 @Override
84 protected void processNewSession(Session session) {
85 String username = session.getUserID();
86 if (username == null)
87 return;
88 if (session.getUserID().equals(NodeConstants.ROLE_ANONYMOUS))
89 return;
90 // if (session.getUserID().equals(AuthConstants.ROLE_KERNEL))
91 // return;
92 // if (session.getUserID().equals(SecurityConstants.ADMIN_ID))
93 // return;
94
95 if (checkedUsers.contains(username))
96 return;
97 Session adminSession = KernelUtils.openAdminSession(getRepository(), session.getWorkspace().getName());
98 try {
99 syncJcr(adminSession, username);
100 checkedUsers.add(username);
101 } finally {
102 JcrUtils.logoutQuietly(adminSession);
103 }
104 }
105
106 /*
107 * JCR
108 */
109 /** Session is logged out. */
110 private void initJcr(Session adminSession) {
111 try {
112 JcrUtils.mkdirs(adminSession, homeBasePath);
113 // JcrUtils.mkdirs(adminSession, peopleBasePath);
114 adminSession.save();
115
116 JcrUtils.addPrivilege(adminSession, homeBasePath, NodeConstants.ROLE_USER_ADMIN, Privilege.JCR_READ);
117 // JcrUtils.addPrivilege(adminSession, peopleBasePath,
118 // NodeConstants.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 void syncJcr(Session session, String username) {
128 try {
129 Node userHome = NodeUtils.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(NodeTypes.NODE_USER_HOME);
138 userHome.setProperty(NodeNames.LDAP_UID, 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 = NodeUtils.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()
152 // .addNode(JcrUtils.lastPathElement(personPath));
153 // else
154 // personBase = JcrUtils.mkdirs(session, personPath);
155 // userProfile = personBase.addNode(ArgeoNames.ARGEO_PROFILE);
156 // userProfile.addMixin(ArgeoTypes.ARGEO_USER_PROFILE);
157 // userProfile.setProperty(ArgeoNames.ARGEO_USER_ID, username);
158 // // userProfile.setProperty(ArgeoNames.ARGEO_ENABLED, true);
159 // // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_EXPIRED,
160 // // true);
161 // // userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_LOCKED,
162 // // true);
163 // //
164 // userProfile.setProperty(ArgeoNames.ARGEO_CREDENTIALS_NON_EXPIRED,
165 // // true);
166 // session.save();
167 //
168 // JcrUtils.clearAccessControList(session, userProfile.getPath(),
169 // username);
170 // JcrUtils.addPrivilege(session, userProfile.getPath(), username,
171 // Privilege.JCR_READ);
172 // }
173
174 // Remote roles
175 // if (roles != null) {
176 // writeRemoteRoles(userProfile, roles);
177 // }
178 if (session.hasPendingChanges())
179 session.save();
180 // return userProfile;
181 } catch (RepositoryException e) {
182 JcrUtils.discardQuietly(session);
183 throw new CmsException("Cannot sync node security model for " + username, e);
184 }
185 }
186
187 /** Generate path for a new user home */
188 private String generateUserPath(String base, String username) {
189 LdapName dn;
190 try {
191 dn = new LdapName(username);
192 } catch (InvalidNameException e) {
193 throw new CmsException("Invalid name " + username, e);
194 }
195 String userId = dn.getRdn(dn.size() - 1).getValue().toString();
196 int atIndex = userId.indexOf('@');
197 if (atIndex > 0) {
198 String domain = userId.substring(0, atIndex);
199 String name = userId.substring(atIndex + 1);
200 return base + '/' + domain + '/' + name;
201 } else if (atIndex == 0 || atIndex == (userId.length() - 1)) {
202 throw new CmsException("Unsupported username " + userId);
203 } else {
204 return base + '/' + userId;
205 }
206 }
207
208 }