util = $util; $this->user = $user; $this->entityManager = $entityManager; $this->config = $config; } /** * @throws Forbidden * @throws NotFound * @throws Error */ public function sendCode(string $userId, string $emailAddress): void { if (!$this->user->isAdmin() && $userId !== $this->user->getId()) { throw new Forbidden(); } $this->checkAllowed(); /** @var ?User $user */ $user = $this->entityManager->getEntity(User::ENTITY_TYPE, $userId); if (!$user) { throw new NotFound(); } $this->util->sendCode($user, $emailAddress); $this->util->storeEmailAddress($user, $emailAddress); } /** * @throws Forbidden */ private function checkAllowed(): void { if (!$this->config->get('auth2FA')) { throw new Forbidden("2FA is not enabled."); } $methodList = $this->config->get('auth2FAMethodList') ?? []; if (!in_array('Email', $methodList)) { throw new Forbidden("Email 2FA is not allowed."); } } }