Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
holdentity.php
1<?php
2
4
11
17{
18 public const TYPE_APP = 'A';
19 public const TYPE_WEBHOOK = 'W';
20 private const NOTIFICATION_CODE = 'HOLD_REST_OVERLOAD';
21 private const OPTION_CODE = 'hold_access_entity';
22 private const MODULE_ID = 'rest';
23
31 public static function add(string $type, string $code) : array
32 {
33 $result = [
34 'success' => false
35 ];
36
37 $data = static::get();
38 if (!is_array($data[$type]) || !in_array($code, $data[$type]))
39 {
40 $data[$type][] = $code;
41 $result['success'] = static::set($data);
42 if ($result['success'])
43 {
44 $url = static::getUrl($type, $code);
45 Notification::set(static::NOTIFICATION_CODE, $url);
46 }
47 }
48
49 return $result;
50 }
51
52 private static function getUrl(string $type, string $code) : string
53 {
54 $url = '';
55 $filter = [];
56 if ($type === static::TYPE_APP)
57 {
58 $app = AppTable::getByClientId($code);
59 if ($app)
60 {
61 if ($app['STATUS'] !== AppTable::STATUS_LOCAL)
62 {
63 $url = \Bitrix\Rest\Marketplace\Url::getApplicationDetailUrl($app['CODE']);
64 }
65 else
66 {
67 $filter['=APP_ID'] = $app['ID'];
68 }
69 }
70 }
71 elseif ($type === static::TYPE_WEBHOOK)
72 {
73 $res = PasswordTable::getList(
74 [
75 'filter' => [
76 '=PASSWORD' => $code,
77 ],
78 'select' => [
79 'ID',
80 ],
81 'limit' => 1,
82 ]
83 );
84 if ($password = $res->fetch())
85 {
86 $filter['=PASSWORD_ID'] = $password['ID'];
87 }
88 }
89 if (!empty($filter))
90 {
91 $res = IntegrationTable::getList(
92 [
93 'filter' => $filter,
94 'select' => [
95 'ID',
96 'ELEMENT_CODE',
97 ],
98 'limit' => 1,
99 ]
100 );
101 if ($item = $res->fetch())
102 {
103 $url = \Bitrix\Rest\Url\DevOps::getInstance()->getIntegrationEditUrl($item['ID'], $item['ELEMENT_CODE']);
104 }
105 }
106
107 return $url;
108 }
109
117 public static function delete(string $type, string $code) : array
118 {
119 $result = [
120 'success' => false
121 ];
122 $data = static::get();
123 if (is_array($data[$type]))
124 {
125 $key = array_search($code, $data[$type]);
126 if ($key !== false)
127 {
128 if (count($data[$type]) === 1)
129 {
130 unset($data[$type]);
131 }
132 else
133 {
134 unset($data[$type][$key]);
135 }
136 $result['success'] = static::set($data);
137 }
138 }
139
140 return $result;
141 }
142
150 public static function is(string $type, string $code) : bool
151 {
152 $list = static::get();
153 return isset($list[$type]) && in_array($code, $list[$type]);
154 }
155
160 public static function reset() : bool
161 {
162 return static::set([]);
163 }
164
169 public static function get() : array
170 {
171 $option = Option::get(static::MODULE_ID, static::OPTION_CODE, false);
172
173 return $option ? Json::decode($option) : [];
174 }
175
181 private static function set(array $data) : bool
182 {
183 if (!empty($data))
184 {
185 Option::set(static::MODULE_ID, static::OPTION_CODE, Json::encode($data));
186 }
187 else
188 {
189 Option::delete(static::MODULE_ID, ['name' => static::OPTION_CODE]);
191 }
192
193 return true;
194 }
195}
static getByClientId($clientId)
Definition app.php:929
const STATUS_LOCAL
Definition app.php:74
static add(string $type, string $code)
static is(string $type, string $code)
static set(string $code, string $url='')