]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/jcr/SimpleJcrSecurityModel.java
Reduce CMS size
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / jcr / SimpleJcrSecurityModel.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.jcr;
17
18 import java.util.List;
19
20 import javax.jcr.Node;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.Session;
23 import javax.jcr.Value;
24 import javax.jcr.security.Privilege;
25 import javax.jcr.version.VersionManager;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.argeo.ArgeoException;
30 import org.argeo.jcr.ArgeoJcrConstants;
31 import org.argeo.jcr.ArgeoNames;
32 import org.argeo.jcr.ArgeoTypes;
33 import org.argeo.jcr.JcrUtils;
34 import org.argeo.jcr.UserJcrUtils;
35
36 /**
37 * Manages data expected by the Argeo security model, such as user home and
38 * profile.
39 */
40 public class SimpleJcrSecurityModel implements JcrSecurityModel {
41 private final static Log log = LogFactory
42 .getLog(SimpleJcrSecurityModel.class);
43 // ArgeoNames not implemented as interface in order to ease derivation by
44 // Jackrabbit bundles
45
46 /** The home base path. */
47 private String homeBasePath = "/home";
48
49 public synchronized Node sync(Session session, String username,
50 List<String> roles) {
51 // TODO check user name validity (e.g. should not start by ROLE_)
52
53 try {
54 Node userHome = UserJcrUtils.getUserHome(session, username);
55 if (userHome == null) {
56 String homePath = generateUserPath(homeBasePath, username);
57 userHome = JcrUtils.mkdirs(session, homePath);
58 // userHome = JcrUtils.mkfolders(session, homePath);
59 userHome.addMixin(ArgeoTypes.ARGEO_USER_HOME);
60 userHome.setProperty(ArgeoNames.ARGEO_USER_ID, username);
61 session.save();
62
63 JcrUtils.clearAccessControList(session, homePath, username);
64 JcrUtils.addPrivilege(session, homePath, username,
65 Privilege.JCR_ALL);
66 } else {
67 // for backward compatibility with pre 1.0 security model
68 if (userHome.hasNode(ArgeoNames.ARGEO_PROFILE)) {
69 userHome.getNode(ArgeoNames.ARGEO_PROFILE).remove();
70 userHome.getSession().save();
71 }
72 }
73
74 // Remote roles
75 if (roles != null) {
76 // writeRemoteRoles(userHome, roles);
77 }
78
79 Node userProfile = UserJcrUtils.getUserProfile(session, username);
80 if (userProfile == null) {
81 String personPath = generateUserPath(
82 ArgeoJcrConstants.PEOPLE_BASE_PATH, username);
83 Node personBase = JcrUtils.mkdirs(session, personPath);
84 userProfile = personBase.addNode(ArgeoNames.ARGEO_PROFILE);
85 userProfile.addMixin(ArgeoTypes.ARGEO_USER_PROFILE);
86 userProfile.setProperty(ArgeoNames.ARGEO_USER_ID, username);
87 userProfile.setProperty(ArgeoNames.ARGEO_ENABLED, true);
88 userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_EXPIRED,
89 true);
90 userProfile.setProperty(ArgeoNames.ARGEO_ACCOUNT_NON_LOCKED,
91 true);
92 userProfile.setProperty(
93 ArgeoNames.ARGEO_CREDENTIALS_NON_EXPIRED, true);
94 session.save();
95
96 JcrUtils.clearAccessControList(session, userProfile.getPath(),
97 username);
98 JcrUtils.addPrivilege(session, userProfile.getPath(), username,
99 Privilege.JCR_READ);
100
101 VersionManager versionManager = session.getWorkspace()
102 .getVersionManager();
103 if (versionManager.isCheckedOut(userProfile.getPath()))
104 versionManager.checkin(userProfile.getPath());
105
106 }
107
108 // Remote roles
109 if (roles != null) {
110 writeRemoteRoles(userProfile, roles);
111 }
112 return userProfile;
113 } catch (RepositoryException e) {
114 JcrUtils.discardQuietly(session);
115 throw new ArgeoException("Cannot sync node security model for "
116 + username, e);
117 }
118 }
119
120 /** Generate path for a new user home */
121 protected String generateUserPath(String base, String username) {
122 int atIndex = username.indexOf('@');
123 if (atIndex > 0) {
124 String domain = username.substring(0, atIndex);
125 String name = username.substring(atIndex + 1);
126 return base + '/' + JcrUtils.firstCharsToPath(domain, 2) + '/'
127 + domain + '/' + JcrUtils.firstCharsToPath(name, 2) + '/'
128 + name;
129 } else if (atIndex == 0 || atIndex == (username.length() - 1)) {
130 throw new ArgeoException("Unsupported username " + username);
131 } else {
132 return base + '/' + JcrUtils.firstCharsToPath(username, 2) + '/'
133 + username;
134 }
135 }
136
137 /** Write remote roles used by remote access in the home directory */
138 protected void writeRemoteRoles(Node userHome, List<String> roles)
139 throws RepositoryException {
140 boolean writeRoles = false;
141 if (userHome.hasProperty(ArgeoNames.ARGEO_REMOTE_ROLES)) {
142 Value[] remoteRoles = userHome.getProperty(
143 ArgeoNames.ARGEO_REMOTE_ROLES).getValues();
144 if (remoteRoles.length != roles.size())
145 writeRoles = true;
146 else
147 for (int i = 0; i < remoteRoles.length; i++)
148 if (!remoteRoles[i].getString().equals(roles.get(i)))
149 writeRoles = true;
150 } else
151 writeRoles = true;
152
153 if (writeRoles) {
154 userHome.getSession().getWorkspace().getVersionManager()
155 .checkout(userHome.getPath());
156 String[] roleIds = roles.toArray(new String[roles.size()]);
157 userHome.setProperty(ArgeoNames.ARGEO_REMOTE_ROLES, roleIds);
158 JcrUtils.updateLastModified(userHome);
159 userHome.getSession().save();
160 userHome.getSession().getWorkspace().getVersionManager()
161 .checkin(userHome.getPath());
162 if (log.isDebugEnabled())
163 log.debug("Wrote remote roles " + roles + " for "
164 + userHome.getProperty(ArgeoNames.ARGEO_USER_ID));
165 }
166
167 }
168
169 public void setHomeBasePath(String homeBasePath) {
170 this.homeBasePath = homeBasePath;
171 }
172
173 }