This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit

eZ Community » Forums » eZ Publish 5 Platform » Fetch a user by email

Fetch a user by email

Fetch a user by email

Tuesday 11 June 2013 9:27:19 pm - 5 replies


Simple question : what would be the best way to fetch user by email in a SF2 controller ? I need to check if someone already use a given email address.

Thank you very much

Tuesday 11 June 2013 11:00:19 pm

Hello Paul,

I don't find in the Symfony kernel, this functionnality sad.gif Emoticon !

But, if you are blocked by that, you can using legacy functions

$email = $data['inputEmail'];
$emailExist = $this->getLegacyKernel()->runCallback(
    function () use ( $email )
        return eZUser::fetchByEmail( $email );
if ( $emailExist instanceof eZUser ) 
    $errors['inputEmail'] = 'Login already exist';

I hope I could help you !

Modified on Tuesday 11 June 2013 11:06:23 pm by Franck Chaux

Wednesday 12 June 2013 8:49:25 am

Hello Franck, 

Thank you very much. I will try this right now.

Wednesday 12 June 2013 9:28:47 am

I am almost there, but the SF2 returns me an error : unknow function for getLegacyKernel().

Here is the full code of my controller :

Any idea ?

Wednesday 12 June 2013 9:44:59 am

Hi Paul,

I'd try to avoid using the legacy kernel in eZ 5 projects, if possible.

I made a symfony service a few days ago, that "fetches" a user via search criterion. I simply injected that service into the controller and had the possibility to check, whether a user exists or not.

To find a user, I coded following service:

class EzService
    const ADMIN_USER_OBJECT_ID = 14;
    /** @var \eZ\Publish\API\Repository\Repository $repository */
    protected $repository;
     * Constructor
     * @param Repository $repository
     * @param callable $legacyKernelClosure
    public function __construct(Repository $repository)
        $this->repository = $repository;
     * Returns the eZ API Repository with admin context
     * @return \eZ\Publish\API\Repository\Repository
    protected function getEzRepositoryAdminContext()
        /** @var \eZ\Publish\API\Repository\Repository adminRepository */
        $administratorUser = $this->repository->getUserService()->loadUser(self::ADMIN_USER_OBJECT_ID);
        return $this->repository;
     * Try to retrieve a user object by an attribute.
     * Default attribute is "login", but others like "email" are possible.
     * If no user object is found, null is returned
     * @param string $value
     * @param string $searchAttribute
     * @return \eZ\Publish\API\Repository\Values\User\User|null
    public function findUser($value, $searchAttribute = 'login')
        $repository = $this->getEzRepositoryAdminContext();
        $searchService = $repository->getSearchService();
        $userPathString = $locationService->loadLocation(self::PARENT_LOCATION_ID_USERS)->pathString;
        $userService = $repository->getUserService();
        // find user
        $query = new \eZ\Publish\API\Repository\Values\Content\Query();
        $query->criterion = new Criterion\LogicalAnd(
                new Criterion\Subtree($userPathString),
                new Criterion\Visibility(Criterion\Visibility::VISIBLE),
                new Criterion\ContentTypeIdentifier(array('user')),
                new Criterion\FullText($value),
        /** @var \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $searchResult */
        $searchResult = $searchService->findContent($query);
        if ($searchResult && count($searchResult->searchHits)) {
            foreach ($searchResult->searchHits as $searchHit) {
                /** @var \eZ\Publish\API\Repository\Values\Content\Content $valueObject */
                $valueObject = $searchHit->valueObject;
                /** @var \eZ\Publish\Core\FieldType\User\Value $fieldValue */
                $fieldValue = $valueObject->getFieldValue('user_account');
                if ($fieldValue->attribute($searchAttribute) == $value) {
                    return $userService->loadUser($valueObject->id);
        return null;

I guess, there is a better way, of finding users, but it works that way. To check, if a user is available, you could use following code:

$ezService =  $this->get('id_of_ez_service');
$user = $ezService->findUser('', 'email');
if ($user) {
  // user exists!
} else {
  // user does not exist!

Hope, that helps!

Best regards, Ryad happy.gif Emoticon

Modified on Wednesday 12 June 2013 9:49:38 am by R E

Wednesday 12 June 2013 10:46:25 am

The method $this->getLegacyKernel() mentioned by Franck Chaux should work, if your controller derives from the eZ controller (as mentioned in the eZ 5 documentation).

If you have your own controller inherits from Symfony controller, you have to inject or use "ezpublish_legacy.kernel" service which gives an callable closure.

You can see, how to use it for example in the definition of "ezpublish_legacy.fieldType.ezimage.variation_service" service in vendor/ezsystems/ezpublish-kernel/eZ/Bundle/EzPublishLegacyBundle/Resources/config/services.yml resp. vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/MVC/Legacy/Image/AliasGenerator.php.

Hope that helps.


You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from