123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- declare(strict_types=1);
- namespace OCA\Provisioning_API\Controller;
- use InvalidArgumentException;
- use OC\Security\Crypto;
- use OCP\Accounts\IAccountManager;
- use OCP\AppFramework\Controller;
- use OCP\AppFramework\Http\Attribute\IgnoreOpenAPI;
- use OCP\AppFramework\Http\TemplateResponse;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\Security\VerificationToken\InvalidTokenException;
- use OCP\Security\VerificationToken\IVerificationToken;
- class VerificationController extends Controller {
-
- private $verificationToken;
-
- private $userManager;
-
- private $l10n;
-
- private $userSession;
-
- private $accountManager;
-
- private $crypto;
- public function __construct(
- string $appName,
- IRequest $request,
- IVerificationToken $verificationToken,
- IUserManager $userManager,
- IL10N $l10n,
- IUserSession $userSession,
- IAccountManager $accountManager,
- Crypto $crypto
- ) {
- parent::__construct($appName, $request);
- $this->verificationToken = $verificationToken;
- $this->userManager = $userManager;
- $this->l10n = $l10n;
- $this->userSession = $userSession;
- $this->accountManager = $accountManager;
- $this->crypto = $crypto;
- }
-
- public function showVerifyMail(string $token, string $userId, string $key) {
- if ($this->userSession->getUser()->getUID() !== $userId) {
-
- throw new InvalidArgumentException('Logged in user is not mail address owner');
- }
- $email = $this->crypto->decrypt($key);
- return new TemplateResponse(
- 'core', 'confirmation', [
- 'title' => $this->l10n->t('Email confirmation'),
- 'message' => $this->l10n->t('To enable the email address %s please click the button below.', [$email]),
- 'action' => $this->l10n->t('Confirm'),
- ], TemplateResponse::RENDER_AS_GUEST);
- }
-
- public function verifyMail(string $token, string $userId, string $key) {
- try {
- if ($this->userSession->getUser()->getUID() !== $userId) {
- throw new InvalidArgumentException('Logged in user is not mail address owner');
- }
- $email = $this->crypto->decrypt($key);
- $ref = \substr(hash('sha256', $email), 0, 8);
- $user = $this->userManager->get($userId);
- $this->verificationToken->check($token, $user, 'verifyMail' . $ref, $email);
- $userAccount = $this->accountManager->getAccount($user);
- $emailProperty = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)
- ->getPropertyByValue($email);
- if ($emailProperty === null) {
- throw new InvalidArgumentException($this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'));
- }
- $emailProperty->setLocallyVerified(IAccountManager::VERIFIED);
- $this->accountManager->updateAccount($userAccount);
- $this->verificationToken->delete($token, $user, 'verifyMail' . $ref);
- } catch (InvalidTokenException $e) {
- $error = $e->getCode() === InvalidTokenException::TOKEN_EXPIRED
- ? $this->l10n->t('Could not verify mail because the token is expired.')
- : $this->l10n->t('Could not verify mail because the token is invalid.');
- } catch (InvalidArgumentException $e) {
- $error = $e->getMessage();
- } catch (\Exception $e) {
- $error = $this->l10n->t('An unexpected error occurred. Please contact your admin.');
- }
- if (isset($error)) {
- return new TemplateResponse(
- 'core', 'error', [
- 'errors' => [['error' => $error]]
- ], TemplateResponse::RENDER_AS_GUEST);
- }
- return new TemplateResponse(
- 'core', 'success', [
- 'title' => $this->l10n->t('Email confirmation successful'),
- 'message' => $this->l10n->t('Email confirmation successful'),
- ], TemplateResponse::RENDER_AS_GUEST);
- }
- }
|