TaskUserContext.java
5.9 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.taskmanagement.TaskManagerException
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.taskmanagement.impl.jcr;
import com.adobe.granite.taskmanagement.TaskManagerException;
import com.adobe.granite.taskmanagement.impl.utils.ServiceLoginUtil;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class TaskUserContext {
private static final Logger logger = LoggerFactory.getLogger(TaskUserContext.class);
private static final String USER_SERVICE = "user";
private static final String NT_REP_SYSTEM_USER = "rep:SystemUser";
private Session session;
private SlingRepository slingRepository;
private List<String> taskAdminGroupName;
protected TaskUserContext() {
}
public TaskUserContext(Session session, SlingRepository repository, List<String> taskAdminGroupName) {
this.session = session;
this.slingRepository = repository;
this.taskAdminGroupName = taskAdminGroupName;
}
public String getCurrentUserId() {
String userid = null;
if (this.session != null) {
userid = this.session.getUserID();
logger.trace("UserId found for caller: [{}]", (Object)userid);
}
return userid;
}
private static UserManager getUserManager(Session session) throws RepositoryException {
if (session instanceof JackrabbitSession) {
return ((JackrabbitSession)session).getUserManager();
}
throw new RuntimeException("Cannot create instance of UserManager from unknown Session type: " + session.getClass());
}
public Session getUserSession() {
return this.session;
}
public boolean isValidUserOrGroup(String id) throws TaskManagerException {
Authorizable authorizable = null;
Session userServiceSession = null;
try {
userServiceSession = ServiceLoginUtil.createWorkflowUserReaderSession(this.slingRepository);
UserManager userManager = TaskUserContext.getUserManager(userServiceSession);
authorizable = userManager.getAuthorizable(id);
}
catch (RepositoryException e) {
throw new TaskManagerException("Error attempting to retrieve an Authoriable from ID: [" + id + "]", (Throwable)e);
}
finally {
if (userServiceSession != null && userServiceSession.isLive()) {
userServiceSession.logout();
}
}
return authorizable != null;
}
public boolean isCurrentUserTaskAdministrator() throws TaskManagerException {
if (this.taskAdminGroupName != null && this.session != null && this.session.getUserID() != null) {
try {
User theUser;
UserManager userManager = TaskUserContext.getUserManager(this.session);
if (this.taskAdminGroupName.contains(this.session.getUserID())) {
return true;
}
Authorizable currentUser = userManager.getAuthorizable(this.session.getUserID());
if (currentUser == null) {
logger.warn("Session with userId {} cannot load itself. Is this a service user who needs access to their own profile?", (Object)this.session.getUserID());
return false;
}
if (!currentUser.isGroup() && (theUser = (User)currentUser).isAdmin()) {
return true;
}
Iterator groupList = currentUser.memberOf();
while (groupList.hasNext()) {
Group g = (Group)groupList.next();
if (!this.taskAdminGroupName.contains(g.getID())) continue;
return true;
}
}
catch (RepositoryException e) {
throw new TaskManagerException("Error determining if current user is a task administrator", (Throwable)e);
}
}
return false;
}
public Set<String> getOwnerIdsForCurrentUser() throws TaskManagerException {
if (this.session != null && this.session.getUserID() != null) {
try {
UserManager userManager = TaskUserContext.getUserManager(this.session);
Authorizable authorizable = userManager.getAuthorizable(this.session.getUserID());
HashSet<String> authorizables = new HashSet<String>();
Iterator groupIterator = authorizable.memberOf();
while (groupIterator.hasNext()) {
Group group = (Group)groupIterator.next();
authorizables.add(group.getID());
}
authorizables.add(this.session.getUserID());
return authorizables;
}
catch (RepositoryException e) {
throw new TaskManagerException("Error getting groups for current user", (Throwable)e);
}
}
return null;
}
}