/** * Modifies the user locale. * * @param LocaleSwitcherDTO $dto */ public function __invoke(LocaleSwitcherDTO $dto) { $user = $dto->user; if ($dto->locale !== $user->getLocale()) { $user->modifyUserLocale($dto->locale); $this->userRepository->save($user); } }
/** * Activates the account. * * @param ActivateAccountDTO $activateAccountDTO */ public function __invoke(ActivateAccountDTO $activateAccountDTO) { if (!($user = $this->userReadRepository->findUserByUsernameAndActivationKey($activateAccountDTO->username, $activateAccountDTO->activationKey))) { throw new UserActivationException(); } if ($user->getPendingActivation()->isActivationExpired()) { $this->userWriteRepository->remove($user); throw new UserActivationException(); } $user->performStateTransition(User::STATE_APPROVED, $activateAccountDTO->activationKey); // the role needs to be added during approval since a non-approved user must not have any role in the system. // Furthermore it leads to technical issues when running the purger as the roles may cause a constraint violation // in the RDBMS. Therefore it's safer to add roles during the approval. $user->addRole($this->roleRepository->determineDefaultRole()); $this->userWriteRepository->save($user); }
/** * Handles a the user creation. * * @param CreateUserDTO $userDTO * * @throws \OverflowException If the activation keycode generation failed. * @throws \LogicException */ public function __invoke(CreateUserDTO $userDTO) { $user = User::create($userDTO->username, $userDTO->password, $userDTO->email, $this->hasher); $user->modifyUserLocale($userDTO->locale); $rounds = 0; $isUnique = false; $activationKey = null; while (!$isUnique) { ++$rounds; if ($rounds >= 200) { throw new \OverflowException('Cannot generate activation key!'); } $activationKey = $this->keyGenerator->generate(255); $options = ['entity' => 'Account:User', 'field' => 'pendingActivation.key']; $isUnique = count($this->validator->validate($activationKey, new UniqueProperty($options))) === 0; } $user->storeUniqueActivationKeyForNonApprovedUser($activationKey); $this->userRepository->save($user); $this->notify(['activation_key' => $user->getPendingActivation()->getKey(), 'username' => $user->getUsername()], [$user], ['mail'], $user->getLocale()); $userDTO->user = $user; }