cirandas.net

ref: master

vendor/plugins/access_control/lib/role_assignment.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class RoleAssignment < ActiveRecord::Base

  attr_accessible :accessor_id, :accessor_type, :role_id, :resource_id, :resource_type, :created_at

  belongs_to :role
  belongs_to :accessor, :polymorphic => true
  belongs_to :resource, :polymorphic => true

  validates_presence_of :role, :accessor

  track_actions :join_community, :after_create, :keep_params => ["resource.name", "resource.url", "resource.profile_custom_icon"],
    if: -> x { x.resource.is_a?(Community) && x.accessor.role_assignments.where(resource_id: x.resource.id, resource_type: 'Profile').count == 1 },
    :custom_user => :accessor, :custom_target => :resource

  track_actions :add_member_in_community, :after_create,
    if: -> x { x.resource.is_a?(Community) && x.accessor.role_assignments.where(resource_id: x.resource.id, resource_type: 'Profile').count == 1 },
    :custom_user => :accessor, :custom_target => :resource

  def has_permission?(perm, res)
    return false unless role.has_permission?(perm.to_s) && (resource || is_global)
    return true if is_global
    return false if res == 'global'
    while res
      return true if (resource == res)
      res = res.superior_instance
    end
    return (resource == res)
  end
end