/** * @param string $email * @param string $password * @param string $roleStringIdentifier * @return User * @throws RoleDoesNotExistException * @throws UserAlreadyExistsException */ public function register(string $email, string $password, string $roleStringIdentifier) { $entityManager = $this->registry->getManager(); $email = UserTools::sanitizeUserEmail($email); $this->validateUserDoesNotExists($email); $passwordHash = Passwords::hash($password); $user = new User($email, $passwordHash); $entityManager->persist($user); $this->roleToUserAssigner->assignByString($roleStringIdentifier, $user); $entityManager->flush($user); return $user; }
/** * @inheritdoc */ public function authenticate(array $credentials) { list($email, $password) = $credentials; $email = UserTools::sanitizeUserEmail($email); /** @var User|null $user */ $user = $this->registry->getRepository(User::class)->findOneBy(['email' => $email]); if ($user === null) { throw new AuthenticationException("User '{$email}' not found."); } if (!Passwords::verify($password, $user->getPassword())) { throw new AuthenticationException('Invalid password.'); } return new Identity($user->getId(), $user->getUserRoles(), ['username' => $user->getEmail()]); }