Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
scope.php
1<?php
2
4
10
11class Scope extends Base
12{
13 public const AJAX = 0b00000001;
14 public const REST = 0b00000010;
15 public const CLI = 0b00000100;
16 public const ALL = 0b00000111;
17
18 public const NOT_AJAX = self::ALL & ~self::AJAX;
19 public const NOT_REST = self::ALL & ~self::REST;
20 public const NOT_CLI = self::ALL & ~self::CLI;
21
22 private $scopes;
23
24 public function __construct($scopes)
25 {
26 $this->scopes = $scopes;
27 parent::__construct();
28 }
29
30 public function onBeforeAction(Event $event)
31 {
32 $scope = $this->getCurrentScope();
33 if (($this->scopes & $scope) === $scope)
34 {
35 return null;
36 }
37
38 $this->addError(new Error('Requested scope is invalid'));
39
40 return new EventResult(EventResult::ERROR, null, null, $this);
41 }
42
43 protected function getCurrentScope()
44 {
45 switch ($this->getAction()->getController()->getScope())
46 {
48 return static::AJAX;
50 return static::REST;
52 return static::CLI;
53 }
54
55 throw new ArgumentOutOfRangeException('Scope is invalid');
56 }
57}