module Softwear module Auth # ======================================================================= # In development, unless ENV['AUTH_SERVER'] 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: =begin --- signed_in: nigel@annarbortees.com users: admin@softwearcrm.com: first_name: Admin last_name: User roles: - sales_manager nigel@annarbortees.com: first_name: Nigel last_name: Baillie roles: - developer - admin ricky@annarbortees.com: first_name: Ricky last_name: Winowiecki roles: - developer - administrator =end # Changes in this .yml file will be reflected live, without restarting your # server. # ======================================================================= class StubbedModel < Softwear::Auth::StandardModel class << self 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 def users_yml_file Rails.root.join('config', 'users.yml').to_s end 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 # # Transforms # ['email@email.com', { 'attr1' => 'val1', 'attr2' => 'val2' }] # From the yml format # # Into # { 'email' => 'email@email.com', 'attr1' => 'val1', 'attr2' => 'val2' } # 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 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 def all users_yml[:users].to_a.map(&method(:yml_entry)) end def of_role(*roles) roles = Array(roles).map(&:to_s) all.select { |u| !u.roles.nil? && roles.any? { |r| u.roles.include?(r) } } end def of_group(group_code) roles = Array(roles).map(&:to_s) all.select { |u| !u.groups.nil? && u.groups.include?(group_code) } end 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 end def yml_entry(*args) self.class.yml_entry(*args) end def users_yml(*args) self.class.users_yml(*args) end def reload update_attributes yml_entry users_yml[:users].to_a[id - 1] self end def valid_password? true end end end end