Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Add member to group

DivyaTyagi
Level 2
Level 2

Hi,

 

I am trying to add member to group from code (via service) and user list from getMembers() is successfully fetching the newly added member details, however, when checking same at userAdmin (screen) then newly added member is not present in the group.

Can anyone help me to find what I am missing there?

 

Regards

Divya

1 Accepted Solution
lukaszm
Correct answer by
Level 5
Level 5

Hi @DivyaTyagi, looking on code you have shared, I have few observations:

  1. It looks that you are using 2 different resource resolver, in general this shoud not be an issue, assuming you have approprite level of permissions for both.
    // resource resolver #1
    UserManager userManager = resourceResolverAddMember.adaptTo(UserManager.class);
    // resource resolver #2
    Session currentSession = resourceResolver.adaptTo(Session.class);
  2. You are using cugGroup.getMembers(); to list group memebrs. This is fine, however please keep in mind that without refershing session you can get inacurate results. You can remove below line and you will see that in logs you will get the same results returned by getMembers method.
    log.info("Member added to group ::" + result);
    // currentSession.save();
    log.info("Current session is saved");

In my opinion the root cause of the issue is level of permissions of session you are using. It can be easily check. Please modify your code like below:

log.info("Member added to group ::" + result);
currentSession.save();
log.info("Current session is saved");

// refreshing session
currentSession.refresh(false);

//** To get members of group
Iterator<Authorizable> auth = cugGroup.getMembers();
while(auth.hasNext()) {
    Authorizable authMember = auth.next();
    log.info("Grp Member :: " + authMember.getID());
} 

If you will you see that changes are not saved - then the solution will be to use session that will allow you to modify/add new users to the group.

View solution in original post

11 Replies
bangar50
Level 7
Level 7

Hi @DivyaTyagi 

      Are you  using Usermanger Api to add the user.

Could you add your code snippet to check.

 

Regards,

Sanjay

Shubham_borole
Community Advisor
Community Advisor

@DivyaTyagi - Just making sure, are you using the JackrabbitSession and making sure the session is saved?

A code snippet should help to get more ideas..

 

Thanks

DivyaTyagi
Level 2
Level 2

I am using javax.jcr.Session and saving the session after adding member to group.

Arun_Patidar
Community Advisor
Community Advisor

Hi,

Did you saved/commit the session changes?

 

Note : Please do not use below method if you are checking if user is already part of group or not, this method is very expensive

userGroup.isMember(user)
DivyaTyagi
Level 2
Level 2

Yes , I am saving the current session after adding member to group.

DivyaTyagi
Level 2
Level 2

Hi,

@bangar50 @Arun_Patidar @Shubham_borole 

Thanks for the response.

Below is the code snippet I am using. Code is executing successfully and I am getting recently added member in logs but same is not reflecting at userAdmin (https://author-server.adobecqms.net/useradmin)

 

if(StringUtils.isNotEmpty(page.getProperties().get(CUG_GROUP, String.class))) {
UserManager userManager = resourceResolverAddMember.adaptTo(UserManager.class);
Session currentSession = resourceResolver.adaptTo(Session.class);
try {
Authorizable user = userManager.getAuthorizable(currentSession.getUserID());
Group cugGroup = (Group) userManager.getAuthorizable(page.getProperties().get(CUG_GROUP, String.class));
if(cugGroup != null){
boolean result = cugGroup.addMember(user);
log.info("Member added to group ::" + result);
currentSession.save();
log.info("Current session is saved");
//** To get members of group
Iterator<Authorizable> auth = cugGroup.getMembers();
while(auth.hasNext()){
Authorizable authMember = auth.next();
log.info("Grp Member :: " + authMember.getID());
}
return true;
}
} catch (RepositoryException e) {
// TODO Auto-generated catch block
log.error("Error :: " + e.getMessage());
e.printStackTrace();
}
return false;
}

 

Regards

Divya

Arun_Patidar
Community Advisor
Community Advisor

Hi,

Your code looks fine.

I also tried with groovyconsole. it works for me.

import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;

UserManager userManager = resourceResolver.adaptTo(UserManager.class);
Authorizable user = userManager.getAuthorizable(session.getUserID());
Group cugGroup = (Group) userManager.getAuthorizable("test-group-2");
if(cugGroup != null){
boolean result = cugGroup.addMember(user);
println("Member added to group ::" + result);
save();
println("Current session is saved");
//** To get members of group
}

 

Could you please check error logs, could be an issue while saving?

 

lukaszm
Correct answer by
Level 5
Level 5

Hi @DivyaTyagi, looking on code you have shared, I have few observations:

  1. It looks that you are using 2 different resource resolver, in general this shoud not be an issue, assuming you have approprite level of permissions for both.
    // resource resolver #1
    UserManager userManager = resourceResolverAddMember.adaptTo(UserManager.class);
    // resource resolver #2
    Session currentSession = resourceResolver.adaptTo(Session.class);
  2. You are using cugGroup.getMembers(); to list group memebrs. This is fine, however please keep in mind that without refershing session you can get inacurate results. You can remove below line and you will see that in logs you will get the same results returned by getMembers method.
    log.info("Member added to group ::" + result);
    // currentSession.save();
    log.info("Current session is saved");

In my opinion the root cause of the issue is level of permissions of session you are using. It can be easily check. Please modify your code like below:

log.info("Member added to group ::" + result);
currentSession.save();
log.info("Current session is saved");

// refreshing session
currentSession.refresh(false);

//** To get members of group
Iterator<Authorizable> auth = cugGroup.getMembers();
while(auth.hasNext()) {
    Authorizable authMember = auth.next();
    log.info("Grp Member :: " + authMember.getID());
} 

If you will you see that changes are not saved - then the solution will be to use session that will allow you to modify/add new users to the group.

View solution in original post

DivyaTyagi
Level 2
Level 2

@lukaszm

Thanks for the reply.

I am getting newly added group in logs via below code :-

//** To get members of group
Iterator<Authorizable> auth = cugGroup.getMembers();
while(auth.hasNext()) {
    Authorizable authMember = auth.next();
    log.info("Grp Member :: " + authMember.getID());
} 

However, newly member not displayed to group (as member) at UserAdmin screen (https://publish-server.adobecqms.net/useradmin)

 

Regards

Divya

lukaszm
Level 5
Level 5

Hi @DivyaTyagi, did you refreshed session before getting list of group members? Also could you please confirm that session you are using has permissions to add new user to the group?

DivyaTyagi
Level 2
Level 2

I have added session.refresh(false) as suggested by you but getting same results. We have tried with even granting admin access for the session we are using to add new user to the group.