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
.AccessControlPolicy
;
18 import javax
.jcr
.security
.AccessControlPolicyIterator
;
19 import javax
.jcr
.security
.Privilege
;
20 import javax
.security
.auth
.Subject
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.apache
.jackrabbit
.api
.security
.JackrabbitAccessControlList
;
25 import org
.apache
.jackrabbit
.api
.security
.JackrabbitAccessControlManager
;
26 import org
.apache
.jackrabbit
.api
.security
.JackrabbitAccessControlPolicy
;
27 import org
.apache
.jackrabbit
.api
.security
.user
.Group
;
28 import org
.apache
.jackrabbit
.api
.security
.user
.User
;
29 import org
.apache
.jackrabbit
.api
.security
.user
.UserManager
;
30 import org
.apache
.jackrabbit
.core
.DefaultSecurityManager
;
31 import org
.apache
.jackrabbit
.core
.security
.SecurityConstants
;
32 import org
.apache
.jackrabbit
.core
.security
.SystemPrincipal
;
33 import org
.apache
.jackrabbit
.core
.security
.authorization
.WorkspaceAccessManager
;
34 import org
.argeo
.ArgeoException
;
35 import org
.argeo
.jcr
.JcrUtils
;
36 import org
.springframework
.security
.Authentication
;
37 import org
.springframework
.security
.GrantedAuthority
;
39 /** Intermediary class in order to have a consistent naming in config files. */
40 public class ArgeoSecurityManager
extends DefaultSecurityManager
{
41 public final static String HOME_BASE_PATH
= "/home";
43 private Log log
= LogFactory
.getLog(ArgeoSecurityManager
.class);
46 /** Since this is called once when the session is created, we take the opportunity to synchronize Spring and Jackrabbit users and groups.*/
47 public String
getUserID(Subject subject
, String workspaceName
)
48 throws RepositoryException
{
49 long begin
= System
.currentTimeMillis();
51 if (!subject
.getPrincipals(SystemPrincipal
.class).isEmpty())
52 return super.getUserID(subject
, workspaceName
);
54 Authentication authen
;
55 Set
<Authentication
> authens
= subject
56 .getPrincipals(Authentication
.class);
57 if (authens
.size() == 0)
58 throw new ArgeoException("No Spring authentication found in "
61 authen
= authens
.iterator().next();
63 UserManager systemUm
= getSystemUserManager(workspaceName
);
65 String userId
= authen
.getName();
66 User user
= (User
) systemUm
.getAuthorizable(userId
);
68 user
= systemUm
.createUser(userId
, authen
.getCredentials()
69 .toString(), authen
, null);
70 log
.info(userId
+ " added as " + user
);
73 setHomeNodeAuthorizations(user
);
76 List
<String
> userGroupIds
= new ArrayList
<String
>();
77 for (GrantedAuthority ga
: authen
.getAuthorities()) {
78 Group group
= (Group
) systemUm
.getAuthorizable(ga
.getAuthority());
80 group
= systemUm
.createGroup(ga
.getAuthority());
81 log
.info(ga
.getAuthority() + " added as " + group
);
83 if (!group
.isMember(user
))
84 group
.addMember(user
);
85 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
.isTraceEnabled())
97 log
.trace("Spring and Jackrabbit Security synchronized for user "
98 + userId
+ " in " + (System
.currentTimeMillis() - begin
)
103 protected void setHomeNodeAuthorizations(User user
) {
104 // give all privileges on user home
105 // FIXME: fails on an empty repo
106 String userId
= "<not yet set>";
108 userId
= user
.getID();
109 Node userHome
= null;
111 userHome
= JcrUtils
.getUserHome(getSystemSession(), userId
);
112 if (userHome
== null)
113 userHome
= JcrUtils
.createUserHome(getSystemSession(),
114 HOME_BASE_PATH
, userId
);
115 } catch (Exception e
) {
119 if (userHome
!= null) {
120 String path
= userHome
.getPath();
121 Principal principal
= user
.getPrincipal();
123 JackrabbitAccessControlManager acm
= (JackrabbitAccessControlManager
) getSystemSession()
124 .getAccessControlManager();
125 JackrabbitAccessControlPolicy
[] ps
= acm
126 .getApplicablePolicies(principal
);
127 if (ps
.length
== 0) {
128 log
.warn("No ACL found for " + user
);
132 JackrabbitAccessControlList list
= (JackrabbitAccessControlList
) ps
[0];
135 Privilege
[] privileges
= new Privilege
[] { acm
136 .privilegeFromName(Privilege
.JCR_ALL
) };
137 Map
<String
, Value
> restrictions
= new HashMap
<String
, Value
>();
138 ValueFactory vf
= getSystemSession().getValueFactory();
139 restrictions
.put("rep:nodePath",
140 vf
.createValue(path
, PropertyType
.PATH
));
141 restrictions
.put("rep:glob", vf
.createValue("*"));
142 list
.addEntry(principal
, privileges
, true /* allow or deny */,
145 } catch (Exception e
) {
147 log
.warn("Cannot set authorization on user node for " + userId
148 + ": " + e
.getMessage());
154 protected WorkspaceAccessManager
createDefaultWorkspaceAccessManager() {
155 WorkspaceAccessManager wam
= super
156 .createDefaultWorkspaceAccessManager();
157 return new ArgeoWorkspaceAccessManagerImpl(wam
);
160 private class ArgeoWorkspaceAccessManagerImpl
implements SecurityConstants
,
161 WorkspaceAccessManager
{
162 private final WorkspaceAccessManager wam
;
164 // private String defaultWorkspace;
166 public ArgeoWorkspaceAccessManagerImpl(WorkspaceAccessManager wam
) {
171 public void init(Session systemSession
) throws RepositoryException
{
172 wam
.init(systemSession
);
173 // defaultWorkspace = ((RepositoryImpl) getRepository()).getConfig()
174 // .getDefaultWorkspaceName();
177 public void close() throws RepositoryException
{
180 public boolean grants(Set
<Principal
> principals
, String workspaceName
)
181 throws RepositoryException
{
182 // everybody has access to all workspaces
183 // TODO: implements finer access to workspaces
186 // anonymous has access to the default workspace (required for
187 // remoting which does a default login when initializing the
189 // Boolean anonymous = false;
190 // for (Principal principal : principals)
191 // if (principal instanceof AnonymousPrincipal)
194 // if (anonymous && workspaceName.equals(defaultWorkspace))
197 // return wam.grants(principals, workspaceName);