Sha256: 763930fa9aaa055149ee48b7ec0afaafe4fe8e131f18ef47a652a682e493ccfd

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module RolePermit

  class Base 
    attr_accessor :ability
       
    def initialize(ability)
      @ability = ability
    end

    def can(action, subject, conditions = nil, &block)
      ability.can(action, subject, conditions, &block)
    end
        
    def cannot(action, subject, conditions = nil, &block)
      ability.cannot(action, subject, conditions, &block)
    end
    
    def owns(user, clazz)
      can :manage, clazz do |comment|
        comment.try(:user) == user || comment.try(:owner) == user
      end                
    end 
    
    def permit?(user)    
      user.has ability      
    end
  end
  
  class Admin < Base
    def initialize(ability)
      super
    end

    def permit?(user)    
      super
      return if !user.role? :admin    
      can :manage, :all    
    end  
  end

  class User < Base
    def initialize(ability)
      super
    end

    def permit?(user) 
      super
      return if user.role? :admin
      can :read, :all           
      
      # user.owns(Comment)
      
      # a user can manage comments he/she created
      # can :manage, Comment do |comment|
      #   comment.try(:user) == user
      # end            
      
      # can :create, Comment
    end  
  end

  class Moderator < Base
    def initialize(ability)
      super
    end
    
    def permit?(user)
      super
      return if !user.role?(:moderator)
      can :read, :all      
      # owns(user, Comment)
    end  
  end


  class Author < Base
    def initialize(ability)
      super
    end
    
    def permit?(user)     
      super      
      return if !user.role? :author
      # can :create, Post

      # an author can manage posts he/she created      
      # can :update, Post do |post|
      #   post.try(:user) == user      
      # end
    end  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
auth-assistant-0.4.0 lib/permits.rb