Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
access.php
1<?php
2
4
17use Http\Request;
18
19Loc::loadMessages(__FILE__);
20
26class Access
27{
28 private static $list;
29
31 protected $user;
32
34 protected $permissions;
35
36 private static $instance;
37
38 protected const ACTION_VIEW = 'VIEW';
39
47 public static function current()
48 {
49 return new static(User::current());
50 }
51
59 private function __construct(User $user)
60 {
61 $this->user = $user;
62 self::registerEvent(EventDictionary::EVENT_ON_AFTER_CHECK);
63 $this->permissions = Permission::getByUserId($this->user->getId());
64 }
65
74 public static function getInstance($user = null)
75 {
76 if(is_null(self::$instance))
77 {
78 self::$instance = new self(!is_null($user) ? $user : User::current());
79 }
80 return self::$instance;
81 }
82
89 public function canViewAnything()
90 {
91 return (
92 $this->canViewLetters()
93 ||
94 $this->canViewAds()
95 ||
96 $this->canViewRc()
97 ||
98 $this->canViewTemplates()
99 ||
100 $this->canViewToloka()
101 ||
102 $this->canViewBlacklist()
103 ||
104 $this->canViewClientList()
105 ||
106 $this->canViewSegments()
107 ||
108 $this->canViewTemplates()
109 );
110 }
111
118 public function canModifySettings()
119 {
120 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_SETTINGS_EDIT);
121 }
122
129 public function canModifyTemplates()
130 {
131 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_TEMPLATE_EDIT);
132 }
133
140 public function canPauseStartStopLetter()
141 {
142 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_PAUSE_START_STOP);
143 }
144
151 public function canPauseStartStopAds()
152 {
153 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_PAUSE_START_STOP);
154 }
155
162 public function canPauseStartStopRc()
163 {
164 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_RC_PAUSE_START_STOP);
165 }
166
173 public function canViewLetters()
174 {
175 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_VIEW);
176 }
177
184 public function canViewTemplates()
185 {
186 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_TEMPLATE_VIEW);
187 }
188
195 public function canViewClientList()
196 {
197 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_SEGMENT_CLIENT_VIEW);
198 }
199
206 public function canModifyLetters()
207 {
208 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_EMAIL_EDIT)
209 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_AUDIO_CALL_EDIT)
210 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_INFO_CALL_EDIT)
211 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_SMS_EDIT)
212 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_MESSENGER_EDIT)
213 ;
214 }
215
223 public function canStopStartPause(string $letterClass)
224 {
225 $letterType = explode("\\", $letterClass);
226
227 switch ($letterType[count($letterType) - 1])
228 {
229 case 'Rc':
230 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_RC_PAUSE_START_STOP);
231 break;
232 case 'Ad':
233 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_PAUSE_START_STOP);
234 break;
235 default:
236 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_PAUSE_START_STOP);
237 }
238 }
239
246 public function canViewAds()
247 {
248 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_VIEW);
249 }
250
257 public function canModifyAds()
258 {
259 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_GOOGLE_EDIT)
260 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_YANDEX_EDIT)
261 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_FB_INSTAGRAM_EDIT)
262 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_VK_EDIT)
263 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_LOOK_ALIKE_VK_EDIT)
264 || AccessController::can($this->user->getId(), ActionDictionary::ACTION_ADS_LOOK_ALIKE_FB_EDIT)
265 ;
266 }
267
274 public function canViewRc()
275 {
277 {
278 return false;
279 }
280
281 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_RC_VIEW);
282 }
283
290 public function canViewToloka()
291 {
292 return Integration\Bitrix24\Service::isTolokaVisibleInRegion()
293 && AccessController::can($this->user->getId(),ActionDictionary::ACTION_RC_VIEW);
294 }
295
302 public function canModifyRc()
303 {
305 {
306 return false;
307 }
308
309 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_RC_EDIT);
310 }
311
318 public function canViewSegments()
319 {
320 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_SEGMENT_VIEW);
321 }
322
329 public function canViewSegmentContact()
330 {
331 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_SEGMENT_CLIENT_VIEW);
332 }
333
340 public function canModifySegments()
341 {
342 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_SEGMENT_EDIT);
343 }
344
351 public function canViewStart()
352 {
353 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_START_VIEW);
354 }
355
362 public function canViewBlacklist()
363 {
364 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_BLACKLIST_VIEW);
365 }
366
373 public function canModifyBlacklist()
374 {
375 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_BLACKLIST_EDIT);
376 }
377
378
385 public function canViewAbuses()
386 {
387 return AccessController::can($this->user->getId(), ActionDictionary::ACTION_MAILING_VIEW);
388 }
389
396 public function canModifyAbuses()
397 {
398 return !Integration\Bitrix24\Service::isCloud() && $this->canModifySegments();
399 }
400
408 public function canView($entityCode)
409 {
410 return $this->canPerform($entityCode, Permission::ACTION_VIEW);
411 }
412
420 public function canModify($entityCode)
421 {
422 return $this->canPerform($entityCode, Permission::ACTION_MODIFY);
423 }
424
433 public function canPerform($entityCode, $actionCode, $minPerm = null)
434 {
435 if ($this->user->canEdit())
436 {
437 return true;
438 }
439
440 if($actionCode === self::ACTION_VIEW)
441 return $this->user->canView();
442
443 return false;
444 }
445
446 private static function getSectionAndAction($action)
447 {
448 $actionMap = ActionDictionary::getLegacyMap();
449 $actionName = ActionDictionary::getActionName($action);
450 $sectionName = explode("_", $actionName)[0];
451 $sectionConst = constant(SectionDictionary::class."::".$sectionName);
452 $sectionMap = SectionDictionary::getLegacyMap();
453
454 return [$sectionMap[$sectionConst], $actionMap[$action]];
455 }
462 public static function handleEvent(Event $event)
463 {
464 $eventData = $event->getParameters();
465 $action = $eventData['action'];
466 [$sectionCode, $actionCode] = self::getSectionAndAction($action);
467
468 $instance = self::getInstance();
469 $eventResult = new EventResult(EventResult::SUCCESS);
470
471 try
472 {
473 $canAccess = $instance->canPerform($sectionCode, $actionCode);
474 } catch (ArgumentException $e)
475 {
476 return $eventResult->forbidAccess();
477 }
478
479 if($canAccess)
480 {
481 return $eventResult->allowAccess();
482 }
483
484 return $eventResult->forbidAccess();
485 }
486
491 public static function registerEvent($eventName, array $filter = [])
492 {
493 if(empty(static::$list[$eventName]))
494 {
495 EventManager::getInstance()->addEventHandler(
496 AccessController::class,
498 array(__CLASS__, 'handleEvent'),
499 false,
500 1);
501 }
502
503 static::$list[$eventName][] = $filter;
504 }
505}
static loadMessages($file)
Definition loc.php:64
static isModuleInstalled($moduleName)
static getInstance($user=null)
Definition access.php:74
canPerform($entityCode, $actionCode, $minPerm=null)
Definition access.php:433
canStopStartPause(string $letterClass)
Definition access.php:223
static registerEvent($eventName, array $filter=[])
Definition access.php:491
static handleEvent(Event $event)
Definition access.php:462