CanCan:限制用户根据角色设置某些模型属性的能力

ste*_*och 19 ruby-on-rails cancan

我有一个带有属性(布尔值)的Post模型和一个带有属性(string)的User模型.有三个角色::publishedroleROLES = %w[admin publisher author]

我不想让角色为作者应能设置的或编辑时,:published在Post模型领域.

我正在使用CanCan(和RailsAdmin gem),我的简化Ability.rb文件如下所示:

class Ability
  include CanCan::Ability
  def initialize(user)
    user ||= User.new

    if user.role? :admin
      can :manage, :all
    elsif user.role? :publisher
      can :manage, Post
    elsif user.role? :author
      # I want to prevent these guys from setting the :published attribute
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

做这种事的人有什么提示吗?

Pio*_*ski 13

到目前为止,这是不可能的.但根据这个:https://github.com/ryanb/cancan/issues/326这个功能应该在cancan 2.0中.

更新:你可以在CanCan 2.0分支上看到这个:https://github.com/ryanb/cancan/tree/2.0 "资源属性"部分