em = $ems; $this->class = $class; } /** * Loads the user for the given username. * * This method must throw UsernameNotFoundException if the user is not * found. * * @param string $username The username * * @return UserInterface * * @throws UsernameNotFoundException if the user is not found */ public function loadUserByUsername($username): UserInterface { //todo change find by email /** * @var CustomUserInterface $user */ $user = $this-> findOneByUsernameOrEmail($username); if (!$user) { throw new UsernameNotFoundException(sprintf('User "%s" does not exist.', $username)); } if(!$user->isEnabled()){ //throw new UsernameNotFoundException(sprintf('User "%s" does exist but isn\'t activated.', $username)); throw new CustomUserMessageAuthenticationException(sprintf('User "%s" does exist but isn\'t activated.', $username)); } return $user; } /** * Refreshes the user for the account interface. * * It is up to the implementation to decide if the user data should be * totally reloaded (e.g. from the database), or if the UserInterface * object can just be merged into some internal array of users / identity * map. * * @param UserInterface $user * * @return UserInterface * * @throws UnsupportedUserException if the account is not supported */ public function refreshUser(UserInterface $user) { $impl = class_implements($user); if (\in_array('CustomUserInterface',$impl, true)) { throw new UnsupportedUserException( sprintf('Instances of "%s" are not supported.', \get_class($user)) ); } return $this->loadUserByUsername($user->getUsername()); } /** * Whether this provider supports the given user class. * * @param string $class * * @return bool */ public function supportsClass($class): bool { $impl = class_implements(new $class); if (\in_array('CustomUserInterface',$impl,true)) { return true; } return false; } /** * @return string */ public function getClass(): string { if (false !== strpos($this->class, ':')) { $metadata = $this->em->getClassMetadata($this->class); $this->class = $metadata->getName(); } return $this->class; } /** * @param $email * * @return \App\Entity\User\User|null|object */ public function findOneByEmail($email) { return $this->em->getRepository('User:User')->findOneBy(array('email' => $email)); } /** * @param $username * * @return \App\Entity\User\User|null|object */ public function findOneByUsername($username) { return $this->em->getRepository('User:User')->findOneBy(array('username' => $username)); } /** * @param $usernameOrEmail * * @return \App\Entity\User\User|null|object */ public function findOneByUsernameOrEmail($usernameOrEmail) { if ($this->isValidEmail($usernameOrEmail)) { return $this->findOneByEmail($usernameOrEmail); } return $this->findOneByUsername($usernameOrEmail); } /** * @param $email * * @return false|int */ protected function isValidEmail($email) { return preg_match('/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i', $email); } }