【问题标题】:Using cancancan's check_authorization with devise with custom controllers?将 cancancan 的 check_authorization 与带有自定义控制器的设计一起使用?
【发布时间】:2020-10-23 22:35:15
【问题描述】:

TL;DR(完整问题如下)

cancan 带有check_authorization,它可以让您知道是否忘记向应用程序的任何部分添加授权。本质上,我只是想知道是否有必要向应用程序的“设计”部分添加授权 - 或者设计是否需要开箱即用? (例如,如果一个用户可以更改另一个用户的密码,那将是非常糟糕的!- 是否需要通过 cancan 来防止这种情况,或者是否设计确保已经开箱即用?)

完整问题

我已经设计和 cancancan 设置,一切似乎都工作。

当我添加check_authorization 后,一切正常,除了登录和退出不再起作用

CanCan::AuthorizationNotPerformed in Devise::SessionsController#destroy
This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check.
Extracted source (around line #266):
264
265
266
267
268
269
              
          next if options[:unless] && controller.send(options[:unless])

          raise AuthorizationNotPerformed,
                'This action failed the check_authorization because it does not authorize_resource. '\
                'Add skip_authorization_check to bypass this check.'
        end

奇怪的是,当用户更新他们的信息时,效果很好(没有错误)

非常感谢任何想法/指针

另外,我应该是设计控制器上的skip_authorization_check 吗?虽然错误信息暗示了这一点,但听起来很冒险

最后,我不知道如何访问设计控制器以添加skip_authorization_check。这是我的目录(使用用户控制器进行设计)

【问题讨论】:

    标签: ruby-on-rails devise cancan ruby-on-rails-6 cancancan


    【解决方案1】:

    答案是您不必担心设计控制器上的skip_authorization_check

    如果您收到任何其他控制器的此错误消息:

    CanCan::AuthorizationNotPerformed (This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check.):
    

    那么解决办法就是

    1. 为需要授权的资源(或其部分)添加授权,或者
    2. 如果资源不需要授权,只需跳过授权检查。

    可以通过将其添加到资源来跳过授权检查

    skip_authorization_check
    

    你可以像这样使用onlyexcept

    skip_authorization_check except: [:index]
    

    skip_authorization_check only: [:show, :edit]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多