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

时间:2017-07-21 11:03:14

标签: elixir phoenix-framework graphql absinthe

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

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

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

2 个答案:

答案 0 :(得分:5)

修改 警告: Graphql documentation不同意这种做法。谨慎使用。无论您需要私有字段,都必须包含适当的中间件。

使用absinthe middleware

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

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回调的一些创造性使用,您可以从主模式文件中获得大量此类函数。

答案 1 :(得分: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定义。

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