Sha256: 29ec93cb50ed111516079da46ad26b392aa85cd43a73fc709ef35449e217f64b

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Ditty
  module Helpers
    module Authentication
      def current_user
        if env['rack.session'].nil? || env['rack.session']['user_id'].nil?
          self.current_user = anonymous_user
        end
        @users ||= Hash.new { |h, k| h[k] = User[k] }
        @users[env['rack.session']['user_id']]
      end

      def current_user=(user)
        env['rack.session'] = {} if env['rack.session'].nil?
        env['rack.session']['user_id'] = user.id if user
      end

      def authenticate
        authenticated?
      end

      def authenticated?
        current_user && !current_user.role?('anonymous')
      end

      def authenticate!
        raise NotAuthenticated unless authenticated?
        true
      end

      def logout
        env['rack.session'].delete('user_id')
      end

      def check_basic(request)
        auth = Rack::Auth::Basic::Request.new(request.env)
        return false unless auth.provided? && auth.basic?

        identity = ::Ditty::Identity.find(username: auth.credentials[0])
        identity ||= ::Ditty::Identity.find(username: CGI.unescape(auth.credentials[0]))
        return false unless identity
        self.current_user = identity.user if identity.authenticate(auth.credentials[1])
      end

      def anonymous_user
        return @anonymous_user if defined? @anonymous_user
        @anonymous_user ||= begin
          role = ::Ditty::Role.where(name: 'anonymous').first
          ::Ditty::User.where(roles: role).first unless role.nil?
        end
      end
    end

    class NotAuthenticated < StandardError
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ditty-0.3.1 lib/ditty/helpers/authentication.rb
ditty-0.3.0 lib/ditty/helpers/authentication.rb
ditty-0.2.2 lib/ditty/helpers/authentication.rb
ditty-0.2.1 lib/ditty/helpers/authentication.rb
ditty-0.2.0 lib/ditty/helpers/authentication.rb