繁体   English   中英

Graphql Absinthe基于Elixir权限的可访问字段

[英]Graphql Absinthe Elixir permission based accessible fields

定义所有用户可能无法访问的字段的正确方法是什么。

例如,普通用户可以查询用户并查找其他用户句柄,但只有管理员用户才能找到他们的电子邮件地址。 用户类型将其定义为字段,但可能无法访问。 是否应该有一般用户可以看到的单独类型? 你会如何定义它?

对不起,如果不是那么清楚,我就是不具备词汇量。

编辑: 警告: Graphql文档不同意此方法。 谨慎使用。 无论您需要私有字段,都必须包含适当的中间件。

使用苦艾酒中间件

这是一些代码如何做到这一点。 在此示例中,经过身份验证的用户可以查看电子邮件地址。 匿名用户不能。 您可以调整逻辑以要求您需要的任何权限。

defmodule MySite.Middleware.RequireAuthenticated do
  @behaviour Absinthe.Middleware

  @moduledoc """
  Middleware to require authenticated user
  """

  def call(resolution, config) do
    case resolution.context do
      %{current_user: _} ->
        resolution
      _ ->
        Absinthe.Resolution.put_result(resolution, {:error, "unauthenticated"})
    end
  end
end

然后你定义你的对象:

  object :user do
    field :id, :id
    field :username, :string 
    field :email, :string do
      middleware MySite.Middleware.RequireAuthenticated
      middleware Absinthe.Middleware.MapGet, :email
    end
  end

因此,我们的现场电子邮件受RequireAuthenticated中间件保护。 但根据上面的链接

中间件/ 3的一个用途是在字段上设置默认中间件,替换default_resolver宏。

通过在字段上使用中间件/ 2宏也会发生这种情况。 这就是我们需要添加的原因

  middleware Absinthe.Middleware.MapGet, :email

到该领域的中间件列表。

最后,当我们执行查询时

query {
  user(id: 1){
    username
    email
    id
  }
}

我们得到响应,填充的开放字段和受保护的字段无效

{
  "errors": [
    {
      "message": "In field \"email\": unauthenticated",
      "locations": [
        {
          "line": 4,
          "column": 0
        }
      ]
    }
  ],
  "data": {
    "user": {
      "username": "MyAwesomeUsername",
      "id": "1",
      "email": null
    }
  }
}

您还可以使用中间件/ 3回调,因此您的对象不会太冗长

  def middleware(middleware, %{identifier: :email} = field, _object) do
    [MySite.Middleware.RequireAuthenticated] ++
      [{Absinthe.Middleware.MapGet, :email}] ++
      middleware
  end

通过对__using __ / 1回调的一些创造性使用,您可以从主模式文件中获取大量此类函数。

@voger给出了一个很棒的答案,我只是想根据接受的问题发布宏观样本。 我目前正在使用它来验证我的架构中的每个字段。

这是一个宏定义:

defmodule MyApp.Notation do
  defmacro protected_field(field, type, viewers, opts \\ []) do
    {ast, other_opts} =
      case Keyword.split(opts, [:do]) do
        {[do: ast], other_opts} ->
          {ast, other_opts}

        {_, other_opts} ->
          {[], other_opts}
      end

    auth_middleware =
      if viewers !== :public do
        quote do
          middleware(MyApp.Middleware.ProtectedField, unquote(viewers))
        end
      end

    quote do
      field(unquote(field), unquote(type), unquote(other_opts)) do
        unquote(auth_middleware)
        middleware(Absinthe.Middleware.MapGet, unquote(field))
        unquote(ast)
      end
    end
  end
end

然后在您的类型定义中,您可以执行此操作。

import MyApp.Notation

# ...

object :an_object do
  protected_field(:description, :string, [User, Admin]) do
    middleware(OtherMiddleware)
    resolve(fn _, _, _ ->
      # Custom Resolve
    end)
  end

  protected_field(:name, :stirng, :public, resolve: &custom_resolve/2)
end

说明:

它添加了一个参数,我称之为viewers ,我只是转发到我的中间件来检查用户类型是否正确。 在这种情况下,我实际上有不同的模型,我称之为AdminUser ,我可以检查当前用户。 这只是一种方法的示例,因此您的解决方案可能会有所不同。 我有一个特殊情况:public领域只是一个直通。

这很好,因为我可以使用额外的参数注入中间件并将其他所有内容转发到原始field定义。

我希望这有助于增加答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM