Expand my Community achievements bar.

SOLVED

Add member to group

Avatar

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

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

Hi @DivyaTyagi 

      Are you  using Usermanger Api to add the user.

Could you add your code snippet to check.

 

Regards,

Sanjay

Avatar

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

Avatar

Level 2

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

Avatar

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)


Arun Patidar

Avatar

Level 2

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

Avatar

Level 2

Hi,

@Sanjay_Bangar @arunpatidar @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

Avatar

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?

 



Arun Patidar

Avatar

Correct answer by
Community Advisor

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.

Avatar

Level 2

@lukasz-m

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

Avatar

Community Advisor

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?

Avatar

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.