Sha256: 37110d20f7c65a62e2db36f08fd2071b04030cf5d740fe16315b4f4a996c336d

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Permits
  class Ability
    include CanCan::Ability

    # set up each RolePermit instance to share this same Ability 
    # so that the can and cannot operations work on the same permission collection!
    def self.permits ability
      special_permits << [:system, :any].map{|name| make_permit(role, ability)}
      role_permits = Permits::Roles.available.inject([]) do |permits, role|
        permits << make_permit role, ability
      end
      special_permits + role_permits
    end

    def initialize(user, request=nil)
      # put ability logic here!
      user ||= Guest.new   
                  
      Ability.permits(self).each do |permit|
        # get role name of permit 
        permit_role = permit.class.demodulize.to_sym                      
        
        if permit_role == :system
          # always execute system permit
          result = role_permit.permit?(user, request)
          break if result == :break
        else
          # only execute the permit if the user has the role of the permit or is for any role
          role_permit.permit?(user, request) if user.has_role?(permit_role) || permit_role == :any
        end
      end
    end      
    
    protected
    
    def self.make_permit role, ability
      "Permit::#{role.to_s.camelize}".constantize.new(ability)
    end          
  end
end      

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cancan-permits-0.1.0 lib/cancan-permits/permits/abiity.rb