Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
permissiondictionary.php
1<?php
10
12
15{
16 public const
19
20 public const DELIMITER = '.';
21
22 public const TYPE_TOGGLER = 'toggler';
23 public const TYPE_VARIABLES = 'variables';
24 public const TYPE_MULTIVARIABLES = 'multivariables';
25
26 public const HINT_PREFIX = 'HINT_';
27
28 protected static $locLoaded = [];
29
36 public static function getType($permissionId): string
37 {
38 return static::TYPE_TOGGLER;
39 }
40
50 public static function getPermission($permissionId): array
51 {
52 $permission = [
53 'id' => $permissionId,
54 'type' => static::TYPE_TOGGLER,
55 'title' => '',
56 'hint' => ''
57 ];
58
59 static::loadLoc();
60 $name = self::getName($permissionId);
61 if (!$name)
62 {
63 return $permission;
64 }
65 $permission['title'] = Loc::getMessage($name) ?? '';
66 $permission['hint'] = Loc::getMessage(self::HINT_PREFIX . $name) ?? '';
67
68 return $permission;
69 }
70
71 public static function getTitle($permissionId): string
72 {
73 static::loadLoc();
74 $name = self::getName($permissionId);
75 if (!$name)
76 {
77 return '';
78 }
79 return Loc::getMessage($name) ?? '';
80 }
81
82 public static function getList(): array
83 {
84 $class = new \ReflectionClass(static::class);
85 $permissions = $class->getConstants();
86
87 $res = [];
88 foreach ($permissions as $name => $id)
89 {
90 if (in_array($name, [
91 'VALUE_NO',
92 'VALUE_YES',
93 'DELIMITER',
94 'TYPE_VARIABLES',
95 'TYPE_MULTIVARIABLES',
96 'TYPE_TOGGLER',
97 'HINT_PREFIX'
98 ]))
99 {
100 continue;
101 }
102
103 $res[$id] = [
104 'NAME' => $name,
105 'LEVEL' => static::getLevel($id)
106 ];
107 }
108 return $res;
109 }
110
111 public static function getParentsPath(string $permissionId): ?string
112 {
113 $nodes = explode(static::DELIMITER, $permissionId);
114 $count = count($nodes);
115 if ($count < 2)
116 {
117 return null;
118 }
119 unset($nodes[$count-1]);
120 return implode(static::DELIMITER, $nodes);
121 }
122
123 public static function recursiveValidatePermission(array $permissions, $id)
124 {
125 if (!array_key_exists($id, $permissions))
126 {
127 return false;
128 }
129
130 if ($permissions[$id] == static::VALUE_NO)
131 {
132 return true;
133 }
134
135 $parentPath = static::getParentsPath($id);
136 if (!$parentPath)
137 {
138 return true;
139 }
140
141 if (!array_key_exists($parentPath, $permissions))
142 {
143 return false;
144 }
145
146 if ($permissions[$parentPath] == static::VALUE_NO)
147 {
148 return false;
149 }
150
151 return static::recursiveValidatePermission($permissions, $parentPath);
152 }
153
154 protected static function getLevel($id): int
155 {
156 $value = explode(static::DELIMITER, $id);
157 return count($value) - 1;
158 }
159
160 protected static function loadLoc()
161 {
162 if (!isset(static::$locLoaded[static::class]))
163 {
164 $r = new \ReflectionClass(static::class);
165 Loc::loadMessages($r->getFileName());
166 static::$locLoaded[static::class] = true;
167 }
168 }
169
170 protected static function getName($permissionId): ?string
171 {
172 $permissions = static::getList();
173 if (!array_key_exists($permissionId, $permissions))
174 {
175 return null;
176 }
177 return $permissions[$permissionId]['NAME'];
178 }
179}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29