Sha256: 041c8743e537e82708daa6a45eb28352e5d2f1de8c19ecf2ae0100fd7c3faabb

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Decidim
  # This class encapsulates an action, which will be used by the
  # permissions system to check if the user is allowed to perform it.
  #
  # It consists of a `scope` (which will typically be either `:public` or
  # `:admin`), the name of the `:action` that is being performed and the
  # `:subject` of the action.
  class PermissionAction
    # action - a Symbol representing the action being performed
    # scope - a Symbol representing the scope of the action
    # subject - a Symbol representing the subject of the action
    def initialize(action:, scope:, subject:)
      @action = action
      @scope = scope
      @subject = subject
      @state = nil
    end

    attr_reader :action, :scope, :subject

    def allow!
      raise PermissionCannotBeDisallowedError, "Allowing a previously disallowed action is not permitted: #{inspect}" if @state == :disallowed
      @state = :allowed
    end

    def disallow!
      @state = :disallowed
    end

    def allowed?
      raise PermissionNotSetError, "Permission hasn't been allowed or disallowed yet: #{inspect}" if @state.blank?
      @state == :allowed
    end

    class PermissionNotSetError < StandardError; end
    class PermissionCannotBeDisallowedError < StandardError; end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-core-0.12.0 app/models/decidim/permission_action.rb
decidim-core-0.12.0.pre app/models/decidim/permission_action.rb