Symfony2-创建一个Doctrine过滤器以选择当前的用户数据 [英] Symfony2 - Create a Doctrine filter to select current user data

查看:73
本文介绍了Symfony2-创建一个Doctrine过滤器以选择当前的用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony 2构建Saas/Multitenant应用程序.我已经创建了一个Doctrine事件订阅者来添加和更新行的所有者,创建行的用户,修改行的用户,时间戳等等.

I'm building a Saas / Multitenant application using Symfony 2. I've created a Doctrine event-subscriber to add and update the owner of a row, the user who created it, the user who modified it, timestamps and so.

现在我需要实现某种过滤器,以便用户登录后,他只能看到其公司的数据.虽然我的第一个操作是使用Doctrine preLoad事件,但是此事件不存在...据我所知,我必须使用Doctrine过滤器,不是吗?如果是这样,此过滤器如何访问用户数据以读取公司ID?我必须使用依赖注入来注入它吗?有什么标准方法可以实现我的目标?

And now I need to implement some kind of filter so when a user is logged in, he only can see data from his company. My first though was using a Doctrine preLoad event, but this event doesn't exist... As far as I know, I must use Doctrine filters, isn't it? If so, how can this filter access user data to read the company id? Must I inject it using the Dependency Injection? Is there any standard way to accomplish my goal?

更新 我正在寻找的是创建某种Doctrine插件/挂钩,因此每次我调用从数据库中获取数据的任何函数(find,findOneBy等)时,我正在获取的实体都实现了一个特定的接口,一个额外的"AND company_id =:id" SQL序列已添加到生成的查询中,因此控制器或模型都不会从其他公司接收数据.

UPDATE What I'm looking for is to create some kind of Doctrine plugin/hook so everytime I call any function that fetch data from the database (find, findOneBy, etc), and the entity I'm fetching implements a particular interface, an extra 'AND company_id=:id' SQL sequence is added to the generated query, so neither the controller or the model receives data from other companies.

推荐答案

为此,您可以使用DoctrineFilter 官方文档的链接 Doctrine2 SQL过滤器

For this you can use a DoctrineFilter Link from official doc Doctrine2 SQL Filters

namespace Rwmt\Bundle\RwmtBundle\DoctrineFilters;

use Doctrine\ORM\Mapping\ClassMetaData,
    Doctrine\ORM\Query\Filter\SQLFilter;

class MultiTenantFilter extends SQLFilter
{
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        // Check if the entity implements the MultiTenant interface
        if (!$targetEntity->reflClass->implementsInterface('Rwmt\Bundle\RwmtBundle\Entity\MultiTenant')) {
            return "";
        }

        return $targetTableAlias.'.tenant_id = ' . $this->getParameter('tenantId');
    }
}

要设置过滤器中使用的参数tenantId,必须启用过滤器并设置参数

And to set the parameter tenantId used in the filter you must enable the filter and set the parameter

$filter = $em->getFilters()->enable('multi_tenant');
$filter->setParameter('tenantId', $tenant->getId(), 'integer');

对于MultiTenant接口,这只是实体要实现的

As for the MultiTenant interface is just something for the entities to implement

namespace Rwmt\Bundle\RwmtBundle\Entity;
use Rwmt\Bundle\RwmtBundle\Entity\Tenant;

interface MultiTenant
{
    public function setTenant(Tenant $tenant);
    public function getTenant();
}

这篇关于Symfony2-创建一个Doctrine过滤器以选择当前的用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