ruby-on-rails - CanCan gem |不能 :index, 用户

标签 ruby-on-rails cancan

非常基本的用户模型,我希望管理员用户:管理所有

else 不能:index、User 和其他一些选项,但是当我尝试阻止非管理员用户查看用户索引时,管理员用户也没有访问权限。

这是我的能力.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new #guest user

    can :manage, :all if user.role == "admin" #if user.admin? can :manage, :all
    can :assign_role, User 

    else
      can :read, :all
      can :create, User


      cannot :assign_role, User
      cannot :index, User

      can [:show, :edit, :update], User do |current_user|
              user.id == current_user.id || user.role == "admin"
            end



  end
end

我能做些什么来阻止所有用户被用户索引阻止?

问候

最佳答案

代码中的 if-else 有问题。

if user.role == "admin"
  can :manage, :all
  can :assign_role, User 

else
  can :read, :all
  can :create, User


  cannot :assign_role, User
  cannot :index, User
  can [:show, :edit, :update], User do |current_user|
    user.id == current_user.id || user.role == "admin"
  end

end

而且您不必明显拒绝非管理员用户分配角色(不能:assign_role,用户)。

关于ruby-on-rails - CanCan gem |不能 :index, 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2367169/

相关文章:

mysql - 如何在 Ruby On Rails 的 MySQL 选择中使用 '?' 符号?

authorization - 使用 cancan 防止访问 Controller

ruby-on-rails - 无法使用 rspec Controller POST 创建操作(设计和 cancan)进行测试

ruby-on-rails - 如何在 Ruby 中获取 http 请求的内容?

ruby-on-rails - 自定义设计 Cookie

ruby-on-rails - rails 3 : User input escaping working differently in views and mailer

ruby-on-rails - CanCan:在除 MainApp 的命名空间中的 load_and_authorize_resource

ruby-on-rails - 如何使 cancan 授权 gem 将用户限制为他们拥有的数据?

ruby-on-rails - 与运算符(operator)从头开始的 Rails 授权

ruby-on-rails - 为 ActiveRecord 关联扩展编写规范