Extract groups that have no users.

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Active Member

Extract groups that have no users.

Jump to solution

Hello,

I want to extract groups that have no users.

The execution of the script JavaScript console runs until it crashes, Because there are several groups in Alfresco but I know how to deal with this problem.


var pagingGroup = utils.createPaging(-1,0);
var siteGroups = groups.getGroups(null, pagingGroup);

for (var i=0; i<siteGroups.length; i++)
{
userCount = siteGroups[i].getUserCount();
if(userCount==0){
logger.log(siteGroups[i].displayName);
}

}

Any help please!!

1 Solution

Accepted Solutions
Highlighted
Moderator
Moderator

Re: Extract groups that have no users.

Jump to solution

There are several ways i can think of. And your code works as well but crashes. 


1- Using AuthorityService in a java backed webscript:

 

final Set<String> groups = authorityService.getAllAuthoritiesInZone("APP.DEFAULT",AuthorityType.GROUP);
for (final String groupFQN : groups) {
  final Set<String> users = authorityService.getContainedAuthorities(AuthorityType.USER, groupFQN, true);
  LOGGER.info("Users in '{}' group are: {}", groupFQN, users);
  if(users.isEmpty()) {
    LOGGER.info("########## Group '{}' has no users ########", groupFQN);
  }
}


2- Using javascript with zone filtering:

//When targetting to default zone.

var paging = utils.createPaging(-1,0);
//When targetting to default zone. var defaultZone = groups.getGroupsInZone("*", "APP.DEFAULT", paging, "displayName"); for (var each=0; each<defaultZone.length; each++) { var userCount = defaultZone[each].getUserCount(); if(userCount==0){ logger.log("Group '" + defaultZone[each].displayName+"' doesn't have members"); } }

 

//When targetting to share site zone.

var paging = utils.createPaging(-1,0);
//When targetting to site zone. var siteGroups = groups.getGroupsInZone("*", "APP.SHARE", paging, "displayName"); for (var each=0; each<siteGroups.length; each++) { var userCount = siteGroups[each].getUserCount(); if(userCount==0){ logger.log("Group '" + siteGroups[each].displayName+"' doesn't have members"); } }

You may be looking for groups in APP.DEFAULT zone most probably, so no need to get all the groups from all the zones. This might be reason why your code takes a lot of time to complete and browser crashes.

Depending on number of groups and number of sites the processing will take time and browser will hang if you try to get all the groups via JS.
If you still intend to fetch groups from all zones then i would suggest to go with Java backed webscript and use the approach given in option 1 above

Refer this doc for more details: https://docs.alfresco.com/4.1/references/API-JS-getGroupsInZone.html

3- Using REST API:

- Get the groups by calling: http://127.0.0.1:8080/alfresco/service/api/groups?shortNameFilter=*&zone=APP.DEFAULT&maxItems=250&so...


- Parse the json response and iterate by each group FQN and then call the following api to get the members:
http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/groups/{id}/members, where id is the FQN of group.

e.g.: http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/groups/GROUP_ALFRESCO_ADMINISTRATORS/members


- Parse the response and calculate whether there are users in the selected group

~Abhinav
(ACSCE, AWS SAA-C02, GAIQ)

View solution in original post

2 Replies
Highlighted
Moderator
Moderator

Re: Extract groups that have no users.

Jump to solution

There are several ways i can think of. And your code works as well but crashes. 


1- Using AuthorityService in a java backed webscript:

 

final Set<String> groups = authorityService.getAllAuthoritiesInZone("APP.DEFAULT",AuthorityType.GROUP);
for (final String groupFQN : groups) {
  final Set<String> users = authorityService.getContainedAuthorities(AuthorityType.USER, groupFQN, true);
  LOGGER.info("Users in '{}' group are: {}", groupFQN, users);
  if(users.isEmpty()) {
    LOGGER.info("########## Group '{}' has no users ########", groupFQN);
  }
}


2- Using javascript with zone filtering:

//When targetting to default zone.

var paging = utils.createPaging(-1,0);
//When targetting to default zone. var defaultZone = groups.getGroupsInZone("*", "APP.DEFAULT", paging, "displayName"); for (var each=0; each<defaultZone.length; each++) { var userCount = defaultZone[each].getUserCount(); if(userCount==0){ logger.log("Group '" + defaultZone[each].displayName+"' doesn't have members"); } }

 

//When targetting to share site zone.

var paging = utils.createPaging(-1,0);
//When targetting to site zone. var siteGroups = groups.getGroupsInZone("*", "APP.SHARE", paging, "displayName"); for (var each=0; each<siteGroups.length; each++) { var userCount = siteGroups[each].getUserCount(); if(userCount==0){ logger.log("Group '" + siteGroups[each].displayName+"' doesn't have members"); } }

You may be looking for groups in APP.DEFAULT zone most probably, so no need to get all the groups from all the zones. This might be reason why your code takes a lot of time to complete and browser crashes.

Depending on number of groups and number of sites the processing will take time and browser will hang if you try to get all the groups via JS.
If you still intend to fetch groups from all zones then i would suggest to go with Java backed webscript and use the approach given in option 1 above

Refer this doc for more details: https://docs.alfresco.com/4.1/references/API-JS-getGroupsInZone.html

3- Using REST API:

- Get the groups by calling: http://127.0.0.1:8080/alfresco/service/api/groups?shortNameFilter=*&zone=APP.DEFAULT&maxItems=250&so...


- Parse the json response and iterate by each group FQN and then call the following api to get the members:
http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/groups/{id}/members, where id is the FQN of group.

e.g.: http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/groups/GROUP_ALFRESCO_ADMINISTRATORS/members


- Parse the response and calculate whether there are users in the selected group

~Abhinav
(ACSCE, AWS SAA-C02, GAIQ)

View solution in original post

Active Member

Re: Extract groups that have no users.

Jump to solution

Thank you very much Smiley Happy