1 | <?php |
||
7 | class UserModel extends BaseModel |
||
8 | { |
||
9 | /** |
||
10 | * Bitrix entity object. |
||
11 | * |
||
12 | * @var object |
||
13 | */ |
||
14 | public static $bxObject; |
||
15 | |||
16 | /** |
||
17 | * Corresponding object class name. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected static $objectClass = 'CUser'; |
||
22 | |||
23 | /** |
||
24 | * Have groups been already fetched from DB? |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $groupsAreFetched = false; |
||
29 | |||
30 | /** |
||
31 | * Instantiate a query object for the model. |
||
32 | * |
||
33 | * @return UserQuery |
||
34 | */ |
||
35 | public static function query() |
||
39 | |||
40 | /** |
||
41 | * Get a new instance for the current user. |
||
42 | * |
||
43 | * @param null $fields |
||
44 | * |
||
45 | * @return static |
||
46 | */ |
||
47 | public static function current($fields = null) |
||
59 | |||
60 | /** |
||
61 | * Fill extra fields when $this->field is called. |
||
62 | * |
||
63 | * @return null |
||
64 | */ |
||
65 | protected function afterFill() |
||
71 | |||
72 | /** |
||
73 | * Fill model groups if they are already known. |
||
74 | * Saves DB queries. |
||
75 | * |
||
76 | * @param array $groups |
||
77 | * |
||
78 | * @return null |
||
79 | */ |
||
80 | public function fillGroups($groups) |
||
86 | |||
87 | /** |
||
88 | * Get all model attributes from cache or database. |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public function get() |
||
100 | |||
101 | /** |
||
102 | * Get user groups from cache or database. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function getGroups() |
||
114 | |||
115 | /** |
||
116 | * Refresh model from database and place data to $this->fields. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public function refresh() |
||
128 | |||
129 | /** |
||
130 | * Refresh user fields and save them to a class field. |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | public function refreshFields() |
||
135 | { |
||
136 | if ($this->id === null) { |
||
137 | return $this->fields = []; |
||
138 | } |
||
139 | |||
140 | $groupBackup = isset($this->fields['GROUP_ID']) ? $this->fields['GROUP_ID'] : null; |
||
141 | |||
142 | $this->fields = static::query()->getById($this->id)->fields; |
||
143 | |||
144 | if ($groupBackup) { |
||
145 | $this->fields['GROUP_ID'] = $groupBackup; |
||
146 | } |
||
147 | |||
148 | $this->fieldsAreFetched = true; |
||
149 | |||
150 | return $this->fields; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Refresh user groups and save them to a class field. |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function refreshGroups() |
||
174 | |||
175 | /** |
||
176 | * Check if user is an admin. |
||
177 | */ |
||
178 | public function isAdmin() |
||
182 | |||
183 | /** |
||
184 | * Check if this user is the operating user. |
||
185 | */ |
||
186 | public function isCurrent() |
||
192 | |||
193 | /** |
||
194 | * Check if user has role with a given ID. |
||
195 | * |
||
196 | * @param $role_id |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function hasGroupWithId($role_id) |
||
204 | |||
205 | /** |
||
206 | * Check if user is authorized. |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function isAuthorized() |
||
216 | |||
217 | /** |
||
218 | * Check if user is guest. |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function isGuest() |
||
226 | |||
227 | /** |
||
228 | * Logout user. |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | public function logout() |
||
238 | |||
239 | /** |
||
240 | * Scope to get only users from a given group / groups. |
||
241 | * |
||
242 | * @param UserQuery $query |
||
243 | * @param int|array $id |
||
244 | * |
||
245 | * @return UserQuery |
||
246 | */ |
||
247 | public function scopeFromGroup($query, $id) |
||
253 | |||
254 | /** |
||
255 | * Substitute old group with the new one. |
||
256 | * |
||
257 | * @param int $old |
||
258 | * @param int $new |
||
259 | * |
||
260 | * @return void |
||
261 | */ |
||
262 | public function substituteGroup($old, $new) |
||
276 | } |
||
277 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state