Symfony2在实体存储库中获取用户标识

时间:2022-10-16 14:58:16

I have coded a page which displays all administrators of the system. What I want to do is customize my query so that it would exclude the currently authenticated user from the list.
Now I know I can get the user_id from the controller and pass it to the entity repository, but I was wondering if there is a way to access that directly through the Entity Repository?

我编写了一个显示系统所有管理员的页面。我想要做的是自定义我的查询,以便它从列表中排除当前经过身份验证的用户。现在我知道我可以从控制器获取user_id并将其传递给实体存储库,但我想知道是否有办法直接通过实体存储库访问它?

For example:

例如:

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class AdminUserRepository extends EntityRepository implements UserProviderInterface
{
   public function getAdmins($int = 10, $offset = 0, array $orderBy = array('admin_id', 'asc')){
        $admin_id = fetch the admin id ?;
        $query = $this->createQueryBuilder('admins')
            ->where("admins.admin_id != '".$admin_id."'")
            ->orderBy('admins.'.$orderBy[0], $orderBy[1])
            ->setFirstResult($offset)
            ->setMaxResults($int)
            ->getQuery()
            ->getResult();
        return $query;
   }
}

2 个解决方案

#1


1  

So, you have a current User ID (let's say, $currentUserId), and you want to write a query for all Users with an ID that is not equal to $currentUserId?

那么,你有一个当前的用户名(比方说,$ currentUserId),你想为ID不等于$ currentUserId的所有用户写一个查询?

Try this:

尝试这个:

class SomeController
{
    public function someAction()
    {
        ...

        $qb = $userRepository->createQueryBuilder("user")
            ->where("user.id != ?1")
            ->setParameter(1, $currentUserId);

        $usersExcludingCurrent = $qb->getQuery()->getResult();

    ...
    }
}

edit

编辑

Ahh, I see what you're saying...

啊,我明白你在说什么......

Well, the repository really should be blind to anything outside of itself. That is, it's not container-aware, and should therefore not know who the current user is.

那么,存储库真的应该对自身以外的任何东西视而不见。也就是说,它不是容器感知的,因此不应该知道当前用户是谁。

My suggestion would be to give your repository a function that gets everything except for a particular User or User ID. Something like:

我的建议是为您的存储库提供一个除了特定用户或用户ID之外的所有功能。就像是:

class AdminUserRepository extends EntityRepository implements UserProviderInterface
{
    public function getAllAdminsExcept($userId, $int = 10, $offset = 0, array $orderBy = array('admin_id', 'asc'))
        {
            ...
        }
}

And then put the "current User" logic into your controller. You could even define a service that has access to both @security and @doctrine, and house all the logic there. Your call, but I'd say that you should definitely keep your repository unaware of anything going on in the security service.

然后将“当前用户”逻辑放入您的控制器。您甚至可以定义一个可以同时访问@security和@doctrine的服务,并将所有逻辑存放在那里。您的电话,但我要说您应该确保您的存储库不知道安全服务中发生的任何事情。

#2


-3  

You can retrieve user object by

您可以通过检索用户对象

public function indexAction()
{
    $user = $this->getUser();
}

trough controller or view by Twig Template:

槽控制器或视图由Twig模板:

<p>Username: {{ app.user.username }}</p>

this sample come from symfony 2 doc.

这个样本来自symfony 2 doc。

#1


1  

So, you have a current User ID (let's say, $currentUserId), and you want to write a query for all Users with an ID that is not equal to $currentUserId?

那么,你有一个当前的用户名(比方说,$ currentUserId),你想为ID不等于$ currentUserId的所有用户写一个查询?

Try this:

尝试这个:

class SomeController
{
    public function someAction()
    {
        ...

        $qb = $userRepository->createQueryBuilder("user")
            ->where("user.id != ?1")
            ->setParameter(1, $currentUserId);

        $usersExcludingCurrent = $qb->getQuery()->getResult();

    ...
    }
}

edit

编辑

Ahh, I see what you're saying...

啊,我明白你在说什么......

Well, the repository really should be blind to anything outside of itself. That is, it's not container-aware, and should therefore not know who the current user is.

那么,存储库真的应该对自身以外的任何东西视而不见。也就是说,它不是容器感知的,因此不应该知道当前用户是谁。

My suggestion would be to give your repository a function that gets everything except for a particular User or User ID. Something like:

我的建议是为您的存储库提供一个除了特定用户或用户ID之外的所有功能。就像是:

class AdminUserRepository extends EntityRepository implements UserProviderInterface
{
    public function getAllAdminsExcept($userId, $int = 10, $offset = 0, array $orderBy = array('admin_id', 'asc'))
        {
            ...
        }
}

And then put the "current User" logic into your controller. You could even define a service that has access to both @security and @doctrine, and house all the logic there. Your call, but I'd say that you should definitely keep your repository unaware of anything going on in the security service.

然后将“当前用户”逻辑放入您的控制器。您甚至可以定义一个可以同时访问@security和@doctrine的服务,并将所有逻辑存放在那里。您的电话,但我要说您应该确保您的存储库不知道安全服务中发生的任何事情。

#2


-3  

You can retrieve user object by

您可以通过检索用户对象

public function indexAction()
{
    $user = $this->getUser();
}

trough controller or view by Twig Template:

槽控制器或视图由Twig模板:

<p>Username: {{ app.user.username }}</p>

this sample come from symfony 2 doc.

这个样本来自symfony 2 doc。