1

我与康康宝石一起工作。

如何获得“CANCAN”来管理两个模型的权限?

谢谢

====================================

我可以做这个 ????

class Ability

  include CanCan::Ability

  def initialize(user)
   # code
  end

  def initialize(accounts)
     #code  
  end
end
4

2 回答 2

4
class ApplicationHelper
  def current_auth_resource
    if admin_signed_in?
      current_admin
    else
      current_user
    end
  end

  def current_ability
      @current_ability or @current_ability = Ability.new(current_auth_resource)
  end
end

class ApplicationController
  include ApplicationHelper
end

class Ability
  include CanCan::Ability

  def initialize(user)      
    if user.is_a?(Admin)
      # add admin permissions only
    elsif user.is_a?(User)
      # add user permissions only
    else
      # add guest permissions only
    end
  end
end

检查这个

于 2013-11-13T08:51:10.867 回答
2

您需要在您的ability.rb 文件中指定某些用户可以操作和执行操作的模型。

class Ability
  include CanCan::Ability

  # usual setup
  def initialize(user)
    user ||= User.new

    if user.has_role? :admin
      can :manage, :all
    end

    if user.has_role? :less_important_role
      can [:read, :update], Model1
      can :manage, Model2
    end
  end
end

查看文档以阅读有关定义能力的更多信息。

于 2013-03-12T00:40:30.790 回答