Sha256: 32ed74f679f609e3231594706c20802887fea2ffd0cfa90844b4b25294f580cc

Contents?: true

Size: 1.48 KB

Versions: 24

Compression:

Stored size: 1.48 KB

Contents

module Cream::Helper
  module Role
    # does the user have ANY of the given roles?
    # Uses generic roles API    
    def has_role?(*roles)
      current_user.has_role? roles.flatten
    end

    # does the user have ALL of the given roles?
    # Uses generic roles API
    def has_roles?(*roles)
      current_user.has_roles? roles.flatten
    end

    # returns true if the current user owns the object
    # tries default 'owner' relations if none given as an argument
    def owner?(obj, relation=nil)
      if relation     
        return true if user_relation?(obj, relation)
      end                                
      [:user, :owner, :author].each do |relation|        
        return true if user_relation?(obj, relation)
      end
      false
    end    

    # execute block if user DOES have any of the given roles
    def for_roles(*user_roles, &block) 
      user_roles = user_roles.flatten
      yield if has_role?(user_roles) && block
    end 
    alias_method :for_role, :for_roles

    # execute block if user DOES NOT have any of the given roles
    def not_for_roles(*user_roles, &block)            
      user_roles = user_roles.flatten
      yield if !has_role?(user_roles) && block
    end        
    alias_method :not_for_role, :not_for_roles

    protected
    
    def user_relation? obj, relation
      raise ArgumentError, "User method must be a Symbol or String" if !relation.kind_of_label?
      current_user == obj.send(relation) if obj.respond_to? relation
    end
  end    
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
cream-0.8.6 lib/cream/helper/role.rb
cream-0.8.5 lib/cream/helper/role.rb
cream-0.8.4 lib/cream/helper/role.rb
cream-0.8.3 lib/cream/helper/role.rb
cream-0.8.2 lib/cream/helper/role.rb
cream-0.8.1 lib/cream/helper/role.rb
cream-0.8.0 lib/cream/helper/role.rb
cream-0.7.10 lib/cream/helper/role.rb
cream-0.7.8 lib/cream/helper/role.rb
cream-0.7.7 lib/cream/helper/role.rb
cream-0.7.6 lib/cream/helper/role.rb
cream-0.7.5 lib/cream/helper/role.rb
cream-0.7.3 lib/cream/helper/role.rb
cream-0.7.2 lib/cream/helper/role.rb
cream-0.7.0 lib/cream/helper/role.rb
cream-0.6.4 lib/cream/helper/role.rb
cream-0.6.3 lib/cream/helper/role.rb
cream-0.6.1 lib/cream/helper/role.rb
cream-0.6.0 lib/cream/helper/role.rb
cream-0.5.10 lib/cream/helper/role.rb