Sha256: e44ec9d822f69f4080556b6703b3176c0f5392783e2f6b74c9e2eee84982f137

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

module Canner

  class Policy

    def initialize(current_user, method, current_branch=nil)
      @current_user = current_user
      @current_branch = current_branch
      @method = method.to_sym
      @roles = fetch_roles
    end

    # if you handle your roles differently you'll need to override.
    # use: rails g canner:fetch_roles
    # expects an array or strings or symbols that represent the user roles
    def fetch_roles
      @current_user.nil? ? [] : @current_user.roles
    end

    # implement in your policy class to auto scope in an action
    def canner_scope
      raise ArgumentError.new("NOT IMPLEMENTED")
      # ex:
      # case @method
      # when :index
      #   User.by_branch(@current_branch)
      # else
      #   User.none
      # end
    end

    # implment in your policy class.
    # return true when the user can access the action or resource and false when they can't
    def can?
      raise ArgumentError.new("NOT IMPLEMENTED")
      # ex:
      # case @method
      # when :index, :show
      #   has_role?(:admin)
      # else
      #   false
      # end
    end

    protected

    def is_method?(methods)
      prepare(methods).include?(@method)
    end

    def has_role?(roles)
      begin
        @roles.any?{|r| Util.prepare(roles).include?(r.name.to_sym) }
      rescue Exception => e
        raise ArgumentError.new "Canner: Problem fetching user roles. If current_user.roles isn't how you do it see wiki for overriding fetch_roles."
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
canner-0.3.0 lib/canner/policy.rb
canner-0.2.2 lib/canner/policy.rb
canner-0.2.1 lib/canner/policy.rb