Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
UserModel.php
1<?php
2
4
9
18{
19 private $permissions;
20
28 public function getRoles(): array
29 {
30 if ($this->roles === null)
31 {
32 $this->roles = [];
33 if ($this->userId === 0 || empty($this->getAccessCodes()))
34 {
35 return $this->roles;
36 }
37
38 $relationRows = RoleRelationTable::query()
39 ->addSelect('ROLE_ID')
40 ->whereIn('RELATION', $this->getAccessCodes())
41 ->exec()
42 ->fetchAll()
43 ;
44
45 $this->roles = array_unique(
46 array_column($relationRows, 'ROLE_ID')
47 );
48 }
49
50 return $this->roles;
51 }
52
63 public function getPermission(string $permissionId): ?int
64 {
65 $permissions = $this->getPermissions();
66
67 $permissions[$permissionId] = $permissions[$permissionId] ?? null;
68
69 if (is_array($permissions[$permissionId]))
70 {
71 $permissions[$permissionId] =
72 isset($permissions[$permissionId][0])
73 ? (int)$permissions[$permissionId][0]
74 : null
75 ;
76 }
77
78 return $permissions[$permissionId];
79 }
80
91 public function getPermissionMulti(string $permissionId): ?array
92 {
93 if ($this->isAdmin())
94 {
95 return [PermissionDictionary::VALUE_VARIATION_ALL];
96 }
97
98 $permissions = $this->getPermissions();
99 $permissions[$permissionId] = $permissions[$permissionId] ?? null;
100
101 return is_array($permissions[$permissionId]) ? $permissions[$permissionId] : null;
102 }
103
110 private function getPermissions(): array
111 {
112 if ($this->permissions !== null)
113 {
114 return $this->permissions;
115 }
116
117 $this->permissions = [];
118 $roles = $this->getRoles();
119
120 if (empty($roles))
121 {
122 return $this->permissions;
123 }
124
125 $query = PermissionTable::query();
126
127 $permissions = $query
128 ->addSelect("PERMISSION_ID")
129 ->addSelect("VALUE")
130 ->whereIn("ROLE_ID", $roles)
131 ->exec()
132 ;
133
134 while ($permission = $permissions->fetch())
135 {
136 $permissionId = $permission["PERMISSION_ID"];
137 $value = (int)$permission["VALUE"];
138
139 $permissionDescription = PermissionDictionary::getPermission($permissionId);
140 if ($permissionDescription['type'] === PermissionDictionary::TYPE_MULTIVARIABLES)
141 {
142 $this->permissions[$permissionId] = $this->permissions[$permissionId] ?? [];
143 if (in_array(PermissionDictionary::VALUE_VARIATION_ALL, $this->permissions[$permissionId], true))
144 {
145 continue;
146 }
147
148 if ($value === PermissionDictionary::VALUE_VARIATION_ALL)
149 {
150 $this->permissions[$permissionId] = [$value];
151 }
152 elseif (!in_array($value, $this->permissions[$permissionId], true))
153 {
154 $this->permissions[$permissionId][] = $value;
155 }
156 }
157 else
158 {
159 $this->permissions[$permissionId] = $this->permissions[$permissionId] ?? 0;
160 if ($value > $this->permissions[$permissionId])
161 {
162 $this->permissions[$permissionId] = $value;
163 }
164 }
165 }
166
167 return $this->permissions;
168 }
169
173 public function getRightGroups(): array
174 {
175 return \CUser::GetUserGroup($this->userId);
176 }
177}
getPermissionMulti(string $permissionId)
Definition UserModel.php:91
getPermission(string $permissionId)
Definition UserModel.php:63