1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Admin\User; |
||
4 | |||
5 | use Apps\ActiveRecord\Profile; |
||
6 | use Apps\ActiveRecord\Role; |
||
7 | use Apps\ActiveRecord\User; |
||
8 | use Ffcms\Core\Arch\Model; |
||
9 | use Ffcms\Core\Helper\Crypt; |
||
10 | use Ffcms\Core\Helper\Type\Any; |
||
11 | use Ffcms\Core\Helper\Type\Str; |
||
12 | use Ffcms\Core\Interfaces\iUser; |
||
13 | |||
14 | /** |
||
15 | * Class FormUserUpdate. Update user data business logic model |
||
16 | * @package Apps\Model\Admin\User |
||
17 | */ |
||
18 | class FormUserUpdate extends Model |
||
19 | { |
||
20 | public $email; |
||
21 | public $login; |
||
22 | public $password; |
||
23 | public $newpassword; |
||
24 | public $role_id; |
||
25 | public $approved = false; |
||
26 | |||
27 | public $approve_token; |
||
28 | |||
29 | /** @var iUser */ |
||
30 | public $_user; |
||
31 | |||
32 | /** |
||
33 | * FormUserUpdate constructor. Pass user object inside the model |
||
34 | * @param iUser $user |
||
35 | */ |
||
36 | public function __construct(iUser $user) |
||
37 | { |
||
38 | $this->_user = $user; |
||
39 | parent::__construct(true); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Load user data on before method |
||
44 | */ |
||
45 | public function before() |
||
46 | { |
||
47 | foreach ($this->getAllProperties() as $property => $old_data) { |
||
0 ignored issues
–
show
|
|||
48 | if ($this->_user->{$property}) { |
||
49 | $this->{$property} = $this->_user->{$property}; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | if (!$this->approve_token) { |
||
54 | $this->approved = true; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Form labels to display |
||
60 | * @return array |
||
61 | */ |
||
62 | public function labels(): array |
||
63 | { |
||
64 | return [ |
||
65 | 'email' => __('Email'), |
||
66 | 'login' => __('Login'), |
||
67 | 'newpassword' => __('New password'), |
||
68 | 'role_id' => __('Role'), |
||
69 | 'approved' => __('Approved') |
||
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Validation rules for input data |
||
75 | * @return array |
||
76 | */ |
||
77 | public function rules(): array |
||
78 | { |
||
79 | return [ |
||
80 | [['email', 'login', 'role_id', 'approved'], 'required'], |
||
81 | ['newpassword', 'used'], |
||
82 | ['email', 'email'], |
||
83 | ['login', 'length_min', 3], |
||
84 | ['email', 'Apps\Model\Admin\User\FormUserUpdate::isUniqueEmail', $this->_user->getParam('id')], |
||
85 | ['login', 'Apps\Model\Admin\User\FormUserUpdate::isUniqueLogin', $this->_user->getParam('id')] |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get all roles as id=>name array |
||
91 | * @return array|null |
||
92 | */ |
||
93 | public function getRoleList() |
||
94 | { |
||
95 | return Role::getIdNameAll(); |
||
0 ignored issues
–
show
Are you sure the usage of
Apps\ActiveRecord\Role::getIdNameAll() targeting Apps\ActiveRecord\Role::getIdNameAll() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Update user information in database based on current obj attributes passed from input data |
||
100 | */ |
||
101 | public function save() |
||
102 | { |
||
103 | foreach ($this->getAllProperties() as $property => $value) { |
||
0 ignored issues
–
show
Are you sure the usage of
$this->getAllProperties() targeting Ffcms\Core\Arch\Model::getAllProperties() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
104 | if ($property === 'password' || $property === 'newpassword') { |
||
105 | // update password only if new is set and length >= 3 |
||
106 | if ($this->newpassword && Str::length($this->newpassword) >= 3) { |
||
107 | $this->_user->password = Crypt::passwordHash($this->newpassword); |
||
108 | } |
||
109 | } elseif ($property === 'approved') { |
||
110 | if ($this->approved) { |
||
111 | $this->_user->approve_token = null; |
||
112 | } else { |
||
113 | $this->_user->approve_token = $this->approve_token ?? Crypt::randomString(mt_rand(32, 128)); |
||
114 | } |
||
115 | } elseif ($property === 'approve_token') { |
||
116 | continue; |
||
117 | } else { |
||
118 | $this->_user->{$property} = $value; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | // get user id before save to determine "add" action |
||
123 | $id = $this->_user->id; |
||
124 | // safe user row |
||
125 | $this->_user->save(); |
||
126 | // if new user - add profile link |
||
127 | if ($id < 1) { |
||
128 | $profile = new Profile(); |
||
129 | $profile->user_id = $this->_user->id; |
||
130 | $profile->save(); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Check if new email is always exist |
||
136 | * @param string $email |
||
137 | * @param int|null $userId |
||
138 | * @return bool |
||
139 | */ |
||
140 | public static function isUniqueEmail($email, $userId = null) |
||
141 | { |
||
142 | $find = User::where('email', '=', $email); |
||
143 | |||
144 | if ($userId && Any::isInt($userId)) { |
||
145 | $find->where('id', '!=', $userId); |
||
146 | } |
||
147 | |||
148 | return $find->count() === 0; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check if new login is always exist |
||
153 | * @param string $login |
||
154 | * @param int|null $userId |
||
155 | * @return bool |
||
156 | */ |
||
157 | public static function isUniqueLogin($login, $userId = null) |
||
158 | { |
||
159 | $find = User::where('login', '=', $login); |
||
160 | |||
161 | if ($userId && Any::isInt($userId)) { |
||
162 | $find->where('id', '!=', $userId); |
||
163 | } |
||
164 | |||
165 | return $find->count() === 0; |
||
166 | } |
||
167 | } |
||
168 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.