Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userfieldpermissiontable.php
1<?php
2
4
11
29{
30 private const PERMISSION_ALLOWED = 1;
31
32 public static function getTableName()
33 {
34 return 'b_user_field_permission';
35 }
36
37 public static function getObjectClass()
38 {
39 return UserFieldPermission::class;
40 }
41
42 public static function getMap()
43 {
44 return [
45 new Entity\IntegerField('ID', [
46 'autocomplete' => true,
47 'primary' => true
48 ]),
49 new Entity\IntegerField('ENTITY_TYPE_ID', [
50 'required' => true
51 ]),
52 new Entity\IntegerField('USER_FIELD_ID', [
53 'required' => true
54 ]),
55 new Entity\StringField('ACCESS_CODE', [
56 'required' => true
57 ]),
58 new Entity\StringField('PERMISSION_ID', [
59 'required' => true
60 ]),
61 new Entity\IntegerField('VALUE', [
62 'required' => true
63 ]),
64 (new Reference(
65 'USER_FIELD',
66 UserFieldTable::class,
67 Join::on('this.USER_FIELD_ID', 'ref.ID')
68 )),
69 new Reference(
70 'USER_ACCESS',
71 UserAccessTable::class,
72 Join::on('this.ACCESS_CODE', 'ref.ACCESS_CODE')
73 ),
74 ];
75 }
76
81 protected static function updateChildPermission($primary, array $data)
82 {
83 $data = self::loadUpdateRow($primary, $data);
84 if ((int)$data['VALUE'] === PermissionDictionary::VALUE_YES) {
85 return;
86 }
87 $sql = "
88 UPDATE `" . static::getTableName() . "`
89 SET VALUE = " . PermissionDictionary::VALUE_NO . "
90 WHERE
91 USER_FIELD_ID = " . $data['USER_FIELD_ID'] . "
92 AND ACCESS_CODE = " . $data['ACCESS_CODE'] . "
93 AND PERMISSION_ID LIKE '" . $data['PERMISSION_ID'] . ".%'
94 ";
95 static::getEntity()->getConnection()->query($sql);
96 }
97
102 public static function validateRow(array $data): bool
103 {
104 $parentPermissions = PermissionDictionary::getParentsPath($data['PERMISSION_ID']);
105 if (!$parentPermissions)
106 {
107 return true;
108 }
109
110 $res = self::getRow([
111 'select' => ['VALUE'],
112 'filter' => [
113 '=USER_FIELD_ID' => (int)$data['USER_FIELD_ID'],
114 '=ACCESS_CODE' => (int)$data['ACCESS_CODE'],
115 '%=PERMISSION_ID' => $parentPermissions,
117 ]
118 ]);
119
120 if (is_array($res) && count($res))
121 {
122 return false;
123 }
124
125 return true;
126 }
127
132 public static function getUserFieldsAccessCodes(int $entityTypeID): array
133 {
134 $query = new Entity\Query(self::getEntity());
135 $query->addSelect('USER_FIELD.FIELD_NAME', 'FIELD_NAME');
136 $query->addSelect('ACCESS_CODE');
137 $query->addSelect('USER_ACCESS.USER_ID', 'USER_ID');
138 $query->addSelect('USER_FIELD_ID');
139 $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
140 $query->addFilter('=VALUE', self::PERMISSION_ALLOWED);
141 $query->whereNotNull('FIELD_NAME');
142
143 $dbResult = $query->exec();
144
145 return $dbResult->fetchAll();
146 }
147
155 public static function saveEntityConfiguration(
156 $accessCodes,
157 string $fieldName,
158 int $entityTypeId,
159 string $permissionId,
160 ?string $entityTypeName = null
161 ): void
162 {
163 if ($userField = self::getUserFieldId($fieldName, $entityTypeName))
164 {
165 self::removeEntityConfiguration($userField['ID'], $entityTypeId);
166 if (is_array($accessCodes))
167 {
168 foreach ($accessCodes as $accessCode)
169 {
173 $permission = self::createObject([
174 'ENTITY_TYPE_ID' => $entityTypeId,
175 'USER_FIELD_ID' => $userField['ID'],
176 'ACCESS_CODE' => $accessCode['ID'],
177 'PERMISSION_ID' => $permissionId,
178 'VALUE' => self::PERMISSION_ALLOWED
179 ]);
180 $permission->save();
181 }
182 }
183 }
184 }
185
190 private static function removeEntityConfiguration(int $userFieldId, int $entityTypeId): void
191 {
193 '=ENTITY_TYPE_ID' => $entityTypeId,
194 '=USER_FIELD_ID' => $userFieldId
195 ]);
196 }
197
203 private static function getUserFieldId(string $fieldName, ?string $entityId = null): ?array
204 {
205 $filter = ['=FIELD_NAME' => $fieldName];
206 if ($entityId)
207 {
208 $filter['=ENTITY_ID'] = $entityId;
209 }
210
212 'select' => ['ID'],
213 'filter' => $filter
214 ]);
215 }
216}
static getRow(array $parameters)