我正在尝试在 ruby 中创建一个 Restful json api - 所以我在 Rack 中使用 grape ( https://github.com/intridea/grape )。 我没有在这个项目中使用 Rails,所以康康舞、巫术等……似乎不是最佳选择。另外,我讨厌将一堆命令式逻辑混入 grape 的声明式 DSL 中。
虽然 grape 内置了身份验证支持,但我没有看到任何关于授权的信息。这似乎是一个足够常见的用例,这条路以前就有人走过,但在谷歌和 grape 代码库本身进行了一些非常彻底的挖掘之后,我什么也没发现。
有没有人在他们的葡萄项目中实现过类似的东西?你用的是什么?
最佳答案
这可能有点太晚了,但无论如何。我建议您使用 Pundit 进行授权,这非常简单。要在您的 Grape API 端点中使用它,您需要包含 Pundit 助手:
class API < Grape::API
format :json
helpers Pundit
helpers do
def current_user
resource_owner
end
end
mount FoosAPI
end
现在在您的 API 端点中,您应该能够像在 Rails Controller 中一样使用 authorize foo, action?
:
class FoosAPI < Grape::API
get ':id' do
foo = Foo.find(params[:id])
authorize foo, :show?
present foo, with: FooEntity
end
end
希望对您有所帮助!
关于ruby - {葡萄}授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14966210/