Closed user group for list of components
We want to add a list component item to the home page. Users who are members of a specific Closed User Group should see it. Users who are not part of the CUG should not.Is it possible?
We want to add a list component item to the home page. Users who are members of a specific Closed User Group should see it. Users who are not part of the CUG should not.Is it possible?
Hi @dillibabu77
Yes, it is possible to add a list component item to the home page and show it only to users who are members of a specific Closed User Group (CUG) in Adobe Experience Manager (AEM).
1. Create a Closed User Group: In AEM, you can create a Closed User Group by following the steps mentioned in the [https://experienceleague.adobe.com/en/docs/experience-manager-65/content/security/closed-user-groups ]. This involves creating a group and assigning users to that group.
2. Configure the List Component: Once you have created the CUG, you need to configure the list component to show or hide based on the user's membership status. This can be done using AEM's component policies.
- Edit the list component and open the component policy editor.
- Add a new policy configuration.
- In the policy configuration, set the visibility rule based on the user's membership in the CUG.
- For example, you can use the "User Group" condition and select the CUG group you created. Set the visibility to "Show" for this condition.
- Save the policy configuration.
3. Apply the List Component to the Home Page: Finally, you need to apply the list component to the home page and ensure that the component policy is applied.
- Edit the home page in AEM.
- Drag and drop the list component onto the desired location on the page.
- Save the changes.
when users access the home page, the list component will only be visible to those who are members of the specific CUG. Users who are not part of the CUG will not see the component.
https://experienceleague.adobe.com/en/docs/experience-manager-65/content/security/closed-user-groups
https://experienceleague.adobe.com/en/docs/experience-manager-learn/assets/advanced/closed-user-groups
package com.example.core.models;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.RepositoryException;
@Model(adaptables = Resource.class)
public class CUGHelper {
@Inject
private ResourceResolver resourceResolver;
private static final String CUG_GROUP_NAME = "your-cug-group-name";
private boolean isUserInCUG;
@PostConstruct
protected void init() {
isUserInCUG = checkIfUserInCUG();
}
public boolean isUserInCUG() {
return isUserInCUG;
}
private boolean checkIfUserInCUG() {
boolean isMember = false;
try {
UserManager userManager = resourceResolver.adaptTo(UserManager.class);
Authorizable user = userManager.getAuthorizable(resourceResolver.getUserID());
if (user != null && user.isGroup()) {
isMember = ((Group) user).isMember(userManager.getAuthorizable(CUG_GROUP_NAME));
}
} catch (RepositoryException e) {
// handle exception
}
return isMember;
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.