|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\rbac\DbManager; |
|
7
|
|
|
use yii\db\TableSchema; |
|
8
|
|
|
use yii\web\IdentityInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class InitialUserSettings |
|
12
|
|
|
* |
|
13
|
|
|
* This class for setting initial user settings, such as status and role after registration. |
|
14
|
|
|
*/ |
|
15
|
|
|
class InitialUserSettings |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Initial constants. |
|
19
|
|
|
*/ |
|
20
|
|
|
const INIT_USER_STATUS = 1; |
|
21
|
|
|
const INIT_USER_ROLE = 'admin'; |
|
22
|
|
|
|
|
23
|
|
|
const AUTH_MANAGER_CLASS = DbManager::class; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Set initial user settings. |
|
27
|
|
|
* |
|
28
|
|
|
* @param IdentityInterface $user |
|
29
|
|
|
* |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function run(IdentityInterface $user): bool |
|
33
|
|
|
{ |
|
34
|
|
|
$db = Yii::$app->db; |
|
35
|
|
|
|
|
36
|
|
|
/* @var $settingsTableSchema TableSchema */ |
|
37
|
|
|
$settingsTableSchema = $db->getTableSchema('settings'); |
|
38
|
|
|
|
|
39
|
|
|
if (null === $settingsTableSchema) { |
|
40
|
|
|
Yii::error('Table "settings" does not exists for initial user properties.'); |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$settings = $db->createCommand('SELECT * FROM settings') |
|
45
|
|
|
->queryOne(); |
|
46
|
|
|
|
|
47
|
|
|
self::setUserStatus($settingsTableSchema, $settings, $user); |
|
48
|
|
|
|
|
49
|
|
|
self::setUserRole($settingsTableSchema, $settings, $user); |
|
50
|
|
|
|
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set initial user status. |
|
56
|
|
|
* |
|
57
|
|
|
* @param TableSchema $settingsTableSchema |
|
58
|
|
|
* @param array|bool $settings |
|
59
|
|
|
* @param IdentityInterface $user |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
private static function setUserStatus(TableSchema $settingsTableSchema, $settings, IdentityInterface $user): void |
|
64
|
|
|
{ |
|
65
|
|
|
if (in_array('initUserStatus', $settingsTableSchema->columnNames)) { |
|
66
|
|
|
$user->status = is_array($settings) && null !== $settings['initUserStatus'] ? |
|
|
|
|
|
|
67
|
|
|
$settings['initUserStatus'] : self::INIT_USER_STATUS; |
|
68
|
|
|
$user->save(); |
|
|
|
|
|
|
69
|
|
|
} else { |
|
70
|
|
|
Yii::error('Field "initUserStatus" does not exists in "settings" table to set initial user status.'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set initial user role. |
|
76
|
|
|
* |
|
77
|
|
|
* @param TableSchema $settingsTableSchema |
|
78
|
|
|
* @param array|bool $settings |
|
79
|
|
|
* @param IdentityInterface $user |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
private static function setUserRole(TableSchema $settingsTableSchema, $settings, IdentityInterface $user): void |
|
84
|
|
|
{ |
|
85
|
|
|
if (in_array('initUserRole', $settingsTableSchema->columnNames)) { |
|
86
|
|
|
|
|
87
|
|
|
/* @var $authManager \yii\rbac\BaseManager */ |
|
88
|
|
|
$authManager = Yii::$app->get('authManager'); |
|
89
|
|
|
|
|
90
|
|
|
if (null === $authManager) { |
|
91
|
|
|
$authManager = Yii::createObject([ |
|
92
|
|
|
'class' => self::AUTH_MANAGER_CLASS, |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!is_array($settings) || null === $settings['initUserRole']) { |
|
97
|
|
|
$initUserRole = self::INIT_USER_ROLE; |
|
98
|
|
|
Yii::error('Can not get field value of "initUserRole" from "settings" table to set initial user role.'); |
|
99
|
|
|
} else { |
|
100
|
|
|
$initUserRole = $settings['initUserRole']; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$authManager->assign( |
|
104
|
|
|
$authManager->getRole($initUserRole), |
|
105
|
|
|
$user->getId() |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
} else { |
|
109
|
|
|
Yii::error('Field "initUserRole" does not exists in "settings" table to set initial user role.'); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|