Skip to content

abstract class Athena::Security::Authorization::Voter
inherits Reference #

The base type of voters, which decide whether a token is granted an attribute, optionally on a subject.

The ASEC::Authorization::AccessDecisionManager asks each voter for its ASEC::Authorization::Voter::Access, then turns their votes into a decision via its ASEC::Authorization::Strategy::Interface.

A voter includes either:

The attributes of a subject voter are usually a fixed set, listed by an enum:

class PostVoter < ASEC::Authorization::Voter
  enum Attribute
    View
    Edit
  end

  include ASEC::Authorization::Voter::Subject(Post, Attribute)

  protected def supports?(attribute : Attribute, subject : Post) : Bool
    true
  end

  protected def vote_on_attribute(attribute : Attribute, subject : Post, token : ASEC::Authentication::Token::Interface, vote : ASEC::Authorization::Voter::Vote?) : Bool
    case attribute
    in .view? then subject.published? || subject.author == token.user_identifier
    in .edit? then subject.author == token.user_identifier
    end
  end
end

checker.granted? PostVoter::Attribute::Edit, post

A voter whose attributes are free-form strings uses String instead, e.g. ASEC::Authorization::Voter::Subject(Post, String).

A voter is only asked to vote on subjects whose type it handles, which is known at compile time. A voter may handle multiple types by including ASEC::Authorization::Voter::Subject(T, A) once per type, with overloads of its methods for each of them. A module may also be used as the type, which includes each of the types including it.

Direct known subclasses

Athena::Security::Authorization::Voter::Authenticated Athena::Security::Authorization::Voter::Role

Constants#

ATTRIBUTE_VOTERS = [Athena::Security::Authorization::Voter::Authenticated, Athena::Security::Authorization::Voter::Role, Athena::Security::Authorization::Voter::RoleHierarchy] of Nil#

The voter types voting on attributes alone, built once all types are known.

:nodoc:

SUBJECT_VOTERS = {} of Nil => Nil#

Maps the name of a subject type to the voter types voting on it, built once all types are known.

:nodoc:

Class methods#

.attribute_name(attribute : Enum) : String#

Returns the string the provided attribute is decided as, e.g. "PostVoter::Attribute::Edit".

Voters including ASEC::Authorization::Voter::Subject(T, A) with an enum as A vote on these strings.

View source

Methods#

#supports_attribute?(attribute : String) : Bool#

Returns false if this voter never votes on the provided attribute, otherwise true.

The ASEC::Authorization::AccessDecisionManager caches the result, and no longer asks this voter to vote on the attribute when it is false. Defaults to only supporting the members of the attribute enums of a voter whose ASEC::Authorization::Voter::Subject(T, A) all have one.

View source