Skip to main content
此主题已关闭回复。
joerghoh
joerghoh回答
Adobe Employee
6月 3, 2022 5:25 下午 UTC

Hi,

 

please don't do that. Imagine what happens if the result of such a request is stored in the dispatcher cache and returned to the next user requesting the same page.

7回复

Himanshu_Jain
Community Advisor
Community Advisor
6月 2, 2022 10:29 上午 UTC
Dinu_Arya
Dinu_Arya作者
Level 6
6月 2, 2022 10:40 上午 UTC

Thank you. But I have to get current user's(whoever is logged in) group . @himanshu_jain 

翻译
arunpatidar
Community Advisor
Community Advisor
6月 2, 2022 10:47 上午 UTC

Hi,

You cannot get it in HTL. You need to write a java or javascript use api for https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SimpleGetGroup.java 

User currentUser = request.getResourceResolver().adaptTo(User.class);

and you make sure the page/content is not cached in case of logged in user otherwise you will have same version of content for all the users (cached version)

翻译
Arun Patidar
sunil_kumar_
Level 5
6月 2, 2022 7:38 下午 UTC

Hi @dinu_arya ,
You can get user information using "UserManagementService" in sling model and then in sightly.

Consider this code is written in sling model.

 

Declare or Inject required variables and Services

private String userID;

private String userName;
private List<String> userGroupsList =new ArrayList<>();

@SlingObject
ResourceResolver resourceResolver;

@OSGiService
UserManagementService userManagementService;

 In @PostConstruct use this code to get User and User's Groups information

String anonymousID = userManagementService != null ? userManagementService.getAnonymousId() : UserConstants.DEFAULT_ANONYMOUS_ID;
userID=resourceResolver.getUserID();
UserManager userManager=resourceResolver.adaptTo(UserManager.class);

if(userManager!=null){
Iterator<Group> userGroups= userManager.getAuthorizable(userID).memberOf();
while (userGroups.hasNext()){
Group group=userGroups.next();
userGroupsList.add(group.getID());
}

}
UserPropertiesManager userPropertiesManager=resourceResolver.adaptTo(UserPropertiesManager.class);
if (userPropertiesManager != null) {
UserProperties userProperties = userPropertiesManager.getUserProperties(userID, "profile");
for (String userProperty : userProperties.getPropertyNames()) {
LOG.info("\n User Properties {} : {} ",userProperty,userProperties.getProperty(userProperty));
}
userName=userProperties.getDisplayName();

}

Get working demo code from get.
https://github.com/aemgeeks1212/aemgeeks/blob/master/core/src/main/java/com/aem/geeks/core/models/impl/UserInfoImpl.java

https://github.com/aemgeeks1212/aemgeeks/blob/master/core/src/main/java/com/aem/geeks/core/models/UserInfo.java

https://github.com/aemgeeks1212/aemgeeks/blob/master/ui.apps/src/main/content/jcr_root/apps/aemgeeks/components/content/userinfo/userinfo.html

 

翻译
joerghoh
Adobe Employee
joerghohAdobe Employee回答
Adobe Employee
6月 3, 2022 5:25 下午 UTC

Hi,

 

please don't do that. Imagine what happens if the result of such a request is stored in the dispatcher cache and returned to the next user requesting the same page.

翻译
sunil_kumar_
Level 5
6月 3, 2022 5:42 下午 UTC

Agree @joerghoh 

翻译