1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 15:37:51 +02:00

Improve email changing/confirmation stuff

This commit is contained in:
Toby Zerner
2015-05-27 16:24:54 +09:30
parent 8f0989fb80
commit 696bfe5a07
18 changed files with 262 additions and 94 deletions

View File

@@ -13,6 +13,7 @@ use Flarum\Core\Events\UserBioWasChanged;
use Flarum\Core\Events\UserAvatarWasChanged;
use Flarum\Core\Events\UserWasActivated;
use Flarum\Core\Events\UserEmailWasConfirmed;
use Flarum\Core\Events\UserEmailChangeWasRequested;
class User extends Model
{
@@ -94,8 +95,6 @@ class User extends Model
$user->password = $password;
$user->join_time = time();
$user->refreshConfirmationToken();
$user->raise(new UserWasRegistered($user));
return $user;
@@ -111,6 +110,7 @@ class User extends Model
{
if ($username !== $this->username) {
$this->username = $username;
$this->raise(new UserWasRenamed($this));
}
@@ -127,12 +127,31 @@ class User extends Model
{
if ($email !== $this->email) {
$this->email = $email;
$this->raise(new UserEmailWasChanged($this));
}
return $this;
}
public function requestEmailChange($email)
{
if ($email !== $this->email) {
$validator = static::$validator->make(
compact('email'),
$this->expandUniqueRules(array_only(static::$rules, 'email'))
);
if ($validator->fails()) {
$this->throwValidationFailureException($validator);
}
$this->raise(new UserEmailChangeWasRequested($this, $email));
}
return $this;
}
/**
* Change the user's password.
*
@@ -257,41 +276,12 @@ class User extends Model
public function activate()
{
$this->is_activated = true;
$this->groups()->sync([3]);
$this->raise(new UserWasActivated($this));
return $this;
}
/**
* Check if a given confirmation token is valid for this user.
*
* @param string $token
* @return boolean
*/
public function assertConfirmationTokenValid($token)
{
if ($this->is_confirmed ||
! $token ||
$this->confirmation_token !== $token) {
throw new InvalidConfirmationTokenException;
}
}
/**
* Generate a new confirmation token for the user.
*
* @return $this
*/
public function refreshConfirmationToken()
{
$this->is_confirmed = false;
$this->confirmation_token = str_random(30);
return $this;
}
/**
* Confirm the user's email.
*
@@ -461,7 +451,13 @@ class User extends Model
*/
public function permissions()
{
return Permission::whereIn('group_id', array_merge([Group::GUEST_ID], $this->groups->lists('id')));
$groupIds = [Group::GUEST_ID];
if ($this->is_activated) {
$groupIds = array_merge($groupIds, [Group::MEMBER_ID], $this->groups->lists('id'));
}
return Permission::whereIn('group_id', $groupIds);
}
/**