Sha256: c088c54b47f1499dbb231a9be8728a62338ac5404508a0633fc5704801be8bee

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

module Rasti
  class App
    class Policy

      class UnauthorizedError < StandardError

        attr_reader :user, :permission

        def initialize(user, permission)
          @user = user
          @permission = permission
        end

        def message
          "Permission denied [#{user} -> #{permission}]"
        end
        
      end
      
      class << self

        def authorizations
          @authorizations ||= {}
        end

        private

        def authorization(permission, &block)
          authorizations[permission] = block
        end

        def ignore(permission)
          authorization(permission) { true }
        end

      end

      def initialize(container, context)
        @container = container
        @context = context
      end

      def authorized?(permission, form)
        if self.class.authorizations.key? permission
          instance_exec form, &self.class.authorizations[permission]
        else
          user.authorized? permission
        end
      end

      def authorize!(permission, form)
        raise UnauthorizedError.new(user.name, permission) unless authorized? permission, form
      end

      private 

      attr_reader :container, :context

      def user
        context.fetch(:user)
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rasti-app-0.1.2 lib/rasti/app/policy.rb
rasti-app-0.1.1 lib/rasti/app/policy.rb
rasti-app-0.1.0 lib/rasti/app/policy.rb
rasti-app-0.0.10 lib/rasti/app/policy.rb
rasti-app-0.0.9 lib/rasti/app/policy.rb
rasti-app-0.0.8 lib/rasti/app/policy.rb