1. read a text file with site names, where the user should be removed
  2. find sites by site names (names from step 1). if no site or more that one site was found, write an exception message in the log
  3. remove the user from each site if follow statements are true:
  • the user is a site member
  • the user is a SiteManager
  • the user is not a member of a any group added to the site
  • the user is not only one SiteManager in the site

 

var user = "andrej";
//this document is a text file, that contains site names
var nodeRef = "workspace://SpacesStore/b370f056-63d7-4431-8279-3f0870fe5f56";


var siteNames = readSiteNames(nodeRef);

for each(var siteName in siteNames) {
  findSites(siteName);
}


/**

findSites by name and remove membership of the user.

**/
function findSites(siteName){
    var sites = siteService.getSites(siteName,null,0);
    
    if (sites.length > 1) {
        logger.warn("WARN: " + "More than one site found by name:" + siteName);
        for each(var site in sites) {
          logger.warn("\t\t" + site.shortName + "\n");
        }
    } else if (sites.length < 1) {
        logger.warn("WARN: " + "Site not found by name:" + siteName);
    } else {
        removeMembership(user,sites[0]);
    }
}

/**

check:
1. if the user is a site member
2. if the user is a SiteManager
3. if the user is not a mamber of a any group added to the site
4. if the user is not ony one SiteManager in the site

**/
function removeMembership(authorityName,site) {
    if(site){
      logger.log(site.shortName);
      
      if (site.isMember(authorityName)) {
          if (site.getMembersRole(authorityName)=="SiteManager") {
              
            if (!site.isMemberOfGroup(authorityName)) {
                
                  if (site.listMembers(null, "SiteManager", 0, true).length < 1) {
                      logger.warn("ERROR: no more SiteManager for site: \t\t" + site.shortName);
                  } else {
                      site.removeMembership(authorityName);
                      logger.log("membership deleted:" + site.shortName);
                  }
              } else {
                  logger.warn("WARN: user is not a SiteManager for site: \t\t" + site.shortName);
              }
          } else {
            logger.warn("ERROR: user is a member of the group for site: \t\t" + site.shortName);
          }
      } else {
          logger.warn("WARN: user is not a member of the site: \t\t" + site.shortName);
      }
    }
    logger.log("\n");
}


/**

read site names from a file.

**/
function readSiteNames(nodeRef) {
    var csvStr = "";
    var foundNode = search.findNode(nodeRef);
 
  csvStr = csvStr + foundNode.content;
  //logger.log(csvStr);
 
  var ids = new Array();
  var lines = csvStr.split("\n");
    return lines;
}