szurubooru/src/Api/Jobs/EditUserEmailJob.php

63 lines
1.5 KiB
PHP
Raw Normal View History

2014-05-04 13:39:00 +02:00
<?php
class EditUserEmailJob extends AbstractUserJob
2014-05-04 13:39:00 +02:00
{
const NEW_EMAIL = 'new-email';
public function isSatisfied()
{
return $this->hasArgument(self::NEW_EMAIL);
}
2014-05-04 13:39:00 +02:00
public function execute()
{
2014-05-04 14:57:44 +02:00
if (getConfig()->registration->needEmailForRegistering)
2014-05-07 00:34:02 +02:00
if (!$this->hasArgument(self::NEW_EMAIL) or empty($this->getArgument(self::NEW_EMAIL)))
2014-05-04 14:57:44 +02:00
throw new SimpleException('E-mail address is required - you will be sent confirmation e-mail.');
2014-05-04 13:39:00 +02:00
$user = $this->user;
$newEmail = UserModel::validateEmail($this->getArgument(self::NEW_EMAIL));
2014-05-07 09:26:04 +02:00
$oldEmail = $user->getConfirmedEmail();
2014-05-04 13:39:00 +02:00
if ($oldEmail == $newEmail)
return $user;
2014-05-07 09:26:04 +02:00
$user->setUnconfirmedEmail($newEmail);
$user->setConfirmedEmail(null);
2014-05-04 13:39:00 +02:00
if ($this->getContext() == self::CONTEXT_NORMAL)
2014-05-07 09:26:04 +02:00
{
UserModel::save($user);
2014-05-07 09:26:04 +02:00
self::observeSave($user);
}
2014-05-04 13:39:00 +02:00
2014-05-04 19:23:09 +02:00
Logger::log('{user} changed {subject}\'s e-mail to {mail}', [
2014-05-04 13:39:00 +02:00
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'subject' => TextHelper::reprUser($user),
'mail' => $newEmail]);
return $user;
}
2014-05-07 09:26:04 +02:00
public static function observeSave($user)
{
if (Access::check(new Privilege(Privilege::ChangeUserEmailNoConfirm), $user))
{
$user->confirmEmail();
}
else
{
if (!empty($user->getUnconfirmedEmail()))
ActivateUserEmailJob::sendEmail($user);
}
}
2014-05-04 13:39:00 +02:00
public function requiresPrivilege()
{
2014-05-04 16:27:15 +02:00
return new Privilege(
2014-05-07 00:34:02 +02:00
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::RegisterAccount
: Privilege::ChangeUserEmail,
2014-05-04 16:27:15 +02:00
Access::getIdentity($this->user));
2014-05-04 13:39:00 +02:00
}
}