Skip to content

Commit

Permalink
Invalidate active sessions after password changing (#5872)
Browse files Browse the repository at this point in the history
* Invalidate active sessions after password changing

* Update CHANGELOG-DEV.md (#5872)
  • Loading branch information
yurabakhtin committed Sep 23, 2022
1 parent 7f9d9f2 commit 1ed3881
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ HumHub Changelog
- Enh #5841: Possibility to show Members/Followers as list from Space about page
- Enh #5850: Display all levels by default on info logging page
- Enh #5864: Use Base URL from general settings for all generated absolute URLs

- Enh #5872: Invalidate active sessions after password changing
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use humhub\components\Migration;

/**
* Class m220919_104234_auth_key
*/
class m220919_104234_auth_key extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->safeAddColumn('user', 'auth_key', $this->string(32)->null());
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->safeDropColumn('user', 'auth_key');
}
}
14 changes: 14 additions & 0 deletions protected/humhub/modules/user/models/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @property string $password
* @property string $salt
* @property string $created_at
*
* @property-read User $user
*/
class Password extends ActiveRecord
{
Expand Down Expand Up @@ -185,6 +187,7 @@ public function setPassword($newPassword)
$this->salt = UUID::v4();
$this->algorithm = $this->defaultAlgorithm;
$this->password = $this->hashPassword($newPassword);
$this->user->auth_key = Yii::$app->security->generateRandomString(32);
}

public function getUser()
Expand Down Expand Up @@ -213,4 +216,15 @@ private function validateAdvancedPasswordRules($attribute, $params)
}
}

public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);

if ($this->user->isAttributeChanged('auth_key') &&
$this->user->save() &&
$this->user->isCurrentUser()) {
Yii::$app->user->switchIdentity($this->user);
}
}

}
3 changes: 2 additions & 1 deletion protected/humhub/modules/user/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* @property integer $updated_by
* @property string $last_login
* @property string $authclient_id
* @property string $auth_key
* @property integer $visibility
* @property integer $contentcontainer_id
* @property Profile $profile
Expand Down Expand Up @@ -347,7 +348,7 @@ public function getId()

public function getAuthKey()
{
return $this->guid;
return $this->auth_key ?: $this->guid;
}

public function validateAuthKey($authKey)
Expand Down

0 comments on commit 1ed3881

Please sign in to comment.