api-platform: 根据访问逻辑限制用户可以查看的记录
创始人
2024-09-07 04:30:51
0

要根据访问逻辑限制用户可以查看的记录,可以使用api-platform的自定义数据提供程序和表达式语言。

以下是一个示例解决方案的代码示例:

首先,创建一个自定义数据提供程序类,该类将过滤查询结果以仅返回用户有权访问的记录。例如,您可以根据用户的角色或其他条件进行过滤。

// src/DataProvider/RestrictedDataProvider.php

namespace App\DataProvider;

use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use Symfony\Component\Security\Core\Security;

class RestrictedDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
    private $security;
    private $collectionDataProvider;

    public function __construct(Security $security, CollectionDataProviderInterface $collectionDataProvider)
    {
        $this->security = $security;
        $this->collectionDataProvider = $collectionDataProvider;
    }

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return true; // 支持所有资源类
    }

    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
    {
        $user = $this->security->getUser();
        $isAdmin = in_array('ROLE_ADMIN', $user->getRoles());

        // 在此处根据访问逻辑定义查询条件
        $criteria = [
            'user' => $isAdmin ? $user : $user->getId(),
        ];

        // 使用传入的CollectionDataProvider获取原始查询结果
        $rawCollection = $this->collectionDataProvider->getCollection($resourceClass, $operationName, $context);

        // 过滤查询结果以仅返回用户有权访问的记录
        $filteredCollection = array_filter($rawCollection, function ($item) use ($criteria) {
            // 根据条件过滤记录
            // 例如:return $item['userId'] === $criteria['user']->getId();
        });

        return $filteredCollection;
    }
}

然后,在服务配置中注册自定义数据提供程序:

# config/services.yaml

services:
    App\DataProvider\RestrictedDataProvider:
        arguments:
            - '@security.helper'
            - '@api_platform.collection_data_provider'

最后,将自定义数据提供程序应用于要限制访问的实体类上:

// src/Entity/YourEntity.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\DataProvider\RestrictedDataProvider;

/**
 * @ApiResource(
 *     collectionOperations={
 *         "get"={
 *             "method"="GET",
 *             "path"="/your_entities",
 *             "access_control"="is_granted('ROLE_USER')"
 *         }
 *     },
 *     itemOperations={},
 *     dataProviderClass=RestrictedDataProvider::class
 * )
 */
class YourEntity
{
    // 实体类定义
}

在上述示例中,我们创建了一个自定义数据提供程序RestrictedDataProvider,该提供程序在getCollection方法中根据用户的角色和其他条件过滤查询结果。然后,我们将自定义数据提供程序应用于要限制访问的实体类上,并在access_control选项中设置访问控制规则。

请注意,您需要根据自己的需求修改自定义数据提供程序中的过滤逻辑和查询条件。

相关内容

热门资讯

透视插件!wepoker可以设... 透视插件!wepoker可以设置盖牌-关于开挂透视辅助教程(有挂教学)透视插件!wepoker可以设...
最新技巧“浙江游戏大厅修改器”... 最新技巧“浙江游戏大厅修改器”原先有开挂辅助挂(确实有挂);1、这是跨平台的浙江游戏大厅修改器黑科技...
透视挂透视!hhpoker可以... 透视挂透视!hhpoker可以开挂-揭露开挂透视辅助攻略(有挂攻略)1、hhpoker可以开挂系统规...
玩家必看分享“小程序微乐游戏辅... 玩家必看分享“小程序微乐游戏辅助器”其实有开挂辅助器(详细教程);1、让任何用户在无需AI插件第三方...
透视工具!wepoker国外版... 透视工具!wepoker国外版透视-教你开挂透视辅助工具(了解有挂)1、全新机制【wepoker国外...
专业讨论“欢乐游戏城破解版内置... 专业讨论“欢乐游戏城破解版内置修改器”本来有开挂辅助脚本(了解有挂);详细欢乐游戏城破解版内置修改器...
一分钟教你“宝宝游戏辅助”好像... 您好:宝宝游戏辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
透视最新!hhpoker透视挂... 透视最新!hhpoker透视挂实战视频-解谜开挂透视辅助攻略(有挂技术)1、超多福利:超高返利,海量...
9分钟了解“四川家园游戏辅助器... 9分钟了解“四川家园游戏辅助器平台交易”果然有开挂辅助下载(有挂规律);亲真的是有正版授权,小编(透...
透视科技!wepoker智能辅... 透视科技!wepoker智能辅助插件-教你开挂透视辅助插件(有挂方针)1、进入到wepoker智能辅...