Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userfieldmodel.php
1<?php
2
4
8
10{
11 private static $permissions;
12 private $id = 0;
13
14 public static function createNew(): self
15 {
16 return new self();
17 }
18
19 public static function createFromId(int $userFieldId): AccessibleItem
20 {
21 $model = new self();
22 $model->setId($userFieldId);
23 return $model;
24 }
25
26 private function __construct()
27 {
28 }
29
30 public function getId(): int
31 {
32 return $this->id;
33 }
34
35 public function setId(int $id): self
36 {
37 $this->id = $id;
38 return $this;
39 }
40
46 public function getPermission(UserAccessibleInterface $user, $permissionId): int
47 {
48 $permissions = $this->loadPermissions();
49
50 $value = 0;
51 foreach ($user->getAccessCodes() as $ac)
52 {
53 if (!array_key_exists($ac, $permissions))
54 {
55 continue;
56 }
57 $value = ($permissions[$ac][$permissionId] > $value) ? $permissions[$ac][$permissionId] : $value;
58 }
59
60 return $value;
61 }
62
68 public function getPermissions(UserAccessibleInterface $user, int $permissionId): array
69 {
70 $permissions = $this->loadPermissions();
71
72 $values = [];
73 foreach ($user->getAccessCodes() as $ac)
74 {
75 foreach ($permissions as $userFieldId => $userFieldPermission)
76 {
77 if (!isset($values[$userFieldId]))
78 {
79 $values[$userFieldId] = 0;
80 }
81
86 if(!isset($userFieldPermission[$permissionId]) && !isset($userFieldPermission[$permissionId][$ac]))
87 {
88 $userFieldPermission[$permissionId][$ac] = true;
89 }
90 // if the field has other codes but no specific user codes, then the field is not allowed
91 else if(!isset($userFieldPermission[$permissionId][$ac]))
92 {
93 $userFieldPermission[$permissionId][$ac] = false;
94 }
95
96 $values[$userFieldId] = (
97 ($userFieldPermission[$permissionId][$ac] > $values[$userFieldId])
98 ? $userFieldPermission[$permissionId][$ac]
99 : $values[$userFieldId]
100 );
101
102 }
103 }
104
105 return $values;
106 }
107
111 public function loadPermissions(): array
112 {
113 if (static::$permissions === null)
114 {
115 static::$permissions = [];
116
117 $res = UserFieldPermissionTable::query()
118 ->addSelect('*')
119 //->where('ENTITY_TYPE_ID', $entityTypeId)
120 ->exec()
121 ->fetchAll();
122
123 foreach ($res as $row)
124 {
125 static::$permissions[$row['USER_FIELD_ID']][$row['PERMISSION_ID']][$row['ACCESS_CODE']] = (int) $row['VALUE'];
126 }
127
128 }
129 return static::$permissions;
130 }
131}
getPermissions(UserAccessibleInterface $user, int $permissionId)
getPermission(UserAccessibleInterface $user, $permissionId)