Class: G5::UserExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/devise_g5_authenticatable/g5/user_exporter.rb

Overview

Exports all users to the G5 auth server. Assumes presence of User model with uid and provider attributes.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UserExporter

Returns a new instance of UserExporter.

Parameters:

  • options (Hash) (defaults to: {})

    the options to export users with.

Options Hash (options):

  • :client_id (String)

    the G5 OAuth client ID

  • :client_secret (String)

    the G5 OAuth client secret

  • :redirect_uri (String)

    the redirect URI registered with G5

  • :endpoint (String)

    the endpoint for the G5 Auth server

  • :authorization_code (String)

    the G5 authorization code to obtain an access token



16
17
18
19
20
21
22
# File 'lib/devise_g5_authenticatable/g5/user_exporter.rb', line 16

def initialize(options = {})
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @redirect_uri = options[:redirect_uri]
  @endpoint = options[:endpoint]
  @authorization_code = options[:authorization_code]
end

Instance Method Details

#exportString

Export local users to the G5 Auth server. A record will be created in G5 Auth and associated with each local User. Password data is not automatically exported, but is returned in a dump of SQL update statements suitable for executing on the G5 Auth server.

Returns:

  • (String)

    SQL dump containing encrypted user passwords



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/devise_g5_authenticatable/g5/user_exporter.rb', line 31

def export
  update_statements = User.all.collect do |user|
    # The user won't actually be able to log in with their usual password,
    # but at least it won't be set to a guessable value
    auth_user = auth_client.create_user(email: user.email,
                                        password: user.encrypted_password)
    update_local_user(user, auth_user)
    update_sql(auth_user.id, user.encrypted_password)
  end

  update_statements.join("\n")
end