【问题标题】:Role based authorization with cancan doesn't works Rails 4 - Ruby 2.1基于角色的 cancan 授权不起作用 Rails 4 - Ruby 2.1
【发布时间】:2014-03-04 05:47:47
【问题描述】:

我使用 cancan (1.6.10) 和 devise (3.2.2),我一直按照 cancan 作者的建议使用 this guide 实现授权,我需要为用户分配多个角色,然后我决定将其存储到使用位掩码的单个整数列(我在用户模型中添加了一个名为“roles_mask”的列)。

我有这些文件:

  1. user.rb
  2. edit.html.erb

我知道我是逐字阅读本指南的,除了我写的那些表明角色是可访问属性的行:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :roles) }
  end
end

更新用户(记录)时,所有字段都会更新,除了 :roles_mask =/,我不明白为什么没有捕获角色字段。我认为有些东西是我看不到的。谁能帮帮我?

*解决方案

使用cancancan(支持 Rails 4)并更改 application_controller.rb 文件它可以工作(因为角色是非标量属性)。

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, roles: []) }
  end
end

【问题讨论】:

    标签: ruby ruby-on-rails-4 devise cancan


    【解决方案1】:

    CanCan 不支持 Rails 4。但是您可以使用对 Rails4 友好的 CanCanCan:https://github.com/CanCanCommunity/cancancan

    【讨论】:

    • 感谢您的建议,我没有注意到那个细节...我稍后再告诉您。
    • 好吧...@rails4guides.com,这个问题似乎仍然存在于那个gem中,我只是向cancancan社区(github.com/CanCanCommunity/cancancan/issues/11)报告......一些想法?
    • 尝试将强参数中的角色定义为数组:角色:[]
    • 这行得通...非常感谢@rails4guides.com,顺便说一句,如果它在文档中会很有帮助,我将在我的问题中描述说明性解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2019-09-29
    • 2021-09-07
    • 2019-03-02
    相关资源
    最近更新 更多