Développer ma barre des réalisations de la Communauté.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
RÉSOLU

Check if user has replicate rights through java code

Avatar

Level 4

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 solution acceptée

Avatar

Réponse correcte par
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

Voir la solution dans l'envoi d'origine

3 Replies

Avatar

Community Advisor

Hi @AliSyed1 

you can check 

https://developer.adobe.com/experience-manager/reference-materials/spec/jsr170/javadocs/jcr-2.0/java...

 the Privilege would contain crx:replicate

 

Arun Patidar

AEM LinksLinkedIn

Avatar

Réponse correcte par
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());
}
}