Class: GoogleDirectory::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/google-directory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope = :main) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/google-directory.rb', line 11

def initialize(scope = :main)
	@config = GoogleDirectory.configuration
	@config = @config.using(scope) if @config.scope_name != scope

	
	@key = Google::APIClient::KeyUtils.load_from_pkcs12(@config.key_file, @config.key_passphrase)
	@client = Google::APIClient.new(:application_name => @config.application_name, :application_version => @config.application_version )

	@client.authorization = Signet::OAuth2::Client.new(
		:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
		:audience => 'https://accounts.google.com/o/oauth2/token',
		:scope => 'https://www.googleapis.com/auth/admin.directory.user',
		:issuer => @config.issuer, 
		:person => @config.admin_email,
		:signing_key => @key
	)

	if token = @config.load_token

		token['issued_at'] = Time.parse( token['issued_at'] )
		@client.authorization.update_token!(token)

	else
		token = @client.authorization.fetch_access_token!
		token['issued_at'] = @client.authorization.issued_at.to_s
		@config.save_token(token)

	end
	
	@directory_api = @client.discovered_api('admin', 'directory_v1')
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/google-directory.rb', line 9

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/google-directory.rb', line 9

def config
  @config
end

#directory_apiObject (readonly)

Returns the value of attribute directory_api.



9
10
11
# File 'lib/google-directory.rb', line 9

def directory_api
  @directory_api
end

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/google-directory.rb', line 9

def result
  @result
end

Instance Method Details

#create_user(email, given_name, family_name, password, opts = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/google-directory.rb', line 55

def create_user(email, given_name, family_name, password, opts = {})
	opts = {
		'name'                       => { 'givenName' => given_name, 'familyName' => family_name},
		'password'                   => password,
		'primaryEmail'               => email,
		'changePasswordAtNextLogin'  => true
	}.merge(opts)

	execute(:api_method => directory_api.users.insert, :body_object => opts)
end

#delete_user(email) ⇒ Object



66
67
68
# File 'lib/google-directory.rb', line 66

def delete_user(email)
	execute(:api_method => directory_api.users.delete, :parameters => {'userKey' => email})
end

#find_user_by_email(email) ⇒ Object



48
49
50
# File 'lib/google-directory.rb', line 48

def find_user_by_email(email)
	execute(:api_method => directory_api.users.get, :parameters => { 'userKey' => email })
end

#find_users(params) ⇒ Object



44
45
46
# File 'lib/google-directory.rb', line 44

def find_users(params)
	execute(:api_method => directory_api.users.list, :parameters => params)
end

#update_user(email, update_data = {}) ⇒ Object



70
71
72
# File 'lib/google-directory.rb', line 70

def update_user(email, update_data = {})
	execute(:api_method => directory_api.users.update, :parameters => {'userKey' => email}, :body_object => update_data)
end

#update_user_password(email, password) ⇒ Object



74
75
76
# File 'lib/google-directory.rb', line 74

def update_user_password(email, password)
	update_user(email, {'password' => password, 'changePasswordAtNextLogin' => false})
end