UserModel::isAuthorized()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels\Models;
4
5
use Arrilot\BitrixModels\Queries\UserQuery;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * UserQuery methods
10
 * @method static static getByLogin(string $login)
11
 * @method static static getByEmail(string $email)
12
 *
13
 * Base Query methods
14
 * @method static Collection|static[] getList()
15
 * @method static static first()
16
 * @method static static getById(int $id)
17
 * @method static UserQuery sort(string|array $by, string $order='ASC')
18
 * @method static UserQuery order(string|array $by, string $order='ASC') // same as sort()
19
 * @method static UserQuery filter(array $filter)
20
 * @method static UserQuery addFilter(array $filters)
21
 * @method static UserQuery resetFilter()
22
 * @method static UserQuery navigation(array $filter)
23
 * @method static UserQuery select($value)
24
 * @method static UserQuery keyBy(string $value)
25
 * @method static UserQuery limit(int $value)
26
 * @method static UserQuery offset(int $value)
27
 * @method static UserQuery page(int $num)
28
 * @method static UserQuery take(int $value) // same as limit()
29
 * @method static UserQuery forPage(int $page, int $perPage=15)
30
 * @method static \Illuminate\Pagination\LengthAwarePaginator paginate(int $perPage = 15, string $pageName = 'page')
31
 * @method static \Illuminate\Pagination\Paginator simplePaginate(int $perPage = 15, string $pageName = 'page')
32
 * @method static UserQuery stopQuery()
33
 * @method static UserQuery cache(float|int $minutes)
34
 *
35
 * Scopes
36
 * @method static UserQuery active()
37
 * @method UserQuery fromGroup(int $groupId)
38
 */
39
class UserModel extends BitrixModel
40
{
41
    /**
42
     * Bitrix entity object.
43
     *
44
     * @var object
45
     */
46
    public static $bxObject;
47
48
    /**
49
     * Corresponding object class name.
50
     *
51
     * @var string
52
     */
53
    protected static $objectClass = 'CUser';
54
55
    /**
56
     * Current user cache.
57
     *
58
     * @var static
59
     */
60
    protected static $currentUser = null;
61
62
    /**
63
     * Have groups been already fetched from DB?
64
     *
65
     * @var bool
66
     */
67
    protected $groupsAreFetched = false;
68
69
    /**
70
     * Instantiate a query object for the model.
71
     *
72
     * @return UserQuery
73
     */
74
    public static function query()
75
    {
76
        return new UserQuery(static::instantiateObject(), get_called_class());
77
    }
78
79
    /**
80
     * Get a new instance for the current user
81
     *
82
     * @return static
83
     */
84
    public static function current()
85
    {
86
        return is_null(static::$currentUser)
87
            ? static::freshCurrent()
88
            : static::$currentUser;
89
    }
90
91
    /**
92
     * Get a fresh instance for the current user and save it to local cache.
93
     *
94
     * @return static
95
     */
96
    public static function freshCurrent()
97
    {
98
        global $USER;
99
100
        return static::$currentUser = (new static($USER->getId()))->load();
101
    }
102
103
    /**
104
     * Fill extra fields when $this->field is called.
105
     *
106
     * @return null
107
     */
108
    protected function afterFill()
109
    {
110
        if (isset($this->fields['GROUP_ID']) && is_array(['GROUP_ID'])) {
111
            $this->groupsAreFetched = true;
112
        }
113
    }
114
115
    /**
116
     * Fill model groups if they are already known.
117
     * Saves DB queries.
118
     *
119
     * @param array $groups
120
     *
121
     * @return null
122
     */
123
    public function fillGroups($groups)
124
    {
125
        $this->fields['GROUP_ID'] = $groups;
126
127
        $this->groupsAreFetched = true;
128
    }
129
130
    /**
131
     * Load model fields from database if they are not loaded yet.
132
     *
133
     * @return $this
134
     */
135
    public function load()
136
    {
137
        $this->getFields();
138
        $this->getGroups();
139
140
        return $this;
141
    }
142
143
    /**
144
     * Get user groups from cache or database.
145
     *
146
     * @return array
147
     */
148
    public function getGroups()
149
    {
150
        if ($this->groupsAreFetched) {
151
            return $this->fields['GROUP_ID'];
152
        }
153
154
        return $this->refreshGroups();
155
    }
156
157
    /**
158
     * Refresh model from database and place data to $this->fields.
159
     *
160
     * @return array
161
     */
162
    public function refresh()
163
    {
164
        $this->refreshFields();
165
166
        $this->refreshGroups();
167
168
        return $this->fields;
169
    }
170
171
    /**
172
     * Refresh user fields and save them to a class field.
173
     *
174
     * @return array
175
     */
176 View Code Duplication
    public function refreshFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        if ($this->id === null) {
179
            $this->original = [];
180
            return $this->fields = [];
181
        }
182
183
        $groupBackup = isset($this->fields['GROUP_ID']) ? $this->fields['GROUP_ID'] : null;
184
185
        $this->fields = static::query()->getById($this->id)->fields;
186
187
        if ($groupBackup) {
188
            $this->fields['GROUP_ID'] = $groupBackup;
189
        }
190
191
        $this->fieldsAreFetched = true;
192
193
        $this->original = $this->fields;
194
195
        return $this->fields;
196
    }
