0

有 2 个命名空间:

api/public
api/mobile

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

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

  def show
    @post = Post.find(params[:id])
  end
end

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

class API::Mobile::PostsController < API::Public::PostsController
  skip_before_action :doorkeeper_authorize!
  before_action -> { doorkeeper_authorize! :mobile }
end

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

有没有办法解决这个问题?

4

2 回答 2

4

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
于 2014-12-22T11:59:41.833 回答
4

您可以在 lambda 中调用一个方法,该方法返回要授权的内容:

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

  def show
    @post = Post.find(params[:id])
  end

  protected
    def authorization_scope
      :public
    end
end

然后你的子类只需要覆盖该方法而不会陷入skip_before_filter的痛苦

class API::Mobile::PostsController < API::Public::PostsController
  protected
    def authorization_scope
      :mobile
    end
end
于 2014-12-22T13:38:24.340 回答