def AuthorizeClient(self):
   """Authorize the clients for making API requests."""
   self.token = gdata.gauth.OAuth2Token(
       client_id=self.client_id, client_secret=self.client_secret,
       scope=SCOPES, user_agent=USER_AGENT)
   uri = self.token.generate_authorize_url()
   print 'Please visit this URL to authorize the application:'
   print uri
   # Get the verification code from the standard input.
   code = raw_input('What is the verification code? ').strip()
   self.token.get_access_token(code)
   self.user_client = AppsClient(domain=self.domain, auth_token=self.token)
   self.org_client = OrganizationUnitProvisioningClient(
       domain=self.domain, auth_token=self.token)
Пример #2
0
  def post(self):
    """Handels the get request for the MainHandler.

    Retrieves a list of all of the domain's users and sends it
    to the Client as a JSON object.
    """
    users_list = []
    session = Session()
    domain = session['domain']
    client = AppsClient(domain=domain)
    client.auth_token = self.Get2loToken()
    client.ssl = True
    feed = client.RetrieveAllUsers()
    for entry in feed.entry:
      users_list.append(entry.login.user_name)
    self.response.out.write(json.dumps(users_list))
Пример #3
0
  def GetNicknames(self, domain, username):
    """Retrieves the list of all the nicknames for the user.

    Args:
      domain: A string determining the user's domain.
      username: A string denoting the user's username.

    Returns:
      A list of user's nicknames if successful.
      Otherwise a list with a single entry containing error message.
    """
    try:
      client = AppsClient(domain=domain)
      client.auth_token = self.Get2loToken()
      client.ssl = True
      feed = client.RetrieveNicknames(username)
      nicknames = []
      for entry in feed.entry:
        nicknames.append(entry.nickname.name)
      return nicknames
    except:
      return ['An error occured while retriving Nicknames for the user.']
class SearchAndOrganizeUsers(object):
  """Search users with a pattern and move them to organization."""

  def __init__(self, client_id, client_secret, domain):
    """Create a new SearchAndOrganizeUsers object configured for a domain.
    
    Args:
      client_id: [string] The clientId of the developer.
      client_secret: [string] The clientSecret of the developer.
      domain: [string] The domain on which the functions are to be performed.
    """
    self.client_id = client_id
    self.client_secret = client_secret
    self.domain = domain

  def AuthorizeClient(self):
    """Authorize the clients for making API requests."""
    self.token = gdata.gauth.OAuth2Token(
        client_id=self.client_id, client_secret=self.client_secret,
        scope=SCOPES, user_agent=USER_AGENT)
    uri = self.token.generate_authorize_url()
    print 'Please visit this URL to authorize the application:'
    print uri
    # Get the verification code from the standard input.
    code = raw_input('What is the verification code? ').strip()
    self.token.get_access_token(code)
    self.user_client = AppsClient(domain=self.domain, auth_token=self.token)
    self.org_client = OrganizationUnitProvisioningClient(
        domain=self.domain, auth_token=self.token)

  def OrganizeUsers(self, customer_id, org_unit_path, pattern):
    """Find users with given pattern and move to an organization in batches.

    Args:
      customer_id: [string] customer_id to make calls to Organization API.
      org_unit_path: [string] path of organization unit where users are moved
      pattern: [regex object] regex to match with users
    """
    users = self.user_client.RetrieveAllUsers()
    matched_users = []
    # Search the users that match given pattern
    for user in users.entry:
      if (pattern.search(user.login.user_name) or
          pattern.search(user.name.given_name) or
          pattern.search(user.name.family_name)):
        user_email = '%s@%s' % (user.login.user_name, self.domain)
        matched_users.append(user_email)
    # Maximum BATCH_SIZE users can be moved at one time
    # Split users into batches of BATCH_SIZE and move in batches
    for i in xrange(0, len(matched_users), BATCH_SIZE):
      batch_to_move = matched_users[i: i + BATCH_SIZE]
      self.org_client.MoveUserToOrgUnit(customer_id,
          org_unit_path, batch_to_move)
    print 'Number of users moved = %d' % len(matched_users)

  def Run(self, org_unit_path, regex):
    self.AuthorizeClient()
    customer_id_entry = self.org_client.RetrieveCustomerId()
    customer_id = customer_id_entry.customer_id
    pattern = re.compile(regex)
    print 'Moving Users with the pattern %s' % regex
    self.OrganizeUsers(customer_id, org_unit_path, pattern)