Class: Softwear::Auth::StubbedModel

Inherits:
StandardModel show all
Defined in:
lib/softwear/auth/stubbed_model.rb

Overview

Changes in this .yml file will be reflected live, without restarting your server.

Constant Summary

Constants inherited from StandardModel

Softwear::Auth::StandardModel::REMOTE_ATTRIBUTES

Instance Attribute Summary

Attributes inherited from StandardModel

#persisted

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StandardModel

abstract_class?, arel_table, auth_server_down?, auth_server_down_mailer, auth_server_host, auth_server_port, base_class, default_socket, expire_query_cache, filter_all, find_by, force_query, #force_query, #full_name, has_many, #initialize, logger, #logger, new, pluck, primary_key, query, #query, #raw_query, record, relation_delegate_class, #role?, #to_json, unscoped, #update_attributes, validate_response, where

Constructor Details

This class inherits a constructor from Softwear::Auth::StandardModel

Class Method Details

.allObject



97
98
99
# File 'lib/softwear/auth/stubbed_model.rb', line 97

def all
  users_yml[:users].to_a.map(&method(:yml_entry))
end

.auth(_token, _appname = nil) ⇒ Object



106
107
108
109
110
# File 'lib/softwear/auth/stubbed_model.rb', line 106

def auth(_token, _appname = nil)
  signed_in = users_yml[:signed_in]

  yml_entry [signed_in, users_yml[:users][signed_in]] unless signed_in.blank?
end

.find(target_id) ⇒ Object



92
93
94
95
# File 'lib/softwear/auth/stubbed_model.rb', line 92

def find(target_id)
  return nil if target_id.nil?
  yml_entry users_yml[:users].to_a[target_id.to_i - 1], target_id.to_i
end

.of_role(*roles) ⇒ Object



101
102
103
104
# File 'lib/softwear/auth/stubbed_model.rb', line 101

def of_role(*roles)
  roles = Array(roles).map(&:to_s)
  all.select { |u| !u.roles.nil? && roles.any? { |r| u.roles.include?(r) } }
end

.raw_query(m, *args) ⇒ Object



38
39
40
41
# File 'lib/softwear/auth/stubbed_model.rb', line 38

def raw_query(m, *args)
  return 'yes' if m =~ /^token/
  raise %[Cannot perform auth server queries on stubbed auth model. (tried to send "#{m.split(/\s+/).first} ..." query)]
end

.users_ymlObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/softwear/auth/stubbed_model.rb', line 47

def users_yml
  if @users_yml
    yml_mtime = File.mtime(users_yml_file)

    if @users_yml_modified.nil? || yml_mtime > @users_yml_modified
      @users_yml_modified = yml_mtime
      @users_yml = nil
    end
  else
    @users_yml_modified = File.mtime(users_yml_file)
  end

  if @users_yml.nil?
    @users_yml = YAML.load(IO.read(users_yml_file)).with_indifferent_access
    @users_yml[:users].to_a.each_with_index do |entry, i|
      entry[1][:id] ||= i + 1
    end
  end
  @users_yml
end

.users_yml_fileObject



43
44
45
# File 'lib/softwear/auth/stubbed_model.rb', line 43

def users_yml_file
  Rails.root.join('config', 'users.yml').to_s
end

.yml_entry(entry, id_if_default = nil) ⇒ Object

Transforms

[email protected]’, { ‘attr1’ => ‘val1’, ‘attr2’ => ‘val2’ }

From the yml format

Into { ‘email’ => ‘[email protected]’, ‘attr1’ => ‘val1’, ‘attr2’ => ‘val2’ }



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/softwear/auth/stubbed_model.rb', line 76

def yml_entry(entry, id_if_default = nil)
  attributes = {}.with_indifferent_access

  if entry.nil?
    entry = ['[email protected]', { first_name: 'Unknown', last_name: 'User', id: id_if_default || -1 }]
  end

  attributes[:email] = entry[0]
  attributes.merge!(entry[1])
  if attributes[:profile_picture]
    attributes[:profile_picture_url] ||= "file://#{attributes[:profile_picture]}"
  end

  new(attributes).tap { |u| u.instance_variable_set(:@persisted, true) }
end

Instance Method Details

#reloadObject



120
121
122
123
# File 'lib/softwear/auth/stubbed_model.rb', line 120

def reload
  update_attributes yml_entry users_yml[:users].to_a[id - 1]
  self
end

#users_yml(*args) ⇒ Object



116
117
118
# File 'lib/softwear/auth/stubbed_model.rb', line 116

def users_yml(*args)
  self.class.users_yml(*args)
end

#valid_password?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/softwear/auth/stubbed_model.rb', line 125

def valid_password?
  true
end

#yml_entry(*args) ⇒ Object



113
114
115
# File 'lib/softwear/auth/stubbed_model.rb', line 113

def yml_entry(*args)
  self.class.yml_entry(*args)
end