/** * {@inheritdoc} */ public function getUser() { if ($this->user) { return $this->user; } $user = $this->userRepository->findOneByUsername('test'); if (!$user) { $contact = $this->contactRepository->createNew(); $contact->setFirstName('Max'); $contact->setLastName('Mustermann'); $this->entityManager->persist($contact); $user = $this->userRepository->createNew(); $this->setCredentials($user); $user->setSalt(''); $user->setLocale('en'); $user->setContact($contact); $this->entityManager->persist($user); } else { $this->setCredentials($user); } $this->entityManager->flush(); $this->user = $user; return $this->user; }
/** * Creates a new user with the given data. * * @param array $data * @param string $locale * @param null|int $id * @param bool $patch * @param bool $flush * * @return null|UserInterface * * @throws \Exception */ public function save($data, $locale, $id = null, $patch = false, $flush = true) { $username = $this->getProperty($data, 'username'); $contact = $this->getProperty($data, 'contact'); $email = $this->getProperty($data, 'email'); $password = $this->getProperty($data, 'password'); $enabled = $this->getProperty($data, 'enabled'); $locked = $this->getProperty($data, 'locked'); $user = null; try { if ($id) { // update user $user = $this->userRepository->findUserById($id); if (!$user) { throw new EntityNotFoundException($this->userRepository->getClassName(), $id); } $this->processEmail($user, $email); } else { // add user if (!$this->isValidPassword($password)) { throw new MissingPasswordException(); } /** @var UserInterface $user */ $user = $this->userRepository->createNew(); $this->processEmail($user, $email, $contact); } // check if username is already in database and the current user is not the user with this username if (!$patch || $username !== null) { if ($user->getUsername() != $username && !$this->isUsernameUnique($username)) { throw new UsernameNotUniqueException($username); } $user->setUsername($username); } // check if password is valid if (!$patch || $password !== null) { if ($this->isValidPassword($password)) { $user->setSalt($this->generateSalt()); $user->setPassword($this->encodePassword($user, $password, $user->getSalt())); } } if (!$patch || $this->getProperty($data, 'userRoles') !== null) { if (!$this->processUserRoles($user, $this->getProperty($data, 'userRoles', []))) { throw new \Exception('Could not update dependencies!'); } } if (!$patch || $this->getProperty($data, 'userGroups') !== null) { if (!$this->processUserGroups($user, $this->getProperty($data, 'userGroups', []))) { throw new \Exception('Could not update dependencies!'); } } if (!$patch || $contact !== null) { $user->setContact($this->getContact($contact['id'])); } if (!$patch || $locale !== null) { $user->setLocale($locale); } if ($enabled !== null) { $user->setEnabled($enabled); } if ($locked !== null) { $user->setLocked($locked); } } catch (\Exception $re) { if (isset($user)) { $this->em->remove($user); } throw $re; } $this->em->persist($user); if ($flush) { $this->em->flush(); } return $user; }