symfony用户查询返回字段我没有在登录时指定

时间:2022-10-16 12:34:39

In my symfony app, using fosuserbundle too, this is a DQL query I create to recover all data I need concerning user:

在我的symfony应用程序中,也使用fosuserbundle,这是我创建的DQL查询,用于恢复有关用户的所有数据:

public function getAlluserNeedlesInfo()
{
    return
      $this->getEntityManager()
           ->createQuery(
              'SELECT partial u.{id, username, email, roles, enabled, createdAt, updatedAt}, partial c.{id, companySiret, companyName, professional, firstName, lastName, slug, createdAt, updatedAt}
               FROM AppBundle:User u
               JOIN u.consumer c
               ORDER BY u.id ASC'
           )
          ->getResult();
}

This is my controller method code:

这是我的控制器方法代码:

public function usersAction()
{
    $em = $this->getDoctrine()->getManager();
    $users = $em->getRepository('AppBundle:User')->getAlluserNeedlesInfo();
    $serializedEntity = $this->container->get('serializer')->serialize($users, 'json');
    return new Response($serializedEntity);
}

For now all works well, this is the results in json format returning by my controller method:

现在一切正常,这是我的控制器方法返回的json格式的结果:

[{
    "id": 1,
    "username": "test",
    "email": "test@test.com",
    "enabled": true,
    "roles": [],
    "consumer": {
        "id": 1,
        "professional": false,
        "last_name": "TEST",
        "first_name": "test",
        "slug": "test-test",
        "created_at": "2016-10-07T03:22:32+0200",
        "updated_at": "2016-10-07T17:42:17+0200"
    },
    "created_at": "2016-10-02T07:28:28+0200",
    "updated_at": "2016-10-09T01:05:04+0200"
}, {
    "id": 2,
    "username": "admin",
    "email": "admin@test.com",
    "enabled": true,
    "roles": ["ROLE_ADMIN"],
    "consumer": {
        "id": 2,
        "company_name": "admin",
        "company_siret": "01234567890006",
        "professional": true,
        "last_name": "ADMIN",
        "first_name": "Admin",
        "slug": "admin-admin",
        "created_at": "2016-10-06T08:45:23+0200",
        "updated_at": "2016-10-07T07:59:34+0200"
    },
    "created_at": "2016-09-04T12:14:03+0200",
    "updated_at": "2016-10-09T03:03:00+0200"
}, {
    "id": 3,
    "username": "test2",
    "email": "test2@test.com",
    "enabled": false,
    "roles": [],
    "consumer": {
        "id": 3,
        "company_name": "test2",
        "company_siret": "02896452300006",
        "professional": true,
        "last_name": "TEST2",
        "first_name": "test2",
        "slug": "test2-test2",
        "created_at": "2016-10-07T03:22:32+0200",
        "updated_at": "2016-10-07T17:42:17+0200"
    },
    "created_at": "2016-10-07T17:28:08+0200",
    "updated_at": "2016-10-07T17:28:08+0200"
}, {
    "id": 4,
    "username": "ju",
    "email": "j.val@gmail.com",
    "enabled": true,
    "roles": [],
    "consumer": {
        "id": 4,
        "professional": false,
        "last_name": "Val",
        "first_name": "ju",
        "slug": "val-ju",
        "created_at": "2016-10-07T18:07:06+0200",
        "updated_at": "2016-10-07T18:07:06+0200"
    },
    "created_at": "2016-10-07T18:07:06+0200",
    "updated_at": "2016-10-07T18:07:06+0200"
}]

All works well, but I notice that If I am logged in with one of these users (for example Val-Ju), the query returns me some other credentials and data I don't specify in, for example:

一切正常,但我注意到如果我使用其中一个用户登录(例如Val-Ju),查询会返回一些我未指定的其他凭据和数据,例如:

