Module: GoogleDirectory::UserCommands

Included in:
Connection
Defined in:
lib/google_directory/user_commands.rb

Overview

Instance Method Summary collapse

Instance Method Details

#user_change_password(attributes:) ⇒ Hash

Note:

updates an exising Google Directory User password - convience method instead of using :user_update

Returns formatted as {command: :user_change_password, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]”, password: “secret” } - if no password is included a random password will be assigned

Returns:

  • (Hash)

    formatted as {command: :user_change_password, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }



66
67
68
69
70
71
72
73
# File 'lib/google_directory/user_commands.rb', line 66

def user_change_password( attributes: )
  password = SecureRandom.base64
  defaults  = { password: password, change_password_at_next_login: true }
  user_attr = defaults.merge( attributes )

  response = update_user( user_attr )
  {response: response, attributes: attributes[:primary_email], command: :user_change_password}
end

#user_create(attributes:) ⇒ Hash

Note:

creates a new Google Directory User

Returns formatted as {command: :user_create, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]”, name: {given_name: “First Names”, family_name: “LAST NAMES” } }

Returns:

  • (Hash)

    formatted as {command: :user_create, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/google_directory/user_commands.rb', line 40

def user_create( attributes: )
  # http://blog.liveedu.tv/ruby-generate-random-string/
  password = SecureRandom.base64
  defaults  = { suspended: true, password: password, change_password_at_next_login: true }
  user_attr = defaults.merge( attributes )
  # create a google user object
  user_object = Google::Apis::AdminDirectoryV1::User.new user_attr
  # create user in directory services
  response = service.insert_user( user_object )
  {response: response, attributes: attributes[:primary_email], command: :user_create}
end

#user_delete(attributes:) ⇒ Hash

Note:

deletes an exising Google Directory User

Returns formatted as {command: :user_delete, attributes: {primary_email: “user@domain”, response: “” } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]” }

Returns:

  • (Hash)

    formatted as {command: :user_delete, attributes: {primary_email: “user@domain”, response: “” } }



103
104
105
106
# File 'lib/google_directory/user_commands.rb', line 103

def user_delete( attributes: )
  response = service.delete_user( attributes[:primary_email] )
  {response: response, attributes: attributes[:primary_email], command: :user_delete}
end

#user_exists?(attributes:) ⇒ Hash

Note:

Test if user exists in Google Directory

Returns formatted as {command: :user_exists?, attributes: {primary_email: “user@domain”, response: Boolean } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]” }

Returns:

  • (Hash)

    formatted as {command: :user_exists?, attributes: {primary_email: “user@domain”, response: Boolean } }



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/google_directory/user_commands.rb', line 23

def user_exists?( attributes: )
  begin
    response = service.get_user( attributes[:primary_email] )
    return {response: true, attributes: attributes[:primary_email], command: :user_exists?}
  rescue Google::Apis::ClientError => error
    if error.message.include? 'notFound'
      return {response: false, attributes: attributes[:primary_email], command: :user_exists?}
    else
      raise error
    end
  end
end

#user_get(attributes:) ⇒ Hash

Note:

Get GoogleDirectory User Info

Returns formatted as {command: :user_get, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]” }

Returns:

  • (Hash)

    formatted as {command: :user_get, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }



14
15
16
17
# File 'lib/google_directory/user_commands.rb', line 14

def user_get( attributes: )
  response = service.get_user( attributes[:primary_email] )
  {response: response, attributes: attributes[:primary_email], command: :user_get}
end

#user_reactivate(attributes:) ⇒ Hash

Note:

activates an exising Google Directory User password - convience method instead of using :user_update

Returns formatted as {command: :user_reactivate, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]” }

Returns:

  • (Hash)

    formatted as {command: :user_reactivate, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }



79
80
81
82
83
84
85
# File 'lib/google_directory/user_commands.rb', line 79

def user_reactivate( attributes: )
  defaults  = { :suspended => false }
  user_attr = defaults.merge( attributes )

  response = update_user( user_attr )
  {response: response, attributes: attributes[:primary_email], command: :user_reactivate}
end

#user_suspend(attributes:) ⇒ Hash

Note:

suspends an exising Google Directory User password - convience method instead of using :user_update

Returns formatted as {command: :user_suspend, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]” }

Returns:

  • (Hash)

    formatted as {command: :user_suspend, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }



91
92
93
94
95
96
97
# File 'lib/google_directory/user_commands.rb', line 91

def user_suspend( attributes: )
  defaults  = { :suspended => true }
  user_attr = defaults.merge( attributes )

  response = update_user( user_attr )
  {response: response, attributes: attributes[:primary_email], command: :user_suspend}
end

#user_update(attributes:) ⇒ Hash

Note:

updates an exising Google Directory User

Returns formatted as {command: :user_update, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }.

Parameters:

  • attributes (Hash)

    this attribute MUST include: { primary_email: “[email protected]”, attributes_to_change: “” } }

Returns:

  • (Hash)

    formatted as {command: :user_update, attributes: {primary_email: “user@domain”, response: GoogleUserObject } }



56
57
58
59
60
# File 'lib/google_directory/user_commands.rb', line 56

def user_update( attributes: )
  # create a user object for google to update
  response = update_user( attributes )
  {response: response, attributes: attributes[:primary_email], command: :user_update}
end