authorize_resource 在应该阻止访问时不阻止访问? 供参考

问题描述

我有以下几点:

class ConversationsController < ApplicationController
  before_action :set_conversation_show,only: [:show]
  authorize_resource

  def show
  end

  private 
    def set_conversation_show
      @conversation = Conversation.where("sender_id = ?",current_user.id).find_by_id(params.fetch(:id))
    end
end

我的理解是:

  • 由于模型实例 (@conversation) 设置在 before_action 中,authorize_resource 应该简单地检查 current_user 是否可以访问该资源,如果没有,拒绝访问。

但是,允许访问....

当我将 authorize_resource 更改为 load_and_authorize_resource 时,访问将被拒绝。

注意:authorize_resourceload_and_authorize_resource 相同,只是它不假设应该加载什么模型实例,而是依赖于通过 before_action 提供的模型实例。 (更多here)。

出于这个原因,我更喜欢使用 authorize_resource - 因为它不会自动假定要检查权限的模型实例。

那么问题是,为什么上面的代码没有按预期工作并拒绝访问?

供参考

相关的ability.rb

can [:show],[Conversation] do |conversation|
  conversation.sender_id == user.id
end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...