Laravel Lighthouse 限制变异字段

Laravel Lighthouse restrict mutation field

我想保护内容类型的某些特定字段,只允许管理员用户修改值,但允许用户访问它。

想象一下带有 is_admin 字段的 User 类型。只有管​​理员应该能够更新它,但每个人都应该能够阅读它。

type User {
    id: ID!
    name: String!
    email: String!
    is_admin: Boolean!
}

can 指令似乎不适用于突变中的字段。起初我尝试使用自定义策略添加 @can(ability: "setAdmin") 但它没有任何效果。同样的 can/policy 用于突变“有效”,但这不够精细。

自定义字段限制 using a custom directive 似乎应该有所帮助,但这似乎也不适用于突变输入类型的字段级别。

type mutation {
    updateUser(
        input: UpdateUserInput! @spread
    ): User @update @middleware(checks: ["auth:api"])
}

input UpdateUserInput {
    id: ID!
    name: String!
    email: String!
    is_admin: Boolean! @adminOnly
}

app/GraphQL/Directives/AdminOnlyDirective.php

中使用此自定义指令
<?php

namespace App\GraphQL\Directives;

use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class AdminOnlyDirective extends BaseDirective implements FieldMiddleware, DefinedDirective
{
    /**
     * Name of the directive as used in the schema.
     *
     * @return string
     */
    public function name(): string
    {
        return 'adminOnly';
    }

    public static function definition(): string
    {
        return /** @lang GraphQL */ <<<GRAPHQL
"""
Limit field update to only admin.
"""
directive @adminOnly() on FIELD_DEFINITION
GRAPHQL;
    }

    public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
    {
        $originalResolver = $fieldValue->getResolver();

        return $next(
            $fieldValue->setResolver(
                function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($originalResolver) {
                    $user = $context->user();
                    if (
                        // Unauthenticated users don't get to see anything
                        ! $user
                        // The user's role has to match have the required role
                        || !$user->is_admin
                    ) {
                        return null;
                    }

                    return $originalResolver($root, $args, $context, $resolveInfo);
                }
            )
        );
    }
}

那么,有没有一种方法可以防止使用 laravel 灯塔“更新”特定字段?

我想到的第一个想法是创建两个不同的输入 and/or 突变。例如。对于有权访问该字段的管理员:

updateUserAsAdmin(
    input: UpdateUserFullInput! @spread
): User @update 
        @middleware(checks: ["auth:api"]) 
        @can("users.update.full")

并且 UpdateUserFullInput 包含 is_admin 字段。

我也遇到过几次这个讨论:https://github.com/nuwave/lighthouse/issues/325 或许你也可以在这里找到一些有用的想法。

您可能还想查看官方文档:https://github.com/nuwave/lighthouse/blob/master/docs/master/security/authorization.md#custom-field-restrictions

目前,您可以在插入数据库之前使用 https://lighthouse-php.com/4.16/custom-directives/argument-directives.html#argtransformerdirective 将该字段转换为 null,或者直接抛出错误以避免特定字段发生更改,就像 @trim 行为;

在 Lighthouse v5 中,它的 class ArgTransformerDirective 已重命名为 ArgSanitizerDirective,方法 transform 已重命名为 sanitize

https://github.com/nuwave/lighthouse/blob/v5.0-alpha.3/src/Schema/Directives/TrimDirective.php

额外:

我还在弄清楚@can 是如何工作的,因为我仍然需要删除整个属性而不是将 null 传递到我的数据库;

更新:@can only apply to input type instead of input type