Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
iblockrightschecker.php
1<?php
2
4
5use CIBlockElementRights;
6use CIBlockRights;
7use CIBlockSectionRights;
8
10{
11 private int $iblockId;
12 private array $elementsRights = [];
13 private array $sectionsRights = [];
14
15 public function __construct(int $iblockId)
16 {
17 $this->iblockId = $iblockId;
18 }
19
20 final protected function getIblockId(): int
21 {
22 return $this->iblockId;
23 }
24
70 public function preloadRights(array $elementIds, array $sectionIds): void
71 {
72 if (!empty($elementIds))
73 {
74 $rows = CIBlockElementRights::UserHasRightTo(
75 $this->getIblockId(),
76 $elementIds,
77 '',
78 CIBlockElementRights::RETURN_OPERATIONS
79 );
80 foreach ($rows as $elementId => $rights)
81 {
82 $this->elementsRights[$elementId] = $rights;
83 }
84 }
85
86 if (!empty($elementIds))
87 {
88 $rows = CIBlockSectionRights::UserHasRightTo(
89 $this->getIblockId(),
90 $sectionIds,
91 '',
92 CIBlockSectionRights::RETURN_OPERATIONS
93 );
94 foreach ($rows as $sectionId => $rights)
95 {
96 $this->sectionsRights[$sectionId] = $rights;
97 }
98 }
99 }
100
101 protected function checkElementRight(int $elementId, string $right): bool
102 {
103 $cachedRights = $this->elementsRights[$elementId] ?? null;
104 if (isset($cachedRights))
105 {
106 return isset($cachedRights[$right]);
107 }
108
109 return CIBlockElementRights::UserHasRightTo($this->getIblockId(), $elementId, $right);
110 }
111
112 protected function checkSectionRight(int $sectionId, string $right): bool
113 {
114 $cachedRights = $this->sectionsRights[$sectionId] ?? null;
115 if ($cachedRights !== null)
116 {
117 return isset($cachedRights[$right]);
118 }
119
120 return CIBlockSectionRights::UserHasRightTo($this->getIblockId(), $sectionId, $right);
121 }
122
123 #region public api
124
125 public function canAddElement(int $elementId): bool
126 {
127 return $this->checkElementRight($elementId, 'section_element_bind');
128 }
129
130 public function canEditElement(int $elementId): bool
131 {
132 return $this->checkElementRight($elementId, 'element_edit');
133 }
134
135 public function canEditElements(): bool
136 {
137 return CIBlockRights::UserHasRightTo($this->getIblockId(), $this->getIblockId(), 'element_edit');
138 }
139
140 public function canEditSection(int $sectionId): bool
141 {
142 return $this->checkSectionRight($sectionId, 'section_edit');
143 }
144
145 public function canDeleteElements(): bool
146 {
147 return CIBlockRights::UserHasRightTo($this->getIblockId(), $this->getIblockId(), 'element_delete');
148 }
149
150 public function canDeleteElement(int $elementId): bool
151 {
152 return $this->checkElementRight($elementId, 'element_delete');
153 }
154
155 public function canDeleteSection(int $sectionId): bool
156 {
157 return $this->checkSectionRight($sectionId, 'section_delete');
158 }
159
160 public function canBindElementToSection(int $sectionId): bool
161 {
162 return $this->checkSectionRight($sectionId, 'section_element_bind');
163 }
164
165 public function canBindSectionToSection(int $sectionId): bool
166 {
167 return $this->checkSectionRight($sectionId, 'section_section_bind');
168 }
169
170 #endregion public api
171}
preloadRights(array $elementIds, array $sectionIds)