Sha256: 9a2ccb3025b519e607a567a5125f5330689f5013dd3604b41d066c1c78487e0b

Contents?: true

Size: 1.23 KB

Versions: 28

Compression:

Stored size: 1.23 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(environment, session)
        @environment = environment
        @session = session
      end

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

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

      private 

      attr_reader :environment, :session

    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
rasti-app-2.2.2 lib/rasti/app/policy.rb
rasti-app-2.2.1 lib/rasti/app/policy.rb
rasti-app-2.2.0 lib/rasti/app/policy.rb
rasti-app-2.1.1 lib/rasti/app/policy.rb
rasti-app-2.1.0 lib/rasti/app/policy.rb
rasti-app-2.0.0 lib/rasti/app/policy.rb
rasti-app-1.1.0 lib/rasti/app/policy.rb
rasti-app-1.0.0 lib/rasti/app/policy.rb