Expand my Community achievements bar.

SOLVED

Check if user has replicate rights through java code

Avatar

Level 3

Hello guys, How to check if user has replicate rights using java. I found an Interface named AccessControlManager but it don't have any method or property to check replicate privilege of the user.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @AliSyed1 ,

You may check if this help

package com.pc.core.models;

import com.day.cq.wcm.api.Page;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

@Model(adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class CheckStatus {

@ScriptVariable
private ResourceResolver resolver;

@ScriptVariable
private Page currentPage;

public boolean hasReplicationRights() {
Session jcrSession = resolver.adaptTo(Session.class);
if (jcrSession != null) {
try {
// Check for replication permissions
if (jcrSession.hasPermission(currentPage.getPath(), "crx:replicate")) {
return true;
}
} catch (RepositoryException e) {
throw new RuntimeException("Error checking replication rights", e);
}
} else {
throw new RuntimeException("Could not obtain JCR Session");
}
return false;
}
}

Thanks

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @AliSyed1 ,

You may check if this help

package com.pc.core.models;

import com.day.cq.wcm.api.Page;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

@Model(adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class CheckStatus {

@ScriptVariable
private ResourceResolver resolver;

@ScriptVariable
private Page currentPage;

public boolean hasReplicationRights() {
Session jcrSession = resolver.adaptTo(Session.class);
if (jcrSession != null) {
try {
// Check for replication permissions
if (jcrSession.hasPermission(currentPage.getPath(), "crx:replicate")) {
return true;
}
} catch (RepositoryException e) {
throw new RuntimeException("Error checking replication rights", e);
}
} else {
throw new RuntimeException("Could not obtain JCR Session");
}
return false;
}
}

Thanks

Avatar

Community Advisor

you have to get the user groups - ie. what are the Groups the user has assigned (like admin, content-authors, template-editor) etc.

Based on the Group privileges / Permissions, you can find the user replicate access or not.Find the group.

If you want to explicitly find only Replicate try like this:

AccessControlManager acm = session.getAccessControlManager();
Privilege[] publishPrivileges = acm.privilegesFromNames(new String[]{"crx:replicate"});

 

To find user assigned groups and privileges :

public void getUserPrivileges(String userId) throws LoginException, RepositoryException {
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(null);
Session session = resolver.adaptTo(Session.class);
UserManager userManager = resolver.adaptTo(UserManager.class);
User user = (User) userManager.getAuthorizable(userId);

// Retrieve user groups
Iterator<Group> groups = user.memberOf();
while (groups.hasNext()) {
Group group = groups.next();
System.out.println("User is a member of group: " + group.getID());
}

// Retrieve user privileges
AccessControlManager acm = session.getAccessControlManager();
Privilege[] privileges = acm.getPrivileges(user.getPath());
for (Privilege privilege : privileges) {
System.out.println("User has privilege: " + privilege.getName());
}
}