src/Repository/UserRepository.php line 68

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  4.  *
  5.  * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU Affero General Public License as published
  9.  * by the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU Affero General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Affero General Public License
  18.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  19.  */
  20. declare(strict_types=1);
  21. /**
  22.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  23.  *
  24.  * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
  25.  *
  26.  * This program is free software; you can redistribute it and/or
  27.  * modify it under the terms of the GNU General Public License
  28.  * as published by the Free Software Foundation; either version 2
  29.  * of the License, or (at your option) any later version.
  30.  *
  31.  * This program is distributed in the hope that it will be useful,
  32.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  * GNU General Public License for more details.
  35.  *
  36.  * You should have received a copy of the GNU General Public License
  37.  * along with this program; if not, write to the Free Software
  38.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  39.  */
  40. namespace App\Repository;
  41. use App\Entity\UserSystem\User;
  42. use Doctrine\ORM\NonUniqueResultException;
  43. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  44. use Symfony\Component\Security\Core\User\UserInterface;
  45. /**
  46.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  47.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  48.  * @method User[]    findAll()
  49.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  50.  */
  51. final class UserRepository extends NamedDBElementRepository implements PasswordUpgraderInterface
  52. {
  53.     protected $anonymous_user;
  54.     /**
  55.      * Returns the anonymous user.
  56.      * The result is cached, so the database is only called once, after the anonymous user was found.
  57.      */
  58.     public function getAnonymousUser(): ?User
  59.     {
  60.         if (null === $this->anonymous_user) {
  61.             $this->anonymous_user $this->findOneBy([
  62.                 'id' => User::ID_ANONYMOUS,
  63.             ]);
  64.         }
  65.         return $this->anonymous_user;
  66.     }
  67.     /**
  68.      * Find a user by its name or its email. Useful for login or password reset purposes.
  69.      *
  70.      * @param string $name_or_password The username or the email of the user that should be found
  71.      *
  72.      * @return User|null The user if it is existing, null if no one matched the criteria
  73.      */
  74.     public function findByEmailOrName(string $name_or_password): ?User
  75.     {
  76.         if (empty($name_or_password)) {
  77.             return null;
  78.         }
  79.         $qb $this->createQueryBuilder('u');
  80.         $qb->select('u')
  81.             ->where('u.name = (:name)')
  82.             ->orWhere('u.email = (:email)');
  83.         $qb->setParameters([
  84.             'email' => $name_or_password,
  85.             'name' => $name_or_password,
  86.         ]);
  87.         try {
  88.             return $qb->getQuery()->getOneOrNullResult();
  89.         } catch (NonUniqueResultException $nonUniqueResultException) {
  90.             return null;
  91.         }
  92.     }
  93.     public function upgradePassword(UserInterface $userstring $newEncodedPassword): void
  94.     {
  95.         if ($user instanceof User) {
  96.             $user->setPassword($newEncodedPassword);
  97.             $this->getEntityManager()->flush($user);
  98.         }
  99.     }
  100. }