Ruby on Rails 的skip_before_action 无法按预期工作

Ива*_*вац 1 ruby-on-rails doorkeeper

有 2 个命名空间:

api/public
api/mobile
Run Code Online (Sandbox Code Playgroud)

在公共控制器中创建具有适当范围的门卫授权。例如:

class API::Public::PostsController < ApplicationController
  before_action -> { doorkeeper_authorize! :public }

  def show
    @post = Post.find(params[:id])
  end
end
Run Code Online (Sandbox Code Playgroud)

移动命名空间中的控制器继承自公共命名空间中的控制器。例如:

class API::Mobile::PostsController < API::Public::PostsController
  skip_before_action :doorkeeper_authorize!
  before_action -> { doorkeeper_authorize! :mobile }
end
Run Code Online (Sandbox Code Playgroud)

因此,这里的要点是功能是相同的,如果移动设备存在一些差异,则可以在移动命名空间中覆盖操作。问题是这两个命名空间的范围不同,但跳过了doorkeeper_authorize!不起作用。

有办法解决这个问题吗?

小智 5

skip_before_filter适用于跳过方法,而不是跳过 lambdas/procs。尝试创建一个公共授权方法:

class API::Public::PostsController < ApplicationController
  before_action :authorize_public

  ...

  def authorize_public
    doorkeeper_authorize! :public
  end
end

class API::Mobile::PostsController < API::Public::PostsController
  skip_before_action :authorize_public
  ...
end
Run Code Online (Sandbox Code Playgroud)