2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.jcr
;
18 import java
.util
.List
;
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
;
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
;
37 * Manages data expected by the Argeo security model, such as user home and
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
46 /** The home base path. */
47 private String homeBasePath
= "/home";
49 public synchronized Node
sync(Session session
, String username
,
51 // TODO check user name validity (e.g. should not start by ROLE_)
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
);
63 JcrUtils
.clearAccessControList(session
, homePath
, username
);
64 JcrUtils
.addPrivilege(session
, homePath
, username
,
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();
76 // writeRemoteRoles(userHome, roles);
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
,
90 userProfile
.setProperty(ArgeoNames
.ARGEO_ACCOUNT_NON_LOCKED
,
92 userProfile
.setProperty(
93 ArgeoNames
.ARGEO_CREDENTIALS_NON_EXPIRED
, true);
96 JcrUtils
.clearAccessControList(session
, userProfile
.getPath(),
98 JcrUtils
.addPrivilege(session
, userProfile
.getPath(), username
,
101 VersionManager versionManager
= session
.getWorkspace()
102 .getVersionManager();
103 if (versionManager
.isCheckedOut(userProfile
.getPath()))
104 versionManager
.checkin(userProfile
.getPath());
110 writeRemoteRoles(userProfile
, roles
);
113 } catch (RepositoryException e
) {
114 JcrUtils
.discardQuietly(session
);
115 throw new ArgeoException("Cannot sync node security model for "
120 /** Generate path for a new user home */
121 protected String
generateUserPath(String base
, String username
) {
122 int atIndex
= username
.indexOf('@');
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) + '/'
129 } else if (atIndex
== 0 || atIndex
== (username
.length() - 1)) {
130 throw new ArgeoException("Unsupported username " + username
);
132 return base
+ '/' + JcrUtils
.firstCharsToPath(username
, 2) + '/'
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())
147 for (int i
= 0; i
< remoteRoles
.length
; i
++)
148 if (!remoteRoles
[i
].getString().equals(roles
.get(i
)))
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
));
169 public void setHomeBasePath(String homeBasePath
) {
170 this.homeBasePath
= homeBasePath
;