Class: Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/ability.rb

Instance Method Summary collapse

Constructor Details

#initialize(operator = nil, current_domain = nil) ⇒ Ability

Returns a new instance of Ability.



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
# File 'app/models/ability.rb', line 6

def initialize(operator=nil,current_domain=nil)
  can :read, Goldencobra::Article
  can :read, Goldencobra::Menue
  can :read, Goldencobra::Widget

  #Rechte die alle betreffen
  Goldencobra::Permission.where("action IS NOT NULL").where("role_id IS NULL OR role_id = ''").each do |permission|
    set_permission(permission,current_domain)
  end

  #Rechte, die nur bestimmte nutzerrollen betreffen
  if operator && operator.respond_to?(:roles)
    operator.roles.each do |role|
      role.permissions.each do |permission|
        if check_for_domain_restrictions(permission,current_domain)
          if permission.subject_class == ":all"
              if permission.action.include?("not_")
                cannot permission.action.gsub("not_", "").to_sym, :all
              else
                can permission.action.to_sym, :all
              end
          else
            set_permission(permission,current_domain)
          end
        end
      end
    end
  end
end

Instance Method Details

#check_for_domain_restrictions(permission, domain) ⇒ Object



72
73
74
# File 'app/models/ability.rb', line 72

def check_for_domain_restrictions(permission,domain)
  permission.domain.blank? || (domain.present? && permission.domain.present? && permission.domain == domain)
end

#set_ability(able, action_name, model_name, id_name = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'app/models/ability.rb', line 56

def set_ability(able, action_name, model_name, id_name=nil)
  if id_name.present?
    send(able, action_name, model_name, id: id_name)
  else
    send(able, action_name, model_name)
  end
end

#set_child_permissions(able, action_name, model_name, id_name) ⇒ Object



64
65
66
67
68
69
70
# File 'app/models/ability.rb', line 64

def set_child_permissions(able, action_name, model_name,id_name)
  if %w(can cannot).include?(able) && model_name.new.respond_to?(:descendant_ids) && object = model_name.find_by_id(id_name)
    object.descendant_ids.each do |child_id|
      set_ability(able, action_name, model_name, child_id)
    end
  end
end

#set_permission(permission, current_domain) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/ability.rb', line 36

def set_permission(permission,current_domain)
  able = 'can'
  action_name = permission.action.to_sym
  model_name = permission.subject_class.constantize

  if permission.action.include?("not_")
    action_name = permission.action.gsub("not_", "").to_sym
    able = 'cannot'
  end

  if check_for_domain_restrictions(permission,current_domain)
    if permission.subject_id.present?
      set_ability(able, action_name, model_name, permission.subject_id.to_i)
      set_child_permissions(able, action_name, model_name, permission.subject_id.to_i)
    else
      set_ability(able, action_name, model_name)
    end
  end
end