1 package org
.argeo
.security
.jackrabbit
;
3 import java
.security
.Principal
;
4 import java
.util
.ArrayList
;
5 import java
.util
.HashMap
;
6 import java
.util
.Iterator
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.PropertyType
;
13 import javax
.jcr
.RepositoryException
;
14 import javax
.jcr
.Session
;
15 import javax
.jcr
.Value
;
16 import javax
.jcr
.ValueFactory
;
17 import javax
.jcr
.security
.Privilege
;
18 import javax
.security
.auth
.Subject
;
20 import org
.apache
.commons
.logging
.Log
;
21 import org
.apache
.commons
.logging
.LogFactory
;
22 import org
.apache
.jackrabbit
.api
.security
.JackrabbitAccessControlList
;
23 import org
.apache
.jackrabbit
.api
.security
.JackrabbitAccessControlManager
;
24 import org
.apache
.jackrabbit
.api
.security
.JackrabbitAccessControlPolicy
;
25 import org
.apache
.jackrabbit
.api
.security
.user
.Group
;
26 import org
.apache
.jackrabbit
.api
.security
.user
.User
;
27 import org
.apache
.jackrabbit
.api
.security
.user
.UserManager
;
28 import org
.apache
.jackrabbit
.core
.DefaultSecurityManager
;
29 import org
.apache
.jackrabbit
.core
.security
.SecurityConstants
;
30 import org
.apache
.jackrabbit
.core
.security
.authorization
.WorkspaceAccessManager
;
31 import org
.argeo
.ArgeoException
;
32 import org
.argeo
.jcr
.JcrUtils
;
33 import org
.springframework
.security
.Authentication
;
34 import org
.springframework
.security
.GrantedAuthority
;
36 /** Intermediary class in order to have a consistent naming in config files. */
37 public class ArgeoSecurityManager
extends DefaultSecurityManager
{
38 private Log log
= LogFactory
.getLog(ArgeoSecurityManager
.class);
41 * Since this is called once when the session is created, we take the
42 * opportunity to make sure that Jackrabbit users and groups reflect Spring
43 * Security name and authorities.
46 public String
getUserID(Subject subject
, String workspaceName
)
47 throws RepositoryException
{
48 long begin
= System
.currentTimeMillis();
50 if (log
.isTraceEnabled())
52 // skip Jackrabbit system user
53 if (!subject
.getPrincipals(ArgeoSystemPrincipal
.class).isEmpty())
54 return super.getUserID(subject
, workspaceName
);
56 Authentication authen
;
57 Set
<Authentication
> authens
= subject
58 .getPrincipals(Authentication
.class);
59 if (authens
.size() == 0)
60 throw new ArgeoException("No Spring authentication found in "
63 authen
= authens
.iterator().next();
65 UserManager systemUm
= getSystemUserManager(workspaceName
);
67 String userId
= authen
.getName();
68 User user
= (User
) systemUm
.getAuthorizable(userId
);
70 user
= systemUm
.createUser(userId
, authen
.getCredentials()
71 .toString(), authen
, null);
72 setSecurityHomeAuthorizations(user
);
73 log
.info(userId
+ " added as " + user
);
77 List
<String
> userGroupIds
= new ArrayList
<String
>();
78 for (GrantedAuthority ga
: authen
.getAuthorities()) {
79 Group group
= (Group
) systemUm
.getAuthorizable(ga
.getAuthority());
81 group
= systemUm
.createGroup(ga
.getAuthority());
82 log
.info(ga
.getAuthority() + " added as " + group
);
84 if (!group
.isMember(user
))
85 group
.addMember(user
);
86 userGroupIds
.add(ga
.getAuthority());
89 // check if user has not been removed from some groups
90 for (Iterator
<Group
> it
= user
.declaredMemberOf(); it
.hasNext();) {
91 Group group
= it
.next();
92 if (!userGroupIds
.contains(group
.getID()))
93 group
.removeMember(user
);
96 if (log
.isDebugEnabled())
97 log
.debug("Spring and Jackrabbit Security synchronized for user "
98 + userId
+ " in " + (System
.currentTimeMillis() - begin
)
103 protected synchronized void setSecurityHomeAuthorizations(User user
) {
104 // give read privileges on user home
105 String userId
= "<not yet set>";
107 userId
= user
.getID();
108 Node userHome
= JcrUtils
.getUserHome(getSystemSession(), userId
);
109 if (userHome
== null)
110 throw new ArgeoException("No security home available for user "
113 String path
= userHome
.getPath();
114 Principal principal
= user
.getPrincipal();
116 JackrabbitAccessControlManager acm
= (JackrabbitAccessControlManager
) getSystemSession()
117 .getAccessControlManager();
118 JackrabbitAccessControlPolicy
[] ps
= acm
119 .getApplicablePolicies(principal
);
120 if (ps
.length
== 0) {
121 // log.warn("No ACL found for " + user);
125 JackrabbitAccessControlList list
= (JackrabbitAccessControlList
) ps
[0];
128 Privilege
[] privileges
= new Privilege
[] { acm
129 .privilegeFromName(Privilege
.JCR_READ
) };
130 Map
<String
, Value
> restrictions
= new HashMap
<String
, Value
>();
131 ValueFactory vf
= getSystemSession().getValueFactory();
132 restrictions
.put("rep:nodePath",
133 vf
.createValue(path
, PropertyType
.PATH
));
134 restrictions
.put("rep:glob", vf
.createValue("*"));
135 list
.addEntry(principal
, privileges
, true /* allow or deny */,
137 } catch (Exception e
) {
139 throw new ArgeoException(
140 "Cannot set authorization on security home for " + userId
141 + ": " + e
.getMessage());
147 protected WorkspaceAccessManager
createDefaultWorkspaceAccessManager() {
148 WorkspaceAccessManager wam
= super
149 .createDefaultWorkspaceAccessManager();
150 return new ArgeoWorkspaceAccessManagerImpl(wam
);
153 private class ArgeoWorkspaceAccessManagerImpl
implements SecurityConstants
,
154 WorkspaceAccessManager
{
155 private final WorkspaceAccessManager wam
;
157 // private String defaultWorkspace;
159 public ArgeoWorkspaceAccessManagerImpl(WorkspaceAccessManager wam
) {
164 public void init(Session systemSession
) throws RepositoryException
{
165 wam
.init(systemSession
);
166 // defaultWorkspace = ((RepositoryImpl) getRepository()).getConfig()
167 // .getDefaultWorkspaceName();
170 public void close() throws RepositoryException
{
173 public boolean grants(Set
<Principal
> principals
, String workspaceName
)
174 throws RepositoryException
{
175 // everybody has access to all workspaces
176 // TODO: implements finer access to workspaces
179 // anonymous has access to the default workspace (required for
180 // remoting which does a default login when initializing the
182 // Boolean anonymous = false;
183 // for (Principal principal : principals)
184 // if (principal instanceof AnonymousPrincipal)
187 // if (anonymous && workspaceName.equals(defaultWorkspace))
190 // return wam.grants(principals, workspaceName);