Module: Invitational::CanCan::Ability
- Includes:
- CanCan::Ability
- Defined in:
- lib/invitational/cancan.rb
Instance Method Summary collapse
- #attribute_roles(attribute, roles) ⇒ Object
- #can(action = nil, subject = nil, conditions = nil, &block) ⇒ Object
- #cannot(action = nil, subject = nil, conditions = nil, &block) ⇒ Object
- #check_permission_for(model, user, in_roles, role_specific) ⇒ Object
- #check_permission_for_attribute(model, user, role, role_specific) ⇒ Object
- #check_permission_for_keyed_roles(model, user, role, role_specific) ⇒ Object
- #check_permission_for_system_role(user, role, role_specific) ⇒ Object
- #setup_role_based_block_for(roles, subject, action, role_specific) ⇒ Object
- #system_roles(roles) ⇒ Object
Instance Method Details
#attribute_roles(attribute, roles) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/invitational/cancan.rb', line 96 def attribute_roles attribute, roles hash = nil if attribute.respond_to? :each attribute.reverse.each do |attr| if hash.nil? hash = {attr => roles} else hash = {attr => hash} end end else hash = {attribute => roles} end hash end |
#can(action = nil, subject = nil, conditions = nil, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/invitational/cancan.rb', line 6 def can(action = nil, subject = nil, conditions = nil, &block) if conditions && conditions.has_key?(:roles) roles = conditions.delete(:roles) if conditions conditions = nil if conditions and conditions.empty? block ||= setup_role_based_block_for roles, subject, action, false end add_rule ::CanCan::Rule.new(true, action, subject, conditions, block) end |
#cannot(action = nil, subject = nil, conditions = nil, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/invitational/cancan.rb', line 17 def cannot(action = nil, subject = nil, conditions = nil, &block) if conditions && conditions.has_key?(:roles) roles = conditions.delete(:roles) if conditions conditions = nil if conditions and conditions.empty? block ||= setup_role_based_block_for roles, subject, action, true end add_rule ::CanCan::Rule.new(false, action, subject, conditions, block) end |
#check_permission_for(model, user, in_roles, role_specific) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/invitational/cancan.rb', line 51 def model, user, in_roles, role_specific in_roles.inject(false) do |result,role| result || if role.respond_to? :values model, user, role, role_specific elsif role == :* Invitational::ChecksForInvitation.for(user, model) else Invitational::ChecksForInvitation.for(user, model, role, role_specific) end end end |
#check_permission_for_attribute(model, user, role, role_specific) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/invitational/cancan.rb', line 83 def model, user, role, role_specific method = role.keys.first = model.send(method) if .respond_to? :any? .any? do |model| model, user, role.values.flatten, role_specific end else , user, role.values.flatten, role_specific end end |
#check_permission_for_keyed_roles(model, user, role, role_specific) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/invitational/cancan.rb', line 65 def model, user, role, role_specific key = role.keys.first if key == :system_roles user, role, role_specific else model, user, role, role_specific end end |
#check_permission_for_system_role(user, role, role_specific) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/invitational/cancan.rb', line 75 def user, role, role_specific roles = role.values.flatten user.uberadmin? || roles.any? do |system_role| user.invited_to_system? system_role end end |
#setup_role_based_block_for(roles, subject, action, role_specific) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/invitational/cancan.rb', line 28 def setup_role_based_block_for roles, subject, action, role_specific key = subject.name.underscore + action.to_s if roles.respond_to? :values role_type, roles = [roles.keys.first, roles.values.first] else role_type = subject end unless role_mappings.has_key?(key) role_mappings[key] = [] end role_mappings[key] += roles block = ->(model){ roles = role_mappings[key] model, user, roles, role_specific } block end |
#system_roles(roles) ⇒ Object
113 114 115 |
# File 'lib/invitational/cancan.rb', line 113 def system_roles roles {system_roles: roles} end |