[{
    "id": 1,
    "username": "test",
    "email": "test@test.com",
    "enabled": true,
    "roles": [],
    "consumer": {
        "id": 1,
        "professional": false,
        "last_name": "TEST",
        "first_name": "test",
        "slug": "test-test",
        "created_at": "2016-10-07T03:22:32+0200",
        "updated_at": "2016-10-07T17:42:17+0200"
    },
    "created_at": "2016-10-02T07:28:28+0200",
    "updated_at": "2016-10-09T01:05:04+0200"
}, {
    "id": 2,
    "username": "admin",
    "email": "admin@test.com",
    "enabled": true,
    "roles": ["ROLE_ADMIN"],
    "consumer": {
        "id": 2,
        "company_name": "admin",
        "company_siret": "01234567890006",
        "professional": true,
        "last_name": "ADMIN",
        "first_name": "Admin",
        "slug": "admin-admin",
        "created_at": "2016-10-06T08:45:23+0200",
        "updated_at": "2016-10-07T07:59:34+0200"
    },
    "created_at": "2016-09-04T12:14:03+0200",
    "updated_at": "2016-10-09T03:03:00+0200"
}, {
    "id": 3,
    "username": "test2",
    "email": "test2@test.com",
    "enabled": false,
    "roles": [],
    "consumer": {
        "id": 3,
        "company_name": "test2",
        "company_siret": "02896452300006",
        "professional": true,
        "last_name": "TEST2",
        "first_name": "test2",
        "slug": "test2-test2",
        "created_at": "2016-10-07T03:22:32+0200",
        "updated_at": "2016-10-07T17:42:17+0200"
    },
    "created_at": "2016-10-07T17:28:08+0200",
    "updated_at": "2016-10-07T17:28:08+0200"
}, {
    "id": 4,
    "username": "ju",
    "username_canonical": "ju",
    "email": "j.val@gmail.com",
    "email_canonical": "j.val@gmail.com",
    "enabled": true,
    "salt": "5ng0rXXXXXXXXXXccwowscs4s",
    "password": "$2yXXXXXXXXXXXXXXXXXXXXXXXXXX1HgIHe42rz\/agpB.0fC",
    "last_login": "2016-10-07T18:15:32+0200",
    "locked": false,
    "expired": false,
    "roles": [],
    "credentials_expired": false,
    "consumer": {
        "id": 4,
        "professional": false,
        "last_name": "Val",
        "first_name": "ju",
        "slug": "val-ju",
        "created_at": "2016-10-07T18:07:06+0200",
        "updated_at": "2016-10-07T18:07:06+0200"
    },
    "created_at": "2016-10-07T18:07:06+0200",
    "updated_at": "2016-10-07T18:15:32+0200"
}]

Of course, this is a thing I don't want, and it's a non-desired behavior.

当然,这是我不想要的,这是一种不希望的行为。

Why this behavior ocurred ?

为什么会出现这种行为?

1 个解决方案

#1


1  

It's the default serialization configuration for FOSUserBundle, if you're logged in, it will serialize all your stuff.

这是FOSUserBundle的默认序列化配置,如果您已登录,它将序列化您的所有内容。

What you need is to override this configuration. It can be done by adding

您需要的是覆盖此配置。它可以通过添加来完成

# src/MyBundle/Resources/config/serialization/Model.User.yml
FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    properties:
        username:
            expose: true
            groups: ["aGroupIfNeeded"]
        #...

If you want more informations about serialization groups.

如果您想了解有关序列化组的更多信息。

#1


1  

It's the default serialization configuration for FOSUserBundle, if you're logged in, it will serialize all your stuff.

这是FOSUserBundle的默认序列化配置,如果您已登录,它将序列化您的所有内容。

What you need is to override this configuration. It can be done by adding

您需要的是覆盖此配置。它可以通过添加来完成

# src/MyBundle/Resources/config/serialization/Model.User.yml
FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    properties:
        username:
            expose: true
            groups: ["aGroupIfNeeded"]
        #...

If you want more informations about serialization groups.

如果您想了解有关序列化组的更多信息。