Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
access.php
1<?php
2
4
13
18class Access
19{
20 public const ENTITY_TYPE_APP = 'app';
21 public const ENTITY_TYPE_APP_STATUS = 'status';
22 public const ENTITY_TYPE_INTEGRATION = 'integration';
23 public const ENTITY_TYPE_AP_CONNECT = 'ap_connect';
24 public const ENTITY_TYPE_WEBHOOK = 'webhook';
25 public const ENTITY_COUNT = 'count';
26
27 public const ACTION_INSTALL = 'install';
28 public const ACTION_OPEN = 'open';
29 public const ACTION_BUY = 'buy';
30
31 public const MODULE_ID = 'rest';
32 public const OPTION_ACCESS_ACTIVE = 'access_active';
33 public const OPTION_AVAILABLE_COUNT = 'app_available_count';
34 public const OPTION_SUBSCRIPTION_AVAILABLE = 'subscription_available';
35 private const OPTION_APP_USAGE_LIST = 'app_usage_list';
36 private const OPTION_REST_UNLIMITED_FINISH = 'rest_unlimited_finish';
37 private const OPTION_HOLD_CHECK_COUNT_APP = '~hold_check_count_app';
38 private const DEFAULT_AVAILABLE_COUNT = -1;
39 private const DEFAULT_AVAILABLE_COUNT_DEMO = 10;
40
41 private static $availableApp = [];
42 private static $availableAppCount = [];
43
47 public static function isFeatureEnabled()
48 {
49 return
51 || (
52 Loader::includeModule('bitrix24')
53 && \Bitrix\Bitrix24\Feature::isFeatureEnabled('rest_access')
54 )
55 ;
56 }
57
65 public static function isAvailable($app = '') : bool
66 {
67 if (!static::isActiveRules())
68 {
69 return true;
70 }
71
72 if (!array_key_exists($app, static::$availableApp))
73 {
74 static::$availableApp[$app] = false;
75 if (Client::isSubscriptionAvailable())
76 {
77 static::$availableApp[$app] = true;
78 }
79 elseif (static::isFeatureEnabled())
80 {
81 static::$availableApp[$app] = true;
82 }
83 elseif ($app !== '')
84 {
85 if (in_array($app, Immune::getList(), true))
86 {
87 static::$availableApp[$app] = true;
88 }
89 else
90 {
91 $appInfo = AppTable::getByClientId($app);
92 if ($appInfo['CODE'] && in_array($appInfo['CODE'], Immune::getList(), true))
93 {
94 static::$availableApp[$app] = true;
95 }
96 }
97 }
98 }
99
100 return static::$availableApp[$app];
101 }
102
110 public static function isAvailableCount(string $entityType, $entity = 0) : bool
111 {
112 if (!static::isActiveRules())
113 {
114 return true;
115 }
116
117 $key = $entityType . $entity;
118 if (!array_key_exists($key, static::$availableAppCount))
119 {
120 static::$availableAppCount[$key] = true;
121 if ($entityType === static::ENTITY_TYPE_APP)
122 {
123 $maxCount = static::getAvailableCount();
124 if ($maxCount >= 0)
125 {
126 $appInfo = AppTable::getByClientId($entity);
127 if (!isset($appInfo['STATUS']) || $appInfo['STATUS'] !== AppTable::STATUS_LOCAL)
128 {
129 if (isset($appInfo['CODE']) && $appInfo['CODE'])
130 {
131 $entity = $appInfo['CODE'];
132 }
133
134 $entityList = static::getActiveEntity(true);
135 if ($entityList[static::ENTITY_COUNT] > $maxCount)
136 {
137 static::$availableAppCount[$key] = false;
138 }
139 elseif (
140 $entityList[static::ENTITY_COUNT] === $maxCount
141 && !in_array($entity, $entityList[$entityType], true)
142 )
143 {
144 static::$availableAppCount[$key] = false;
145 }
146
147 if (
148 static::$availableAppCount[$key] === false
149 && (
150 in_array($entity, Immune::getList(), true)
151 || (
152 !static::needCheckCount()
153 && in_array($entity, $entityList[$entityType], true)
154 )
155 )
156 )
157 {
158 static::$availableAppCount[$key] = true;
159 }
160 }
161 }
162 }
163 }
164
165 return static::$availableAppCount[$key];
166 }
167
172 public static function getAvailableCount() : int
173 {
174 $result = -1;
175 $subscriptionActive = Client::isSubscriptionAvailable();
176 if (!$subscriptionActive)
177 {
178 $restUnlimitedFinish = false;
179 $count = static::DEFAULT_AVAILABLE_COUNT;
180 if (Loader::includeModule('bitrix24'))
181 {
182 if (Client::isSubscriptionAccess())
183 {
184 $restUnlimitedFinish = Option::get(static::MODULE_ID, static::OPTION_REST_UNLIMITED_FINISH, null);
185 $count = (int) \Bitrix\Bitrix24\Feature::getVariable('rest_no_subscribe_access_limit');
186 if (\CBitrix24::getLicensePrefix() === 'ua')
187 {
188 $count = -1;
189 }
190 }
191 }
192 else
193 {
194 $count = (int) Option::get(
195 static::MODULE_ID,
196 static::OPTION_AVAILABLE_COUNT,
197 static::DEFAULT_AVAILABLE_COUNT
198 );
199 }
200 if (
201 (!$restUnlimitedFinish || $restUnlimitedFinish < time())
202 && $count >= 0
203 )
204 {
205 $result = $count;
206 }
207 }
208
209 return $result;
210 }
211
212 public static function getActiveEntity($force = false)
213 {
214 $option = Option::get(static::MODULE_ID, static::OPTION_APP_USAGE_LIST, null);
215 if ($force || is_null($option))
216 {
217 $result = static::calcUsageEntity();
218 Option::set(static::MODULE_ID, static::OPTION_APP_USAGE_LIST, Json::encode($result));
219 }
220 else
221 {
222 try
223 {
224 $result = Json::decode($option);
225 }
226 catch (\Exception $exception)
227 {
228 $result = [];
229 }
230 if (!is_array($result))
231 {
232 $result = [];
233 }
234 }
235
236 return $result;
237 }
238
239 private static function calcUsageEntity()
240 {
241 $result = [
242 static::ENTITY_TYPE_APP => [],
243 static::ENTITY_TYPE_APP_STATUS => [],
244 static::ENTITY_COUNT => 0
245 ];
246 $immuneList = Immune::getList();
247
248 $res = AppTable::getList(
249 [
250 'filter' => [
251 '=ACTIVE' => AppTable::ACTIVE,
252 ],
253 'select' => [
254 'CODE',
255 'STATUS',
256 ],
257 ]
258 );
259 while ($item = $res->fetch())
260 {
261 if (!in_array($item['CODE'], $immuneList, true))
262 {
263 if (!isset($result[static::ENTITY_TYPE_APP_STATUS][$item['STATUS']]))
264 {
265 $result[static::ENTITY_TYPE_APP_STATUS][$item['STATUS']] = 0;
266 }
267 $result[static::ENTITY_TYPE_APP_STATUS][$item['STATUS']]++;
268
269 if ($item['STATUS'] === AppTable::STATUS_LOCAL)
270 {
271 $result[static::ENTITY_TYPE_APP][] = $item['CODE'];
272 }
273
274 if ($item['STATUS'] === AppTable::STATUS_FREE)
275 {
276 $result[static::ENTITY_TYPE_APP][] = $item['CODE'];
277 $result[static::ENTITY_COUNT]++;
278 }
279 }
280 }
281
282 return $result;
283 }
284
292 public static function getHelperCode($action = '', $entityType = '', $entityData = []) : string
293 {
294 if ($action === static::ACTION_BUY)
295 {
296 return 'limit_subscription_market_trial_access';
297 }
298
299 if ($entityType === static::ENTITY_TYPE_APP && !is_array($entityData))
300 {
301 $entityData = AppTable::getByClientId($entityData);
302 }
303
304 $code = '';
305 $dateFinish = Client::getSubscriptionFinalDate();
306 $entity = static::getActiveEntity();
307 $maxCount = static::getAvailableCount();
308 $isB24 = ModuleManager::isModuleInstalled('bitrix24') && Loader::includeModule('bitrix24');
309 $isSubscriptionFinished = $dateFinish && $dateFinish < (new Date());
310 $isSubscriptionAccess = Client::isSubscriptionAccess();
311 $isSubscriptionDemoAvailable = Client::isSubscriptionDemoAvailable() && !$dateFinish;
312 $isSubscriptionAvailable = Client::isSubscriptionAvailable();
313 $canBuySubscription = Client::canBuySubscription();
314 $isDemoSubscription = Client::isSubscriptionDemo();
315 $isCanInstallInDemo = true;
316 if (
317 !empty($entityData['HOLD_INSTALL_BY_TRIAL'])
318 && $entityData['HOLD_INSTALL_BY_TRIAL'] === 'Y'
319 )
320 {
321 $isCanInstallInDemo = false;
322 }
323
324 $license = $isB24 ? \CBitrix24::getLicenseFamily() : '';
325 $isDemo = $license === 'demo';
326 $isMinLicense = $isB24 && mb_strpos($license, 'project') === 0;
327 $isMaxLicense = $isB24 && ($license === 'ent' || $license === 'pro' || mb_strpos($license, 'company') === 0);
328
329 $isMaxApplication = false;
330 if ($maxCount >= 0 && $entity[static::ENTITY_COUNT] >= $maxCount)
331 {
332 $isMaxApplication = true;
333 }
334
335 $isMaxApplicationDemo = false;
336 if ($entity[static::ENTITY_COUNT] >= static::DEFAULT_AVAILABLE_COUNT_DEMO)
337 {
338 $isMaxApplicationDemo = true;
339 }
340
341 $hasPaidApplication = false;
342 if (
343 $entity[static::ENTITY_TYPE_APP_STATUS][AppTable::STATUS_PAID] > 0
344 || $entity[static::ENTITY_TYPE_APP_STATUS][AppTable::STATUS_SUBSCRIPTION] > 0
345 )
346 {
347 $hasPaidApplication = true;
348 }
349
350 $isFreeEntity = false;
351 if ($entityType === static::ENTITY_TYPE_INTEGRATION || $entityType === static::ENTITY_TYPE_AP_CONNECT)
352 {
353 $isFreeEntity = true;
354 }
355 elseif (!empty($entityData))
356 {
357 if (
358 $entityData['ID'] > 0
359 && (isset($entityData['ACTIVE']) && $entityData['ACTIVE'])
360 && (
361 $entityData['STATUS'] === AppTable::STATUS_FREE
362 || $entityData['STATUS'] === AppTable::STATUS_LOCAL
363 )
364 )
365 {
366 $isFreeEntity = true;
367 }
368 elseif (
369 (
370 !isset($entityData['ACTIVE'])
371 || !$entityData['ACTIVE']
372 )
373 && !(
374 $entityData['BY_SUBSCRIPTION'] === 'Y'
375 || ($entityData['FREE'] === 'N' && !empty($entityData['PRICE']))
376 )
377 )
378 {
379 $isFreeEntity = true;
380 }
381 }
382
383 $isUsedDemoLicense = false;
384 if ($isB24 && (int) Option::get('bitrix24', 'DEMO_START', 0) > 0)
385 {
386 $isUsedDemoLicense = true;
387 }
388
389 if (!static::isActiveRules())
390 {
391 if (
392 !empty($entityData)
393 && (
394 $entityData['BY_SUBSCRIPTION'] === 'Y'
395 || ($entityData['ID'] > 0 && $entityData['STATUS'] === AppTable::STATUS_SUBSCRIPTION)
396 )
397 )
398 {
399 if ($isSubscriptionDemoAvailable)
400 {
401 // activate demo subscription
402 $code = 'limit_subscription_market_access';
403 }
404 elseif ($isB24 && $isDemo)
405 {
406 // choose license with subscription
407 $code = 'limit_subscription_market_tarifwithmarket';
408 }
409 else
410 {
411 // choose subscription
412 $code = 'limit_subscription_market_marketpaid';
413 }
414 }
415 }
416 elseif (!$isSubscriptionAccess)
417 {
418 if ($isMinLicense)
419 {
420 if ($isUsedDemoLicense)
421 {
422 $code = 'limit_free_rest_hold_no_demo';
423 }
424 elseif ($entityType === static::ENTITY_TYPE_AP_CONNECT)
425 {
426 $code = 'limit_market_bus';
427 }
428 else
429 {
430 $code = 'limit_free_rest_hold';
431 }
432 }
433 }
434 elseif (!static::isAvailable())
435 {
436 if ($hasPaidApplication || !$isFreeEntity)
437 {
438 if ($isSubscriptionDemoAvailable)
439 {
440 // activate demo subscription
441 $code = 'limit_subscription_market_access';
442 }
443 elseif (!$isB24)
444 {
445 // choose subscription
446 $code = 'plus_need_trial';
447 }
448 else
449 {
450 // choose license with subscription
451 $code = 'limit_subscription_market_tarifwithmarket';
452 if ($action === static::ACTION_OPEN)
453 {
454 $code = 'installed_plus_buy_license_with_plus';
455 }
456 }
457 }
458 elseif ($isB24 && !$isUsedDemoLicense)
459 {
460 // activate demo license
461 if ($entityType === static::ENTITY_TYPE_AP_CONNECT)
462 {
463 $code = 'limit_market_bus';
464 }
465 else
466 {
467 $code = 'limit_free_rest_hold';
468 }
469 }
470 elseif ($isB24 && !$isMaxApplicationDemo)
471 {
472 // choose license
473 $code = 'limit_free_rest_hold_no_demo';
474 }
475 elseif ($isSubscriptionDemoAvailable)
476 {
477 // activate demo subscription
478 $code = 'limit_subscription_market_marketpaid';
479 }
480 elseif ($isB24 && $isSubscriptionAccess)
481 {
482 // choose license with subscription
483 $code = 'limit_subscription_market_tarifwithmarket';
484 if ($action === static::ACTION_OPEN)
485 {
486 $code = 'limit_free_apps_buy_license_with_plus';
487 }
488 }
489 elseif (!$isB24 && $isSubscriptionAccess)
490 {
491 // choose subscription
492 $code = 'plus_need_trial';
493 }
494 }
495 elseif ($isB24 && !$isDemo && $isMaxApplication && $isFreeEntity && !$isMaxLicense)
496 {
497 if (!$isUsedDemoLicense)
498 {
499 // activate demo license
500 if ($entityType === static::ENTITY_TYPE_AP_CONNECT)
501 {
502 $code = 'limit_market_bus';
503 }
504 else
505 {
506 $code = 'limit_free_apps_need_demo';
507 }
508 }
509 else
510 {
511 // choose license
512 $code = 'limit_free_apps_buy_license';
513 }
514 }
515 elseif (
516 $isSubscriptionDemoAvailable
517 && $isCanInstallInDemo
518 && ($hasPaidApplication || $isMaxApplication || !$isFreeEntity)
519 )
520 {
521 if (!$isFreeEntity)
522 {
523 // activate demo subscription
524 $code = 'limit_subscription_market_access_buy_marketplus';
525 }
526 elseif ($isB24 && $isDemo)
527 {
528 // activate demo subscription
529 $code = 'limit_subscription_market_marketpaid';
530 }
531 else
532 {
533 // activate demo subscription
534 $code = 'limit_subscription_market_access';
535 }
536 }
537 elseif ($isDemoSubscription && !$isCanInstallInDemo)
538 {
539 // only paid subscription app
540 $code = 'subscription_market_paid_access';
541 }
542 elseif ($canBuySubscription)
543 {
544 if ($isSubscriptionFinished)
545 {
546 // choose subscription
547 $code = 'limit_subscription_market_access_buy_marketplus';
548 }
549 else
550 {
551 // choose new subscription
552 $code = 'plus_need_trial';
553 }
554 }
555 elseif ($isB24 && $isDemo)
556 {
557 if (!$isSubscriptionDemoAvailable)
558 {
559 // choose license with subscription
560 $code = 'limit_subscription_market_tarifwithmarket';
561 }
562 else
563 {
564 // activate demo subscription
565 $code = 'limit_subscription_market_access';
566 }
567 }
568 elseif (!$isSubscriptionAvailable)
569 {
570 $code = 'limit_free_rest_hold_no_demo';
571 }
572
573 return $code;
574 }
575
579 public static function resetToFree()
580 {
581 static::reset();
582 Option::delete(static::MODULE_ID, ['name' => static::OPTION_HOLD_CHECK_COUNT_APP]);
583 \Bitrix\Rest\Marketplace\Notification::setLastCheckTimestamp(time());
584
585 return true;
586 }
587
591 public static function onBitrix24LicenseChange($licenseType)
592 {
593 static::reset();
594 if (
595 !Client::isSubscriptionAccess()
596 && Loader::includeModule('bitrix24')
597 && in_array($licenseType, \CBitrix24::PAID_EDITIONS, true)
598 )
599 {
600 Option::set(static::MODULE_ID, static::OPTION_HOLD_CHECK_COUNT_APP, 'Y');
601 }
602 }
603
607 public static function needCheckCount()
608 {
609 if (!static::isActiveRules())
610 {
611 return false;
612 }
613
614 return Option::get(static::MODULE_ID, static::OPTION_HOLD_CHECK_COUNT_APP, 'N') === 'N';
615 }
616
620 public static function isActiveRules()
621 {
622 return
624 || Option::get(static::MODULE_ID, static::OPTION_ACCESS_ACTIVE, 'N') === 'Y'
625 ;
626 }
627
635 public static function calcUsageEntityAgent($period = false)
636 {
637 static::getActiveEntity(true);
638 return $period === true ? '\Bitrix\Rest\Engine\Access::calcUsageEntityAgent(true);' : '';
639 }
640
645 public static function reset() : bool
646 {
647 static::$availableApp = [];
648 static::$availableAppCount = [];
649
650 return true;
651 }
652}
static includeModule($moduleName)
Definition loader.php:69
static isModuleInstalled($moduleName)
static isAvailableCount(string $entityType, $entity=0)
Definition access.php:110
static calcUsageEntityAgent($period=false)
Definition access.php:635
static getHelperCode($action='', $entityType='', $entityData=[])
Definition access.php:292
static getActiveEntity($force=false)
Definition access.php:212
const OPTION_SUBSCRIPTION_AVAILABLE
Definition access.php:34
static isAvailable($app='')
Definition access.php:65
static onBitrix24LicenseChange($licenseType)
Definition access.php:591