cirandas.net

ref: master

vendor/plugins/access_control/lib/role.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Role < ActiveRecord::Base

  attr_accessible :key, :name, :environment, :permissions

  has_many :role_assignments, :dependent => :destroy
  belongs_to :environment
  belongs_to :organization
  serialize :permissions, Array
  validates_presence_of :name
  validates_uniqueness_of :name, :scope => :environment_id
  validates_uniqueness_of :key, :if => lambda { |role| !role.key.blank? }, :scope => :environment_id
  before_validation :create_key, :on => :create

  def initialize(*args)
    super(*args)
  end

  def key=(value)
    if self[:key] && system
      raise ArgumentError, 'Can\'t change key of system role'
    else
      self[:key] = value
    end
  end

  def permissions
    self[:permissions] ||= []
  end

  def has_permission?(perm)
    permissions.include?(perm)
  end

  def has_kind?(k)
    perms[k] && permissions.any?{|p| perms[k].keys.include?(p)}
  end

  def kind
    key.present? && key.starts_with?('environment_') ? 'Environment' : 'Profile'
  end

  def name
    text = self[:name]
    self.class.included_modules.map(&:to_s).include?('GetText') ? gettext(text) : text
  end

  before_destroy :check_for_system_defined_role
  def check_for_system_defined_role
    ! self.system
  end

  protected
  def perms
    ActiveRecord::Base::PERMISSIONS
  end

  private
  def create_key
    self.key = 'profile_' + self.name.gsub(' ', '_').gsub(/[^a-zA-Z0-9_]/, '').downcase if self.key.blank? && !self.name.blank?
  end
end