|
|
@ -1,4 +1,5 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
namespace core\entities\user; |
|
|
|
namespace core\entities\user; |
|
|
|
|
|
|
|
|
|
|
|
use core\entities\Session; |
|
|
|
use core\entities\Session; |
|
|
@ -43,83 +44,84 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
|
|
|
|
|
|
|
|
public static function create(string $username, string $email, string $password): self |
|
|
|
public static function create(string $username, string $email, string $password): self |
|
|
|
{ |
|
|
|
{ |
|
|
|
$user = new User(); |
|
|
|
$user = new User(); |
|
|
|
$user->username = $username; |
|
|
|
$user->username = $username; |
|
|
|
$user->email = $email; |
|
|
|
$user->email = $email; |
|
|
|
$user->setPassword(!empty($password) ? $password : Yii::$app->security->generateRandomString()); |
|
|
|
$user->setPassword(!empty($password) ? $password : Yii::$app->security->generateRandomString()); |
|
|
|
$user->created_at = time(); |
|
|
|
$user->created_at = time(); |
|
|
|
$user->status = self::STATUS_ACTIVE; |
|
|
|
$user->status = self::STATUS_ACTIVE; |
|
|
|
$user->auth_key = Yii::$app->security->generateRandomString(); |
|
|
|
$user->auth_key = Yii::$app->security->generateRandomString(); |
|
|
|
|
|
|
|
|
|
|
|
if ($user->user_pic) |
|
|
|
if ($user->user_pic) { |
|
|
|
{ |
|
|
|
$fileName = md5('avatar-' . $user->user_pic->baseName . time()) . '.' . $user->user_pic->extension; |
|
|
|
$fileName = md5('avatar-' . $user->user_pic->baseName . time()) . '.' . $user->user_pic->extension; |
|
|
|
if ($user->user_pic->saveAs((new AvatarGenerator())->getPath('avatar') . '/' . $fileName)) { |
|
|
|
if ($user->user_pic->saveAs((new AvatarGenerator())->getPath('avatar') . '/' . $fileName)) |
|
|
|
$user->user_pic = $fileName; |
|
|
|
{ |
|
|
|
} |
|
|
|
$user->user_pic = $fileName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $user; |
|
|
|
return $user; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function edit(string $username, string $email, string $password, $user_pic = null): void |
|
|
|
public function edit(string $username, string $email, string $password, $user_pic = null): void |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->username = $username; |
|
|
|
$this->username = $username; |
|
|
|
$this->email = $email; |
|
|
|
$this->email = $email; |
|
|
|
$this->updated_at = time(); |
|
|
|
$this->updated_at = time(); |
|
|
|
|
|
|
|
|
|
|
|
if ($user_pic) |
|
|
|
if ($user_pic) { |
|
|
|
{ |
|
|
|
$fileName = md5('avatar-' . $user_pic->baseName . time()) . '.' . $user_pic->extension; |
|
|
|
$fileName = md5('avatar-' . $user_pic->baseName . time()) . '.' . $user_pic->extension; |
|
|
|
if ($user_pic->saveAs((new AvatarGenerator())->getPath('avatar') . '/' . $fileName)) { |
|
|
|
if ($user_pic->saveAs((new AvatarGenerator())->getPath('avatar') . '/' . $fileName)) |
|
|
|
(new AvatarGenerator())->remove('avatar', $this->user_pic); |
|
|
|
{ |
|
|
|
$this->user_pic = $fileName; |
|
|
|
(new AvatarGenerator())->remove('avatar', $this->user_pic); |
|
|
|
} |
|
|
|
$this->user_pic = $fileName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
if ($password) { |
|
|
|
|
|
|
|
$this->setPassword(!empty($password) ? $password : Yii::$app->security->generateRandomString()); |
|
|
|
if ($password) { |
|
|
|
$this->generateAuthKey(); |
|
|
|
$this->setPassword(!empty($password) ? $password : Yii::$app->security->generateRandomString()); |
|
|
|
Session::deleteAll(['user_id' => $this->id]); |
|
|
|
$this->generateAuthKey(); |
|
|
|
} |
|
|
|
Session::deleteAll(['user_id' => $this->id]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function editProfile(string $email, string $username, string $password = null, $user_pic = null, $language): void |
|
|
|
public function editProfile( |
|
|
|
{ |
|
|
|
string $email, |
|
|
|
$this->email = $email; |
|
|
|
string $username, |
|
|
|
|
|
|
|
string $password = null, |
|
|
|
|
|
|
|
$user_pic = null, |
|
|
|
|
|
|
|
$language |
|
|
|
|
|
|
|
): void { |
|
|
|
|
|
|
|
$this->email = $email; |
|
|
|
$this->username = $username; |
|
|
|
$this->username = $username; |
|
|
|
if ($password && !empty($password)) { |
|
|
|
if ($password && !empty($password)) { |
|
|
|
$this->setPassword( $password ); |
|
|
|
$this->setPassword($password); |
|
|
|
} |
|
|
|
} |
|
|
|
$this->updated_at = time(); |
|
|
|
$this->updated_at = time(); |
|
|
|
$this->backend_language = $language; |
|
|
|
$this->backend_language = $language; |
|
|
|
|
|
|
|
|
|
|
|
/* @var $user_pic \yii\web\UploadedFile */ |
|
|
|
/* @var $user_pic \yii\web\UploadedFile */ |
|
|
|
if ($user_pic) |
|
|
|
if ($user_pic) { |
|
|
|
{ |
|
|
|
$fileName = md5('avatar-' . $user_pic->baseName . time()) . '.' . $user_pic->extension; |
|
|
|
$fileName = md5('avatar-' . $user_pic->baseName . time()) . '.' . $user_pic->extension; |
|
|
|
$path = Yii::getAlias('@runtime/' . $fileName); |
|
|
|
$path = Yii::getAlias( '@runtime/' . $fileName); |
|
|
|
$user_pic->saveAs($path); |
|
|
|
$user_pic->saveAs($path); |
|
|
|
$this->user_pic = basename(Yii::$app->avatar->update($username, null, $path)); |
|
|
|
$this->user_pic = basename(Yii::$app->avatar->update($username, null, $path)); |
|
|
|
if (file_exists($path)) { |
|
|
|
if (file_exists($path)) { |
|
|
|
unlink($path); |
|
|
|
unlink($path); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static function requestSignup(string $username, string $email, string $password): self |
|
|
|
public static function requestSignup(string $username, string $email, string $password): self |
|
|
|
{ |
|
|
|
{ |
|
|
|
$user = new User(); |
|
|
|
$user = new User(); |
|
|
|
$user->username = $username; |
|
|
|
$user->username = $username; |
|
|
|
$user->email = $email; |
|
|
|
$user->email = $email; |
|
|
|
$user->setPassword($password); |
|
|
|
$user->setPassword($password); |
|
|
|
$user->created_at = time(); |
|
|
|
$user->created_at = time(); |
|
|
|
$user->status = self::STATUS_WAIT; |
|
|
|
$user->status = self::STATUS_WAIT; |
|
|
|
$user->email_confirm_token = Yii::$app->security->generateRandomString(); |
|
|
|
$user->email_confirm_token = Yii::$app->security->generateRandomString(); |
|
|
|
$user->generateAuthKey(); |
|
|
|
$user->generateAuthKey(); |
|
|
|
$user->recordEvent(new UserSignUpRequested($user)); |
|
|
|
$user->recordEvent(new UserSignUpRequested($user)); |
|
|
|
|
|
|
|
|
|
|
|
return $user; |
|
|
|
return $user; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -128,18 +130,19 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
if (!$this->isWait()) { |
|
|
|
if (!$this->isWait()) { |
|
|
|
throw new \DomainException('User is already active.'); |
|
|
|
throw new \DomainException('User is already active.'); |
|
|
|
} |
|
|
|
} |
|
|
|
$this->status = self::STATUS_ACTIVE; |
|
|
|
$this->status = self::STATUS_ACTIVE; |
|
|
|
$this->email_confirm_token = null; |
|
|
|
$this->email_confirm_token = null; |
|
|
|
$this->recordEvent(new UserSignUpConfirmed($this)); |
|
|
|
$this->recordEvent(new UserSignUpConfirmed($this)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static function signupByNetwork($network, $identity): self |
|
|
|
public static function signupByNetwork($network, $identity): self |
|
|
|
{ |
|
|
|
{ |
|
|
|
$user = new User(); |
|
|
|
$user = new User(); |
|
|
|
$user->created_at = time(); |
|
|
|
$user->created_at = time(); |
|
|
|
$user->status = self::STATUS_ACTIVE; |
|
|
|
$user->status = self::STATUS_ACTIVE; |
|
|
|
$user->generateAuthKey(); |
|
|
|
$user->generateAuthKey(); |
|
|
|
$user->networks = [Network::create($network, $identity)]; |
|
|
|
$user->networks = [Network::create($network, $identity)]; |
|
|
|
|
|
|
|
|
|
|
|
return $user; |
|
|
|
return $user; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -151,7 +154,7 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
throw new \DomainException('Network is already attached.'); |
|
|
|
throw new \DomainException('Network is already attached.'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
$networks[] = Network::create($network, $identity); |
|
|
|
$networks[] = Network::create($network, $identity); |
|
|
|
$this->networks = $networks; |
|
|
|
$this->networks = $networks; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -204,7 +207,7 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
return [ |
|
|
|
return [ |
|
|
|
TimestampBehavior::class, |
|
|
|
TimestampBehavior::class, |
|
|
|
[ |
|
|
|
[ |
|
|
|
'class' => SaveRelationsBehavior::class, |
|
|
|
'class' => SaveRelationsBehavior::class, |
|
|
|
'relations' => ['networks'], |
|
|
|
'relations' => ['networks'], |
|
|
|
], |
|
|
|
], |
|
|
|
]; |
|
|
|
]; |
|
|
@ -221,6 +224,7 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
* Finds user by username |
|
|
|
* Finds user by username |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $username |
|
|
|
* @param string $username |
|
|
|
|
|
|
|
* |
|
|
|
* @return static|null |
|
|
|
* @return static|null |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function findByUsername($username) |
|
|
|
public static function findByUsername($username) |
|
|
@ -232,6 +236,7 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
* Finds user by password reset token |
|
|
|
* Finds user by password reset token |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $token password reset token |
|
|
|
* @param string $token password reset token |
|
|
|
|
|
|
|
* |
|
|
|
* @return static|null |
|
|
|
* @return static|null |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function findByPasswordResetToken($token) |
|
|
|
public static function findByPasswordResetToken($token) |
|
|
@ -242,7 +247,7 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
|
|
|
|
|
|
|
|
return static::findOne([ |
|
|
|
return static::findOne([ |
|
|
|
'password_reset_token' => $token, |
|
|
|
'password_reset_token' => $token, |
|
|
|
'status' => self::STATUS_ACTIVE, |
|
|
|
'status' => self::STATUS_ACTIVE, |
|
|
|
]); |
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -250,6 +255,7 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
* Finds out if password reset token is valid |
|
|
|
* Finds out if password reset token is valid |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $token password reset token |
|
|
|
* @param string $token password reset token |
|
|
|
|
|
|
|
* |
|
|
|
* @return bool |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function isPasswordResetTokenValid($token) |
|
|
|
public static function isPasswordResetTokenValid($token) |
|
|
@ -258,8 +264,9 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$timestamp = (int) substr($token, strrpos($token, '_') + 1); |
|
|
|
$timestamp = (int)substr($token, strrpos($token, '_') + 1); |
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
|
|
|
|
|
|
|
|
|
|
|
return $timestamp + $expire >= time(); |
|
|
|
return $timestamp + $expire >= time(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -267,6 +274,7 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
* Validates password |
|
|
|
* Validates password |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $password password to validate |
|
|
|
* @param string $password password to validate |
|
|
|
|
|
|
|
* |
|
|
|
* @return bool if password provided is valid for current user |
|
|
|
* @return bool if password provided is valid for current user |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function validatePassword($password) |
|
|
|
public function validatePassword($password) |
|
|
@ -274,11 +282,11 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash); |
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param $password |
|
|
|
* @param $password |
|
|
|
* |
|
|
|
* |
|
|
|
* @throws \yii\base\Exception |
|
|
|
* @throws \yii\base\Exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private function setPassword($password) |
|
|
|
private function setPassword($password) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password); |
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password); |
|
|
@ -292,14 +300,15 @@ class User extends ActiveRecord implements AggregateRoot |
|
|
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
|
|
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function attributeLabels() { |
|
|
|
public function attributeLabels() |
|
|
|
return [ |
|
|
|
{ |
|
|
|
'id' => Yii::t('user', 'ID'), |
|
|
|
return [ |
|
|
|
'username' => Yii::t('user', 'Username'), |
|
|
|
'id' => Yii::t('user', 'ID'), |
|
|
|
'email' => Yii::t('user', 'E-mail'), |
|
|
|
'username' => Yii::t('user', 'Username'), |
|
|
|
'status' => Yii::t('user', 'Status'), |
|
|
|
'email' => Yii::t('user', 'E-mail'), |
|
|
|
'created_at' => Yii::t('user', 'Created At'), |
|
|
|
'status' => Yii::t('user', 'Status'), |
|
|
|
'updated_at' => Yii::t('user', 'Updated At'), |
|
|
|
'created_at' => Yii::t('user', 'Created At'), |
|
|
|
]; |
|
|
|
'updated_at' => Yii::t('user', 'Updated At'), |
|
|
|
|
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|