Sha256: 53e6b901a8a2cecbdb1a60902a7839938c41b34fe6407b0b4d973e63f95feeae

Contents?: true

Size: 1.66 KB

Versions: 12

Compression:

Stored size: 1.66 KB

Contents

description 'YAML based user storage'
require 'yaml/store'

class YamlfileService < User::Service
  def initialize(config)
    FileUtils.mkpath(File.dirname(config[:store]))
    @store = ::YAML::Store.new(config[:store])
  end

  # @override
  def find(name)
    @store.transaction(true) do |store|
      user = store[name]
      user && User.new(name, user['email'], user['groups'])
    end
  end

  # @override
  def authenticate(name, password)
    @store.transaction(true) do |store|
      user = store[name]
      raise AuthenticationError, :wrong_user_or_pw.t if !user || user['password'] != crypt(password)
      User.new(name, user['email'], user['groups'])
    end
  end

  # @override
  def signup(user, password)
    @store.transaction do |store|
      raise :user_already_exists.t(name: user.name) if store[user.name]
      store[user.name] = {
        'email' => user.email,
        'password' => crypt(password),
	'groups' => user.groups.to_a
      }
    end
  end

  # @override
  def update(user)
    @store.transaction do |store|
      raise NameError, "User #{user.name} not found" if !store[user.name]
      store[user.name]['email'] = user.email
      store[user.name]['groups'] = user.groups.to_a
    end
  end

  # @override
  def change_password(user, oldpassword, password)
    @store.transaction do |store|
      check do |errors|
        errors << 'User not found' if !store[user.name]
        errors << :wrong_password.t if crypt(oldpassword) != store[user.name]['password']
      end
      store[user.name]['password'] = crypt(password)
    end
  end

  private

  def crypt(s)
    s.blank? ? s : sha256(s)
  end
end

User::Service.register :yamlfile, YamlfileService

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
olelo-0.9.15 plugins/authentication/yamlfile.rb
olelo-0.9.14 plugins/authentication/yamlfile.rb
olelo-0.9.13 plugins/authentication/yamlfile.rb
olelo-0.9.12 plugins/authentication/yamlfile.rb
olelo-0.9.11 plugins/authentication/yamlfile.rb
olelo-0.9.10 plugins/authentication/yamlfile.rb
olelo-0.9.9 plugins/authentication/yamlfile.rb
olelo-0.9.8 plugins/authentication/yamlfile.rb
olelo-0.9.7 plugins/authentication/yamlfile.rb
olelo-0.9.6 plugins/authentication/yamlfile.rb
olelo-0.9.5 plugins/authentication/yamlfile.rb
olelo-0.9.4 plugins/authentication/yamlfile.rb