将用户排除在G Suite的特定组之外

时间:2019-04-18 17:29:24

标签: google-apps-script google-admin-sdk gsuite

我正在尝试在Google Apps脚本中编写一个脚本,该脚本将暂停其帐户超过120天不活动的用户的帐户。但是我不希望该脚本删除特定组中用户的帐户。以下是我想到的脚本:

/**
 * Lists all the users in a domain sorted by first name.
 */
function listAllUsers() {

  var contents = [];
  var pageToken;
  var page;
  do {
    page = AdminDirectory.Users.list({
      customer: 'xyz12345',
      orderBy: 'givenName',
      maxResults: 100,
      pageToken: pageToken
    });
    var users = page.users;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        if(user.suspended==false){
          contents.push([user.name.fullName, user.primaryEmail, user.creationTime, user.lastLoginTime]);
        }
      }
    } else {
      Logger.log('No users found.');
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
  checkUserLoginTime(contents);
}


/**
 * Check the user Log in Time of the users.
 * @param contents (The array containing the users list)
 */
function checkUserLoginTime(contents) {
  //Logger.log(contents);
  var usersToSuspend = [];
  var timeNow = new Date();
  var checkTime = new Date(); 
  checkTime.setDate(checkTime.getDate()-120);
  checkTime = new Date(checkTime);
  Logger.log('TimeNow:'+ timeNow);
  Logger.log('checktime:'+ checkTime);

  for (var i=0; i<contents.length; i++){
    var fullName = contents[i][0];
    var email = contents[i][1];
    var formattedCreationTime = new Date(contents[i][2]);
    var formattedLastLoginTime = new Date(contents[i][3]);
    Logger.log(fullName);
    Logger.log(email);
    Logger.log(formattedCreationTime);
    Logger.log(formattedLastLoginTime);
    if(formattedCreationTime <= checkTime && formattedLastLoginTime <= checkTime){
      usersToSuspend.push([fullName, email]); 
      Logger.log('The user account is Inactive')
      //suspendUsers(email);
    } else{
      Logger.log('The user account is Active')
    }
  }

}

此脚本将把所有用户帐户推入数组usersToSuspend,该数组的上次登录时间大于120天。是否有办法过滤掉该数组或停止将属于特定组的电子邮件发送到该数组,例如:xyz@gmail.com

2 个答案:

答案 0 :(得分:0)

这是我用来从用户的电子邮件中测试组成员身份的功能。

function testGroupMembership(groupEmail, testUser) {
    var group = GroupsApp.getGroupByEmail(groupEmail);
    var users = group.getUsers();
    for (var u = 0; u < users.length; u++){
      if ( users[u].getEmail() === testUser){
        return true;
      }
  return false;
}

传递用户的电子邮件地址和组的电子邮件地址。如果用户是成员,则返回true。因此,您必须“不”或相反,才能暂停帐户。

答案 1 :(得分:0)

最终在不创建单独函数或复杂for loops的情况下对我有用的脚本如下:

/**
 * Lists all the users in a domain sorted by first name.
 */
function listAllUsers() {

  var contents = [];
  var pageToken;
  var page;
  do {
    page = AdminDirectory.Users.list({
      customer: 'xyz12345',
      orderBy: 'givenName',
      maxResults: 100,
      pageToken: pageToken
    });
    var users = page.users;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        if(user.suspended==false){
          contents.push([user.name.fullName, user.primaryEmail, user.creationTime, user.lastLoginTime]);
        }
      }
    } else {
      Logger.log('No users found.');
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
  checkUserLoginTime(contents);
}


/**
 * Check the user Log in Time of the users.
 * @param contents (The array containing the users list)
 */
function checkUserLoginTime(contents) {
  //Logger.log(contents);
  var usersToSuspend = [];
  var timeNow = new Date();
  var checkTime = new Date();
  var group = GroupsApp.getGroupByEmail("xyz@gmail.com");     // gets the group
  var users = group.getUsers();     // gets the users in the group
  checkTime.setDate(checkTime.getDate()-120);
  checkTime = new Date(checkTime);
  Logger.log('TimeNow:'+ timeNow);
  Logger.log('checktime:'+ checkTime);

  for (var i=0; i<contents.length; i++){
    var fullName = contents[i][0];
    var email = contents[i][1];
    var formattedCreationTime = new Date(contents[i][2]);
    var formattedLastLoginTime = new Date(contents[i][3]);
    Logger.log(fullName);
    Logger.log(email);
    Logger.log(formattedCreationTime);
    Logger.log(formattedLastLoginTime);
    if(formattedCreationTime <= checkTime && formattedLastLoginTime <= checkTime){
      if (group.hasUser(email)) {     //checks if the user is part of the group
        Logger.log(email + ' belongs to xyz@gmail.com group');
      } else{
       usersToSuspend.push([fullName, email]); 
       Logger.log('The user account is Inactive')
       //suspendUsers(email);
      }
    } else{
      Logger.log('The user account is Active')
    }
  }

}

group.hasUser(email)消除了运行forif循环的需要。