197
198
    /**
199
     * Refresh user groups and save them to a class field.
200
     *
201
     * @return array
202
     */
203
    public function refreshGroups()
204
    {
205
        if ($this->id === null) {
206
            return [];
207
        }
208
209
        global $USER;
210
211
        $this->fields['GROUP_ID'] = $this->isCurrent()
212
            ? $USER->getUserGroupArray()
213
            : static::$bxObject->getUserGroup($this->id);
214
215
        $this->groupsAreFetched = true;
216
217
        return $this->fields['GROUP_ID'];
218
    }
219
220
    /**
221
     * Check if user is an admin.
222
     */
223
    public function isAdmin()
224
    {
225
        return $this->hasGroupWithId(1);
226
    }
227
228
    /**
229
     * Check if this user is the operating user.
230
     */
231
    public function isCurrent()
232
    {
233
        global $USER;
234
235
        return $USER->getId() && $this->id == $USER->getId();
236
    }
237
238
    /**
239
     * Check if user has role with a given ID.
240
     *
241
     * @param $role_id
242
     *
243
     * @return bool
244
     */
245
    public function hasGroupWithId($role_id)
246
    {
247
        return in_array($role_id, $this->getGroups());
248
    }
249
250
    /**
251
     * Check if user is authorized.
252
     *
253
     * @return bool
254
     */
255
    public function isAuthorized()
256
    {
257
        global $USER;
258
259
        return ($USER->getId() == $this->id) && $USER->isAuthorized();
260
    }
261
262
    /**
263
     * Check if user is guest.
264
     *
265
     * @return bool
266
     */
267
    public function isGuest()
268
    {
269
        return ! $this->isAuthorized();
270
    }
271
272
    /**
273
     * Logout user.
274
     *
275
     * @return void
276
     */
277
    public function logout()
278
    {
279
        global $USER;
280
281
        $USER->logout();
282
    }
283
284
    /**
285
     * Scope to get only users from a given group / groups.
286
     *
287
     * @param UserQuery $query
288
     * @param int|array $id
289
     *
290
     * @return UserQuery
291
     */
292
    public function scopeFromGroup($query, $id)
293
    {
294
        $query->filter['GROUPS_ID'] = $id;
295
296
        return $query;
297
    }
298
299
    /**
300
     * Substitute old group with the new one.
301
     *
302
     * @param int $old
303
     * @param int $new
304
     *
305
     * @return void
306
     */
307
    public function substituteGroup($old, $new)
308
    {
309
        $groups = $this->getGroups();
310
311
        if(($key = array_search($old, $groups)) !== false) {
312
            unset($groups[$key]);
313
        }
314
315
        if (!in_array($new, $groups)) {
316
            $groups[] = $new;
317
        }
318
319
        $this->fields['GROUP_ID'] = $groups;
320
    }
321
}
322