1 | <?php |
||
2 | |||
3 | namespace Jidaikobo\Kontiki\Models; |
||
4 | |||
5 | class UserModel extends BaseModel |
||
6 | { |
||
7 | use Traits\CRUDTrait; |
||
8 | use Traits\MetaDataTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
9 | use Traits\IndexTrait; |
||
10 | |||
11 | protected string $table = 'users'; |
||
12 | |||
13 | protected function defineFieldDefinitions(): void |
||
14 | { |
||
15 | // add dynamic rules at $this->processFieldDefinitions() |
||
16 | $this->fieldDefinitions = [ |
||
17 | 'id' => $this->getIdField(), |
||
18 | |||
19 | 'username' => $this->getField( |
||
20 | __('username'), |
||
21 | [ |
||
22 | 'rules' => [ |
||
23 | 'required', |
||
24 | ['lengthMin', 3] |
||
25 | ], |
||
26 | 'display_in_list' => true |
||
27 | ] |
||
28 | ), |
||
29 | |||
30 | 'password' => $this->getField( |
||
31 | __('password'), |
||
32 | [ |
||
33 | 'type' => 'password', |
||
34 | 'rules' => [ |
||
35 | 'required', |
||
36 | ['lengthMin', 8] |
||
37 | ], |
||
38 | 'filter' => FILTER_UNSAFE_RAW, |
||
39 | ] |
||
40 | ), |
||
41 | |||
42 | 'role' => $this->getField( |
||
43 | __('role'), |
||
44 | [ |
||
45 | 'type' => 'select', |
||
46 | 'options' => [ |
||
47 | 'editor' => __('editor'), |
||
48 | 'admin' => __('admin'), |
||
49 | ], |
||
50 | 'rules' => [ |
||
51 | 'required', |
||
52 | ], |
||
53 | 'attributes' => [ |
||
54 | 'class' => 'form-control form-select' |
||
55 | ], |
||
56 | 'display_in_list' => true |
||
57 | ] |
||
58 | ), |
||
59 | |||
60 | 'created_at' => $this->getReadOnlyField( |
||
61 | __('created_at', 'Created'), |
||
62 | [ |
||
63 | 'display_in_list' => true |
||
64 | ] |
||
65 | ), |
||
66 | ]; |
||
67 | } |
||
68 | |||
69 | protected function processFieldDefinitions( |
||
70 | string $context = '', |
||
71 | array $data = [], |
||
72 | int $id = null |
||
73 | ): void { |
||
74 | // add rule |
||
75 | $this->fieldDefinitions['username']['rules'][] = [ |
||
76 | 'unique', |
||
77 | $this->table, |
||
78 | 'username', |
||
79 | $id |
||
80 | ]; |
||
81 | |||
82 | if ($context == 'create') { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | // Exclude `required` from password's rules |
||
87 | // No password specified means no change |
||
88 | $this->fieldDefinitions['password']['rules'] = array_filter( |
||
89 | $this->fieldDefinitions['password']['rules'], |
||
90 | fn($rule) => $rule !== 'required' |
||
91 | ); |
||
92 | |||
93 | $this->fieldDefinitions['password']['description'] = __('users_edit_message'); |
||
94 | |||
95 | // disable form elements |
||
96 | if (in_array($context, ['trash', 'restore', 'delete'])) { |
||
97 | $this->disableFormFieldsForContext(); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public function validate(array $data, array $context): array |
||
102 | { |
||
103 | $result = parent::validate($data, $context); |
||
104 | $adminCheck = $this->atLeastOneAdmin($data, $context); |
||
105 | $deleteCheck = $this->cannotDeleteAdmin($context); |
||
106 | |||
107 | return [ |
||
108 | 'valid' => $result['valid'] && $adminCheck['valid'] && $deleteCheck['valid'], |
||
109 | 'errors' => array_merge_recursive( |
||
110 | $result['errors'], |
||
111 | $adminCheck['errors'], |
||
112 | $deleteCheck['errors'] |
||
113 | ) |
||
114 | ]; |
||
115 | } |
||
116 | |||
117 | private function atLeastOneAdmin(array $data, array $context): array |
||
118 | { |
||
119 | $result = [ |
||
120 | 'valid' => true, |
||
121 | 'errors' => [] |
||
122 | ]; |
||
123 | |||
124 | $id = $context['id'] ?? 0; |
||
125 | if (!$id || !isset($data['role'])) { |
||
126 | return $result; |
||
127 | } |
||
128 | |||
129 | $targetUser = $this->getById($id); |
||
130 | |||
131 | if ($this->isDemotingLastAdmin($targetUser, $data)) { |
||
132 | $result['valid'] = false; |
||
133 | $result['errors']['role']['messages'] = [__('at_least_one_admin')]; |
||
134 | } |
||
135 | |||
136 | return $result; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Check if the given user is the last admin and being demoted. |
||
141 | */ |
||
142 | private function isDemotingLastAdmin(?array $user, array $newData): bool |
||
143 | { |
||
144 | if (!$user || $user['role'] !== 'admin' || $newData['role'] === 'admin') { |
||
0 ignored issues
–
show
The expression
$user of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
145 | return false; |
||
146 | } |
||
147 | |||
148 | $otherAdmins = $this->db->table($this->table) |
||
149 | ->where('role', 'admin') |
||
150 | ->where('id', '!=', $user['id']) |
||
151 | ->count(); |
||
152 | |||
153 | return $otherAdmins === 0; |
||
154 | } |
||
155 | |||
156 | private function cannotDeleteAdmin(array $context): array |
||
157 | { |
||
158 | $result = [ |
||
159 | 'valid' => true, |
||
160 | 'errors' => [] |
||
161 | ]; |
||
162 | |||
163 | if (($context['context'] ?? '') !== 'delete') { |
||
164 | return $result; |
||
165 | } |
||
166 | |||
167 | $id = $context['id'] ?? 0; |
||
168 | $targetUser = $this->getById($id); |
||
169 | |||
170 | if ($targetUser['role'] !== 'admin') { |
||
171 | return $result; |
||
172 | } |
||
173 | $result['valid'] = false; |
||
174 | $result['errors']['role']['messages'] = [__('cannot_delete_admin')]; |
||
175 | |||
176 | return $result; |
||
177 | } |
||
178 | |||
179 | private function hashPassword(string $password): string |
||
180 | { |
||
181 | return password_hash($password, PASSWORD_BCRYPT); |
||
0 ignored issues
–
show
|
|||
182 | } |
||
183 | |||
184 | protected function processDataForForm(string $actionType, array $data): array |
||
185 | { |
||
186 | if ($actionType == 'edit') { |
||
187 | $data['password'] = ''; |
||
188 | } |
||
189 | return $data; |
||
190 | } |
||
191 | |||
192 | protected function afterProcessDataBeforeSave(string $context, array $data): array |
||
193 | { |
||
194 | if ($context == 'create') { |
||
195 | $data['password'] = $this->hashPassword($data['password']); |
||
196 | } |
||
197 | |||
198 | if ($context == 'update') { |
||
199 | // Branching password processing |
||
200 | if (isset($data['password'])) { |
||
201 | if (trim($data['password']) === '') { |
||
202 | unset($data['password']); |
||
203 | } else { |
||
204 | $data['password'] = $this->hashPassword($data['password']); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | return $data; |
||
209 | } |
||
210 | } |
||
211 |