class Softwear::Auth::StubbedModel

In development, unless ENV is set, Softwear::Auth::Model is set to this StubbedModel. The StubbedModel does not contact the auth_server, and instead retrieves user info from config/users.yml, which assumes the following format:

Public Class Methods

all() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_yml() click to toggle source
# 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_file() click to toggle source
# 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) click to toggle source

Transforms

'email@email.com', { 'attr1' => 'val1', 'attr2' => 'val2' }

From the yml format

Into { 'email' => 'email@email.com', 'attr1' => 'val1', 'attr2' => 'val2' }

# 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 = ['usernotfound@example.com', { 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

Public Instance Methods

reload() click to toggle source
# 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) click to toggle source
# File lib/softwear/auth/stubbed_model.rb, line 116
def users_yml(*args)
  self.class.users_yml(*args)
end
valid_password?() click to toggle source
# File lib/softwear/auth/stubbed_model.rb, line 125
def valid_password?
  true
end
yml_entry(*args) click to toggle source
# File lib/softwear/auth/stubbed_model.rb, line 113
def yml_entry(*args)
  self.class.yml_entry(*args)
end