Highlighted

Admin User/ Administrator Group

Avatar

Avatar

ashwiniv5325199

Avatar

ashwiniv5325199

ashwiniv5325199

08-01-2019

Hi All,

            How to authenticate whether the logged user is admin or belong to administrator group or not in a servlet?

Thanks & Regards,

Ashwini V

Replies

Highlighted

Avatar

Avatar

amitgupta_adbe

Employee

Avatar

amitgupta_adbe

Employee

amitgupta_adbe
Employee

08-01-2019

Highlighted

Avatar

Avatar

ashwiniv5325199

Avatar

ashwiniv5325199

ashwiniv5325199

08-01-2019

Thanks for the response

How to use the session and authenicate the current user is admin or whether the user belong to administrator group?

Because using the below code , it is giving NULL POINTER exception

ResourceResolver resourceResolver = resourceFactory.getAdministrativeResourceResolver(null);   

Session session = resourceResolver.adaptTo(Session.class);

UserManager currentUser = resourceResolver.adaptTo(UserManager.class);

Please suggest some other code to resolve this issue

Thanks & Regards,

Ashwini

Highlighted

Avatar

Avatar

Gaurav-Behl

MVP

Total Posts

1.1K

Likes

226

Correct Answer

281

Avatar

Gaurav-Behl

MVP

Total Posts

1.1K

Likes

226

Correct Answer

281
Gaurav-Behl
MVP

08-01-2019

Admin session is not recommended in 6.x. Use service users to get the session and thereby current user's properties. You can get groups within Authorizable. You may have to modify the code based on AEM version that you use.

something like

Session session = resourceResolver.adaptTo(Session.class);

UserManager userManager = resourceResolver.adaptTo(UserManager.class);

Authorizable auth = userManager.getAuthorizable(session.getUserID());

Iterator<Group> groups = auth.memberOf();

or

      

  //Create a UserManager instance from the session object

  UserManager userManager = ((JackrabbitSession) session).getUserManager();

users= userManager.findAuthorizables("jcr:primaryType", "rep:User");

Refer:

Adobe CQ/Adobe AEM: How to Use Sessions and Resource Resolver through Service Authentication In AEM6

Service Users in AEM

Apache Sling :: Managing users and groups (jackrabbit.usermanager)

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

08-01-2019

Highlighted

Avatar

Avatar

edubey

Total Posts

1.5K

Likes

264

Correct Answer

392

Avatar

edubey

Total Posts

1.5K

Likes

264

Correct Answer

392
edubey

08-01-2019

Make sure to user current user session

Session userSession = slingRequest.getResourceResolver().adaptTo(Session.class);

final UserManager userManager = slingRequest.getResourceResolver().adaptTo(UserManager.class);

final User user = (User) userManager.getAuthorizable(userSession.getUserID());

Boolean isadmin = user.isAdmin()

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

08-01-2019

We have many AEM articles that show use of using a System user - ie - Scott's Digital Community: Querying Adobe Experience Manager 6.4 JCR data

That is your issue. You should be using a System user.