Expand my Community achievements bar.

SOLVED

Accessing user group membership using HTL

Avatar

Level 3

I'm trying to accomplish something similar found in this post.

I have this in my "scripts.js" file:

use(function() { var resolver = resource.getResourceResolver(); var session = resolver.adaptTo(Packages.javax.jcr.Session); var userManager = resolver.adaptTo(Packages.org.apache.jackrabbit.api.security.user.UserManager); var auth = userManager.getAuthorizable(session.getUserID()); var groups = auth.memberOf(); return { isAnonymous: isAnonymous, groups: groups }; });

 

And in my component, I have this: 

<ul data-sly-use.user="scripts.js" data-sly-list="${user.groups}"> <li>${itemList.index}: ${item}</li> </ul>

It's just displaying an index list of blank items:

  • 0:
  • 1:
  • 2:
  • 3:
  • 4:

Any ideas?

 

Thanks in advance!

1 Accepted Solution

Avatar

Correct answer by
Level 10

Try this, It works

use(function() { var resolver = resource.getResourceResolver(); var session = resolver.adaptTo(Packages.javax.jcr.Session); var userManager = resolver.adaptTo(Packages.org.apache.jackrabbit.api.security.user.UserManager); var auth = userManager.getAuthorizable(session.getUserID()); var groups = auth.memberOf(); return { groups: groups }; });
<ul data-sly-use.user="file.js" data-sly-list="${user.groups}"> <li>${itemList.index}: ${item.principal.name} </li> </ul>

View solution in original post

5 Replies

Avatar

Correct answer by
Level 10

Try this, It works

use(function() { var resolver = resource.getResourceResolver(); var session = resolver.adaptTo(Packages.javax.jcr.Session); var userManager = resolver.adaptTo(Packages.org.apache.jackrabbit.api.security.user.UserManager); var auth = userManager.getAuthorizable(session.getUserID()); var groups = auth.memberOf(); return { groups: groups }; });
<ul data-sly-use.user="file.js" data-sly-list="${user.groups}"> <li>${itemList.index}: ${item.principal.name} </li> </ul>

Avatar

Employee

You can do it even shorter:

  1. use(function() {
  2. var resolver = resource.getResourceResolver();
  3.  
  4. var userManager = resolver.adaptTo(Packages.org.apache.jackrabbit.api.security.user.UserManager);
  5.  
  6. var auth = userManager.getAuthorizable(resolver.getUserID());
  7. var groups = auth.memberOf();
  8.  
  9. return {
  10. groups: groups
  11. }
  12. });

Avatar

Level 3

I appreciate the responses everyone! @edubey, how did you know that "principal.name" was available?