Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usermodel.php
1<?php
10
15
16abstract class UserModel
17 implements AccessibleUser
18{
19 protected
26
27 protected static $cache = [];
28
29 public static function createFromId(int $userId): AccessibleUser
30 {
31 $key = 'USER_'.static::class.'_'.$userId;
32 if (!array_key_exists($key, static::$cache))
33 {
34 $model = new static();
35 $model->setUserId($userId);
36 static::$cache[$key] = $model;
37 }
38 return static::$cache[$key];
39 }
40
41 protected function __construct()
42 {
43 }
44
45 public function getUserId(): int
46 {
47 return $this->userId;
48 }
49
50 public function setUserId(int $userId): AccessibleUser
51 {
52 $this->userId = $userId;
53 return $this;
54 }
55
56 public function getName(): string
57 {
58 if ($this->name === null)
59 {
60 if ($this->userId === 0)
61 {
62 $this->name = '';
63 return $this->name;
64 }
65
66 }
67 return $this->name;
68 }
69
70 public function getUserDepartments(): array
71 {
72 if ($this->userDepartments === null)
73 {
74 $this->userDepartments = UserSubordinate::getDepartmentsByUserId($this->userId);
75 }
77 }
78
79 public function isAdmin(): bool
80 {
81 if (!$this->userId)
82 {
83 return false;
84 }
85 if ($this->isAdmin === null)
86 {
87 $currentUser = CurrentUser::get();
88 if ((int) $currentUser->getId() === $this->userId)
89 {
90 $userGroups = $currentUser->getUserGroups();
91 }
92 else
93 {
94 $userGroups = \CUser::GetUserGroup($this->userId);
95 }
96 $this->isAdmin = in_array(1, $userGroups);
97 }
98 return $this->isAdmin;
99 }
100
101 public function getAccessCodes(): array
102 {
103 if ($this->accessCodes === null)
104 {
105 $this->accessCodes = [];
106 if ($this->userId === 0)
107 {
108 return $this->accessCodes;
109 }
110
111 // mantis #0160102
112 $res = UserAccessTable::getList([
113 'select' => ['ACCESS_CODE'],
114 'filter' => [
115 '=USER_ID' => $this->userId,
116 '!%=ACCESS_CODE' => 'CHAT%',
117 ]
118 ]);
119 foreach ($res as $row)
120 {
121 $signature = (new AccessCode($row['ACCESS_CODE']))->getSignature();
122 if ($signature)
123 {
124 $this->accessCodes[$signature] = $signature;
125 }
126 }
127
128 $this->accessCodes = array_values($this->accessCodes);
129
130 // add employee access code
131 if (!\Bitrix\Main\ModuleManager::isModuleInstalled('intranet'))
132 {
133 return $this->accessCodes;
134 }
135
136 $user = UserTable::getList([
137 'select' => ['UF_DEPARTMENT'],
138 'filter' => [
139 '=ID' => $this->userId,
140 ],
141 'limit' => 1
142 ])->fetch();
143
144 if (
145 $user
146 && is_array($user['UF_DEPARTMENT'])
147 && count($user['UF_DEPARTMENT'])
148 && !empty(array_values($user['UF_DEPARTMENT'])[0])
149 )
150 {
151 $this->accessCodes[] = AccessCode::ACCESS_EMPLOYEE . '0';
152 }
153 }
154 return $this->accessCodes;
155 }
156
157 public function getSubordinate(int $userId): int
158 {
159 return (new UserSubordinate($this->userId))->getSubordinate($userId);
160 }
161
162 abstract public function getRoles(): array;
163
164 abstract public function getPermission(string $permissionId): ?int;
165}
static createFromId(int $userId)
Definition usermodel.php:29
getPermission(string $permissionId)
static isModuleInstalled($moduleName)
static getList(array $parameters=array())