Skip to content

Commit

Permalink
Fixed identity, add user options
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-wdmg committed Jun 20, 2023
1 parent fa02563 commit 15da715
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
=========

## 1.2.7 (2023-06-20)
* Fixed identity, add user options

## 1.2.6 (2023-06-18)
* Fixed getUserId()
* Fixed copyrights
Expand Down
6 changes: 3 additions & 3 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Yii2 Users
*
* @category Module
* @version 1.2.6
* @version 1.2.7
* @author Alexsander Vyshnyvetskyy <alex.vyshnyvetskyy@gmail.com>
* @link https://github.com/wdmg/yii2-users
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
Expand Down Expand Up @@ -47,7 +47,7 @@ class Module extends BaseModule
/**
* @var string the module version
*/
private $version = "1.2.6";
private $version = "1.2.7";

/**
* @var integer, priority of initialization
Expand Down Expand Up @@ -129,7 +129,7 @@ public function bootstrap($app)
if (!($app instanceof \yii\console\Application) && !$app->user->isGuest) {
$module = $this;
\yii\base\Event::on(\yii\web\Controller::class, \yii\web\Controller::EVENT_BEFORE_ACTION, function ($event) use ($app, $module) {
if (!$module->isRestAPI() && $app->user->identity) {
if ($app->user->identity) {
$lastseen_at = $app->user->identity->lastseen_at;
if (strtotime('-1 minutes', strtotime(date('Y-m-d H:i:s'))) > strtotime($lastseen_at)) {
$app->user->identity->updateAll(['lastseen_at' => date('Y-m-d H:i:s')], ['id' => $app->user->id]);
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Use the `Module::dashboardNavItems()` method of the module to generate a navigat
?>

# Status and version [ready to use]
* v.1.2.7 - Fixed identity, add user options
* v.1.2.6 - Fixed copyrights and getUserId()
* v.1.2.5 - Fixed: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
* v.1.2.4 - Multiple Sign In`s and logoffs by session timeout
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["yii2", "yii2-users", "users", "users manager", "wdmg"],
"type": "yii2-extension",
"license": "MIT",
"version": "1.2.6",
"version": "1.2.7",
"homepage": "https://github.com/wdmg/yii2-users",
"support": {
"source": "https://github.com/wdmg/yii2-users",
Expand Down
7 changes: 5 additions & 2 deletions controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,15 @@ public function actionDelete($id)
* @return Users the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
protected function findModel($id, $rest = false)
{
if (($model = Users::findOne($id)) !== null) {
return $model;
}

throw new NotFoundHttpException(Yii::t('app/modules/users', 'The requested page does not exist.'));
if ($rest)
throw new NotFoundHttpException(Yii::t('app/modules/users', 'The requested user does not exist.'));
else
throw new NotFoundHttpException(Yii::t('app/modules/users', 'The requested page does not exist.'));
}
}
1 change: 1 addition & 0 deletions messages/ru-RU/app/modules/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
'Password reset for {appname}' => 'Сброс пароля к сайту {appname}',
'Hi {username}!' => 'Здравствуйте {username}!',
'Follow the link below to reset your password: {link}' => 'Перейдите по данной ссылке для сброса вашего пароля: {link}',
'The value of field `{attribute}` must be a valid JSON, error: {error}.' => "Значение поля `{attribute}` должно быть валидным JSON-значением, ошибка: {error}.",

'Check your email for confirmation registration.' => 'Проверьте свою электронную почту для подтверждения регистрации.',
'Error sending registration confirmation email.' => 'Ошибка отправки электронного письма с подтверждением регистрации.',
Expand Down
29 changes: 29 additions & 0 deletions migrations/m230620_141245_users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use yii\db\Migration;

/**
* Class m230620_141245_users
*/
class m230620_141245_users extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{

$this->addColumn('{{%users}}', 'options', $this->json()
->null()
->after('status'));

}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('{{%users}}', 'options');
}
}
62 changes: 58 additions & 4 deletions models/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use yii\web\IdentityInterface;
use yii\base\NotSupportedException;
use wdmg\helpers\ArrayHelper;
use wdmg\validators\JsonValidator;


