Sha256: 73b5ead57b54ae1ea02d41a5a53219ef149c341f441ff779960acce8bdf379f5

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Rasti
  class App
    class Policy

      class UnauthorizedError < StandardError
        def initialize(user, permission)
          super "Access 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, user)
        @container = container
        @user = user
      end

      def authorized?(permission, params)
        if self.class.authorizations.key? permission
          self.class.authorizations[permission].call params
        else
          user.authorized? permission
        end
      end

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

      private 

      attr_reader :container, :user

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rasti-app-0.0.1 lib/rasti/app/policy.rb