Sha256: 92f880d5c4504af7ffe66f9a3dcd3c3b5d537ba5b28edfcae6b993b0858acd75

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

##
# As Authorized 是被授权的参与者或访问者的领域逻辑关注点。

module Unidom::Authorization::Concerns::AsAuthorized

  extend ActiveSupport::Concern

  included do |includer|

    has_many :authorizings, class_name: 'Unidom::Authorization::Authorizing', as:     :authorized
    has_many :permissions,  through:    :authorizings,                        source: :permission

    ##
    # 将本参与者或访问者,授予指定的权限 permission , by 是授权者, at 是授权时间,缺省为当前时间。如:
    # 假设 selected_person 对应的类已经 include Unidom::Authorization::Concerns::AsAuthorized 。
    # selected_person.is_authorized! permission, by: current_person
    # # or
    # selected_person.is_authorized! permission, by: current_person, at: Time.now
    def is_authorized!(permission: nil, by: nil, at: Time.now)

      assert_present! :permission, permission
      assert_present! :by,         by
      assert_present! :at,         at

      authorizings.create! permission: permission, authorizer: by, opened_at: at

    end

    ##
    # 判断本参与者或访问者,在给定的时间 at 是否拥有给定的权限 permission 。如:
    # 假设 selected_person 对应的类已经 include Unidom::Authorization::Concerns::AsAuthorized 。
    # selected_person.is_authorized? permission
    # # or
    # selected_person.is_authorized? permission, at: Time.now
    def is_authorized?(permission: nil, at: Time.now)

      assert_present! :permission, permission
      assert_present! :at,         at

      authorizings.permission_is(permission).valid_at(now: at).alive.exists?

    end

    def is_prohibited!(permission: nil, at: Time.now)

      assert_present! :permission, permission
      assert_present! :at,         at

      authorizings.permission_is(permission).valid_at(now: at).alive.update_all closed_at: at, defunct: true

    end

  end

  module ClassMethods
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
unidom-authorization-2.0 app/models/unidom/authorization/concerns/as_authorized.rb
unidom-authorization-1.6.10 app/models/unidom/authorization/concerns/as_authorized.rb
unidom-authorization-1.6.9 app/models/unidom/authorization/concerns/as_authorized.rb