/**
Expand All @@ -20,6 +21,7 @@
* @property string $password_reset_token
* @property string $email
* @property int $status
* @property string $options
* @property string $created_at
* @property string $updated_at
*/
Expand All @@ -36,7 +38,10 @@ class Users extends ActiveRecord implements IdentityInterface
const USR_STATUS_ACTIVE = 10;
const USR_STATUS_BLOCKED = 15;

const USR_UPDATE_OR_CREATE_PASSWD = 'manage_user_password';

const SCENARIO_CREATE = 'create_user';
const SCENARIO_UPDATE = 'update_user';
const USR_UPDATE_OR_CREATE_PASSWD = 'manage_user_password';

public $is_online;
public $role;
Expand Down Expand Up @@ -86,8 +91,8 @@ public function rules()
[['status'], 'integer'],
[['status'], 'default', 'value' => self::USR_STATUS_ACTIVE],
[['status'], 'in', 'range' => [self::USR_STATUS_DELETED, self::USR_STATUS_WAITING, self::USR_STATUS_ACTIVE, self::USR_STATUS_BLOCKED]],

[['role'], 'in', 'range' => $this->getAllRoles(false), 'on' => self::USR_UPDATE_OR_CREATE_PASSWD],
['options', JsonValidator::class, 'message' => Yii::t('app/modules/menu', 'The value of field `{attribute}` must be a valid JSON, error: {error}.')],
[['role'], 'in', 'range' => $this->getAllRoles(false), 'on' => self::USR_UPDATE_OR_CREATE_PASSWD],
[['password', 'password_confirm'], 'string', 'min' => 8, 'max' => 24, 'on' => self::USR_UPDATE_OR_CREATE_PASSWD],
[['password', 'password_confirm'], 'match', 'pattern' => '/(.*[A-Z]){1,}.*/', 'message' => Yii::t('app/modules/users', 'The password must contains min 1 char in uppercase.'), 'on' => self::USR_UPDATE_OR_CREATE_PASSWD],
[['password', 'password_confirm'], 'match', 'pattern' => '/(.*[\d]){1,}.*/', 'message' => Yii::t('app/modules/users', 'The password must contains min 1 char as number.'), 'on' => self::USR_UPDATE_OR_CREATE_PASSWD],
Expand All @@ -98,6 +103,17 @@ public function rules()
];
}

/**
* {@inheritdoc}
*/
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_CREATE] = ['username', 'email', 'password', 'password_confirm'];
$scenarios[self::SCENARIO_UPDATE] = ['email', 'password', 'password_confirm'];
return $scenarios;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -262,7 +278,8 @@ public static function isPasswordResetTokenValid($token)
*/
public function getId()
{
return $this->getPrimaryKey();
//return $this->getPrimaryKey();
return $this->id;
}

/**
Expand Down Expand Up @@ -471,6 +488,43 @@ public function getIsOnline()
return $this->is_online;
}

/** Check and return user option(s)
*
* @return mixed
*/
public static function getOptions($name = null, $default = null)
{

$user = static::findOne(Yii::$app->getUser()->getIdentity()->getId());
if (!empty($user->options)) {

$options = $user->options;
if (!is_null($name)) {

if (isset($options[$name]))
return $options[$name];

} else {
return $options;
}
}

return $default;
}

/** Check and return user option(s)
*
* @return mixed
*/
public static function setOptions($values = null, $json_validation = true)
{
$options = self::getOptions();
$options = array_merge($options, $values);
$user = static::findOne(Yii::$app->getUser()->getIdentity()->getId());
$user->options = $options;
return $user->update((bool)$json_validation, ['options']);
}

/**
* Return stats count by all users
*
Expand Down

0 comments on commit 15da715

Please sign in to comment.