Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
accesspermissiontable.php
1<?php
10
17
18Loc::loadMessages(__FILE__);
19
20abstract class AccessPermissionTable extends DataManager
21{
22 public static function getMap()
23 {
24 return [
25 new Entity\IntegerField('ID', [
26 'autocomplete' => true,
27 'primary' => true
28 ]),
29 new Entity\IntegerField('ROLE_ID', [
30 'required' => true
31 ]),
32 new Entity\StringField('PERMISSION_ID', [
33 'required' => true
34 ]),
35 new Entity\IntegerField('VALUE', [
36 'required' => true
37 ])
38 ];
39 }
40
41 public static function checkFields(Result $result, $primary, array $data)
42 {
43 parent::checkFields($result, $primary, $data);
44
45 if (!$result->isSuccess(true))
46 {
47 return;
48 }
49
50 if (!static::checkDataFields($data))
51 {
52 if (empty($primary))
53 {
54 $result->addError(new Entity\EntityError(Loc::getMessage('ACCESS_PERMISSION_PARENT_VALIDATE_ERROR')));
55 return;
56 }
57 $data = static::loadUpdateRow($primary, $data);
58 }
59
60 if (!static::validateRow($data))
61 {
62 $result->addError(new Entity\EntityError(Loc::getMessage('ACCESS_PERMISSION_PARENT_VALIDATE_ERROR')));
63 }
64 }
65
66 public static function addMulti($rows, $ignoreEvents = false)
67 {
68 throw new NotSupportedException();
69 }
70
71 public static function updateMulti($primaries, $data, $ignoreEvents = false)
72 {
73 throw new NotSupportedException();
74 }
75
76 public static function onAfterAdd(Event $event)
77 {
78 $primary = $event->getParameter("primary");
79 $data = $event->getParameter("fields");
80
81 self::updateChildPermission($primary, $data);
82
83 parent::onAfterAdd($event);
84 }
85
86 public static function onAfterUpdate(Event $event)
87 {
88 $primary = $event->getParameter("primary");
89 $data = $event->getParameter("fields");
90
91 self::updateChildPermission($primary, $data);
92
93 parent::onAfterUpdate($event);
94 }
95
96 protected static function updateChildPermission($primary, array $data)
97 {
98 $connection = static::getEntity()->getConnection();
99 $helper = $connection->getSqlHelper();
100
101 $data = static::loadUpdateRow($primary, $data);
102 if ((int) $data['VALUE'] === PermissionDictionary::VALUE_YES)
103 {
104 return;
105 }
106 $sql = "
107 UPDATE ". $helper->quote(static::getTableName()) ."
108 SET VALUE = ". PermissionDictionary::VALUE_NO ."
109 WHERE
110 ROLE_ID = ". $data['ROLE_ID'] ."
111 AND PERMISSION_ID LIKE '". $data['PERMISSION_ID'] .".%'
112 ";
113 $connection->query($sql);
114 }
115
116 protected static function loadUpdateRow($primary, array $data)
117 {
118 if (!static::checkDataFields($data))
119 {
120 $row = static::getRowById($primary);
121 foreach ($row as $k => $v)
122 {
123 if (!array_key_exists($k, $data))
124 {
125 $data[$k] = $v;
126 }
127 }
128 }
129 return $data;
130 }
131
132 protected static function validateRow(array $data): bool
133 {
134 if ((int) $data['VALUE'] === PermissionDictionary::VALUE_NO)
135 {
136 return true;
137 }
138
139 $parentPermissions = PermissionDictionary::getParentsPath($data['PERMISSION_ID']);
140 if (!$parentPermissions)
141 {
142 return true;
143 }
144
145 $res = static::getList([
146 'select' => ['VALUE'],
147 'filter' => [
148 '=ROLE_ID' => (int) $data['ROLE_ID'],
149 '%=PERMISSION_ID' => $parentPermissions,
151 ],
152 'limit' => 1
153 ])->fetchAll();
154
155 if (is_array($res) && !empty($res))
156 {
157 return false;
158 }
159
160 return true;
161 }
162
163 protected static function checkDataFields(array $data)
164 {
165 $fields = static::getMap();
166 foreach ($fields as $field)
167 {
168 if (!$field->hasParameter('required'))
169 {
170 continue;
171 }
172 if (
173 !array_key_exists($field->getName(), $data)
174 || !$data[$field->getName()]
175 )
176 {
177 return false;
178 }
179 }
180
181 return true;
182 }
183}
static updateMulti($primaries, $data, $ignoreEvents=false)
static checkFields(Result $result, $primary, array $data)
getParameter($key)
Definition event.php:80
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
isSuccess($internalCall=false)
Definition result.php:52
addError(Error $error)
Definition result.php:50