Sha256: 271e552a8c56017f30a484f8eaf1a14d69a41732bdec2ca64ac374b76dc4f043

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Kadmin
  module Auth
    class UserStore
      def initialize(path = nil)
        @store = {}

        path ||= Rails.root.join('config', 'admin_users.yml')
        if File.exist?(path) && File.readable?(path)
          definitions = YAML.load_file(path.to_s)
          create_organizations(definitions['organizations'])
          load_users(definitions['users'])
        else
          Rails.logger.warn("Can't read admin users auth file at #{file}. Auth might not work")
        end
      end

      def get(email)
        return @store[email.to_s.downcase]
      end

      def set(email, user)
        @store[email.to_s.downcase] = user
      end

      def exists?(email)
        @store.key?(email.to_s.downcase)
      end

      private

      def load_users(users)
        users.each do |user|
          email = user['email']
          options = {
            admin: user.fetch('admin', false),
            accept: Array.wrap(user.fetch('accept', [])).map(&:to_sym),
            organization: user.fetch('organization', 'offerista') # default organization, needs to exist in DB
          }

          set(email, Kadmin::Auth.config.user_class.new(email, **options))
        end
      end

      def create_organizations(organizations)
        organizations.each do |organization|
          Kadmin::Organization.find_or_create_by(name: organization)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kadmin-1.0.4 lib/kadmin/auth/user_store.rb