UserAdapterFactory.java
4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.Group
* org.apache.jackrabbit.api.security.user.User
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.sling.api.adapter.AdapterFactory
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.security.user.internal;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"Adapter for user management related classes."}), @Property(name="adapter.condition", value={"If the Session is a Jackrabbit Session, the ResourceResolver wraps a Jackrabbit Session, or the Resource was obtained from a Jackrabbit Session. In the case adapting a Resource to a User, Group or Authorizable, the Resource's path must correspond to a User, Group or Authorizable."})})
public class UserAdapterFactory
implements AdapterFactory {
private static final Class<UserManager> USER_MANAGER = UserManager.class;
private static final Class<User> USER = User.class;
private static final Class<Group> GROUP = Group.class;
private static final Class<Authorizable> AUTHORIZABLE = Authorizable.class;
@Property(name="adapters")
public static final String[] ADAPTER_CLASSES = new String[]{USER_MANAGER.getName(), USER.getName(), GROUP.getName(), AUTHORIZABLE.getName()};
@Property(name="adaptables")
public static final String[] ADAPTABLE_CLASSES = new String[]{ResourceResolver.class.getName(), JackrabbitSession.class.getName(), Resource.class.getName()};
private final Logger log = LoggerFactory.getLogger(UserAdapterFactory.class);
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (USER_MANAGER == type) {
JackrabbitSession session = this.getSession(adaptable);
if (session != null) {
try {
return (AdapterType)session.getUserManager();
}
catch (RepositoryException e) {
this.log.error("Error while retrieve UserManager from Session.", (Object)e.getMessage());
}
}
} else if (AUTHORIZABLE == type || USER == type || GROUP == type) {
try {
JackrabbitSession session = this.getSession(adaptable);
if (session != null) {
Authorizable auth;
UserManager umgr = session.getUserManager();
if (adaptable instanceof Resource) {
String path = ((Resource)adaptable).getPath();
auth = umgr.getAuthorizableByPath(path);
} else {
auth = umgr.getAuthorizable(session.getUserID());
}
if (auth != null && type.isAssignableFrom(auth.getClass())) {
return (AdapterType)auth;
}
}
}
catch (RepositoryException e) {
this.log.debug("Could not adapt to current User: User is not allowed to read itself or has been deleted while session is open");
}
}
return null;
}
private JackrabbitSession getSession(Object adaptable) {
if (adaptable instanceof ResourceResolver) {
adaptable = ((ResourceResolver)adaptable).adaptTo(Session.class);
}
if (adaptable instanceof Resource) {
adaptable = ((Resource)adaptable).getResourceResolver().adaptTo(Session.class);
}
if (adaptable instanceof JackrabbitSession) {
return (JackrabbitSession)adaptable;
}
return null;
}
}