List<JahiaPrincipal> principals = new ArrayList<JahiaPrincipal>();
JahiaUserManagerService userService = ServicesRegistry.getInstance().getJahiaUserManagerService();
JahiaGroupManagerService groupService = ServicesRegistry.getInstance().getJahiaGroupManagerService();
JCRSiteNode site = node.getResolveSite();
Map<String, List<String[]>> m = node.getAclEntries();
Collection<WorkflowRule> rules = getWorkflowRules(node, null);
for (WorkflowRule rule : rules) {
if (rule.getProviderKey().equals(definition.getProvider()) &&
rule.getWorkflowDefinitionKey().equals(definition.getKey())) {
Map<String, String> perms = workflowPermissions.get(rule.getWorkflowDefinitionKey());
if (perms != null) {
String permName = perms.get(activityName);
if (permName != null) {
if (permName.indexOf("$") > -1) {
Workflow w = getWorkflow(definition.getProvider(), processId, null);
if (w != null) {
for (Map.Entry<String, Object> entry : w.getVariables().entrySet()) {
if (entry.getValue() instanceof List) {
@SuppressWarnings("unchecked")
List<Object> variable = (List<Object>) entry.getValue();
for (Object workflowVariable : variable) {
if (workflowVariable instanceof WorkflowVariable) {
String v = ((WorkflowVariable) workflowVariable).getValue();
permName = permName.replace("$" + entry.getKey(), v);
}
}
}
}
}
}
try {
Set<String> s = new HashSet<String>();
try {
String path = "/permissions" + permName;
while (path.length() >= "/permissions".length()) {
JCRNodeWrapper permissionNode = session.getNode(path);
for (PropertyIterator iterator = permissionNode.getWeakReferences(
"j:permissions"); iterator.hasNext();) {
Property prop = iterator.nextProperty();
Node roleNode = prop.getParent();
s.add(roleNode.getName());
}
path = StringUtils.substringBeforeLast(path, "/");
}
} catch (PathNotFoundException e) {
logger.warn("Unable to find the node for the permission " + permName);
}
for (Map.Entry<String, List<String[]>> entry : m.entrySet()) {
for (String[] strings : entry.getValue()) {
if (strings[1].equals("GRANT")) {
if (s.contains(strings[2])) {
String principal = entry.getKey();
final String principalName = principal.substring(2);
if (principal.charAt(0) == 'u') {
JahiaUser jahiaUser = userService.lookupUser(principalName);
principals.add(jahiaUser);
} else if (principal.charAt(0) == 'g') {
JahiaGroup group = groupService.lookupGroup(site.getID(),
principalName);
principals.add(group);
}
}
}