Class: Visor::Auth::Backends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/auth/backends/base.rb

Overview

This is the Base super class for all Backends. Each new backend inherits from Base, which contains the model and all validations for the users metadata.

Implementing a new backend is as simple as create a new backend class which inherits from Base and them implement the specific methods for querying the underlying database.

Direct Known Subclasses

MongoDB, MySQL

Constant Summary collapse

MANDATORY =

Keys validation

Mandatory attributes

[:access_key, :email]
READONLY =

Read-only attributes

[:_id, :secret_key, :created_at, :updated_at]
ALL =

All attributes

MANDATORY + READONLY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Base

Initializes a Backend instance.

Parameters:

  • [Hash] (Hash)

    a customizable set of options

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :host (String)

    The host address.

  • :port (Integer)

    The port to be used.

  • :db (String)

    The wanted database.

  • :user (String)

    The username to be authenticate db access.

  • :password (String)

    The password to be authenticate db access.

  • :conn (Object)

    The connection pool to access database.



37
38
39
40
41
42
43
44
# File 'lib/auth/backends/base.rb', line 37

def initialize(opts)
  @host     = opts[:host]
  @port     = opts[:port]
  @db       = opts[:db]
  @user     = opts[:user]
  @password = opts[:password]
  @conn     = opts[:conn]
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



24
25
26
# File 'lib/auth/backends/base.rb', line 24

def conn
  @conn
end

#dbObject (readonly)

Returns the value of attribute db.



24
25
26
# File 'lib/auth/backends/base.rb', line 24

def db
  @db
end

#hostObject (readonly)

Returns the value of attribute host.



24
25
26
# File 'lib/auth/backends/base.rb', line 24

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



24
25
26
# File 'lib/auth/backends/base.rb', line 24

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



24
25
26
# File 'lib/auth/backends/base.rb', line 24

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



24
25
26
# File 'lib/auth/backends/base.rb', line 24

def user
  @user
end

Instance Method Details

#set_protected_post(info) ⇒ Hash

Set protected fields value from a post operation. Being them the _id and created_at.

Parameters:

  • info (Hash)

    The user information.

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Hash)

    The updated user information.



80
81
82
# File 'lib/auth/backends/base.rb', line 80

def set_protected_post(info)
  info.merge!(_id: SecureRandom.uuid, secret_key: SecureRandom.base64(30), created_at: Time.now)
end

#set_protected_put(info) ⇒ Hash

Set protected field value from a get operation. Being it the updated_at.

Parameters:

  • info (Hash)

    The user information update.

Returns:

  • (Hash)

    The updated user information.



91
92
93
# File 'lib/auth/backends/base.rb', line 91

def set_protected_put(info)
  info.merge!(updated_at: Time.now)
end

#to_sql_insert(h) ⇒ String

Generates a compatible SQL INSERT string from a hash.

Parameters:

  • h (Hash)

    The input hash.

Returns:

  • (String)

    A string as “(k, k1) VALUES (‘v’, ‘v1’)”, only Strings Times or Hashes values are surrounded with ‘<value>’.



124
125
126
127
# File 'lib/auth/backends/base.rb', line 124

def to_sql_insert(h)
  surround = h.values.map { |v| string_time_or_hash?(v) ? "'#{v}'" : v }
  %W{(#{h.keys.join(', ')}) (#{surround.join(', ')})}
end

#to_sql_update(h) ⇒ String

Generates a compatible SQL UPDATE string from a hash.

Parameters:

  • h (Hash)

    The input hash.

Returns:

  • (String)

    A string as “k=‘v’, k1=‘v1’”, only Strings Times or Hashes values are surrounded with ‘<value>’.



113
114
115
# File 'lib/auth/backends/base.rb', line 113

def to_sql_update(h)
  h.map { |k, v| string_time_or_hash?(v) ? "#{k}='#{v}'" : "#{k}=#{v}" }.join(', ')
end

#to_sql_where(h) ⇒ String

Generates a compatible SQL WHERE string from a hash.

Parameters:

  • h (Hash)

    The input hash.

Returns:

  • (String)

    A string as “k=‘v’ AND k1=‘v1’”, only Strings Times or Hashes values are surrounded with ‘<value>’.



102
103
104
# File 'lib/auth/backends/base.rb', line 102

def to_sql_where(h)
  h.map { |k, v| string_time_or_hash?(v) ? "#{k}='#{v}'" : "#{k}=#{v}" }.join(' AND ')
end

#validate_data_post(info) ⇒ Object

Validates the user information for a post operation, based on possible keys and values.

@raise If some of the information fields do not respect the

possible values, contains any read-only or misses any mandatory field.

Parameters:

  • info (Hash)

    The user information.



53
54
55
56
57
# File 'lib/auth/backends/base.rb', line 53

def validate_data_post(info)
  info.assert_exclusion_keys(READONLY)
  info.assert_inclusion_keys(MANDATORY)
  validate_email(info[:email])
end

#validate_data_put(info) ⇒ Object

Validates the user information for a put operation, based on possible keys and values.

@raise If some of the metadata fields do not respect the

possible values, contains any read-only or misses any mandatory field.

Parameters:

  • info (Hash)

    The user information.



66
67
68
69
# File 'lib/auth/backends/base.rb', line 66

def validate_data_put(info)
  info.assert_exclusion_keys(READONLY)
  validate_email(info[:email]) if info[:email]
end

#validate_query_filters(filters) ⇒ Object

Validates that incoming query filters fields are valid.

@raise If some of the query filter fields do not respect the

possible values.

Parameters:

  • filters (Hash)

    The image metadata filters coming from a GET request.



136
137
138
139
# File 'lib/auth/backends/base.rb', line 136

def validate_query_filters(filters)
  filters.symbolize_keys!
  filters.assert_valid_keys(ALL)
end