首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CanCan:限制用户根据角色设置特定模型属性的能力

CanCan:限制用户根据角色设置特定模型属性的能力
EN

Stack Overflow用户
提问于 2011-05-07 22:11:13
回答 4查看 7.7K关注 0票数 20

我有一个具有:published属性(boolean)的Post模型和一个具有role属性(string)的用户模型。有三个角色:ROLES = %w[admin publisher author]

我不希望角色是author的用户能够在帖子模型上设置或编辑:published字段。

我使用的是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

有人有做这类事情的小贴士吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5921591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档