1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
access.php
См. документацию.
1<?php
2
4
16
21class Access
22{
23 public const ENTITY_TYPE_APP = 'app';
24 public const ENTITY_TYPE_APP_STATUS = 'status';
25 public const ENTITY_TYPE_INTEGRATION = 'integration';
26 public const ENTITY_TYPE_AP_CONNECT = 'ap_connect';
27 public const ENTITY_TYPE_WEBHOOK = 'webhook';
28 public const ENTITY_COUNT = 'count';
29
30 public const ACTION_INSTALL = 'install';
31 public const ACTION_OPEN = 'open';
32 public const ACTION_BUY = 'buy';
33
34 public const MODULE_ID = 'rest';
35 public const OPTION_ACCESS_ACTIVE = 'access_active';
36 public const OPTION_AVAILABLE_COUNT = 'app_available_count';
37 public const OPTION_SUBSCRIPTION_AVAILABLE = 'subscription_available';
38 private const OPTION_APP_USAGE_LIST = 'app_usage_list';
39 private const OPTION_REST_UNLIMITED_FINISH = 'rest_unlimited_finish';
40 private const OPTION_HOLD_CHECK_COUNT_APP = '~hold_check_count_app';
41 private const DEFAULT_AVAILABLE_COUNT = -1;
42 private const DEFAULT_AVAILABLE_COUNT_DEMO = 10;
43 private const SYSTEM_METHODS = ['crm.automation.trigger'];
44
45 private static $availableApp = [];
46 private static $availableAppCount = [];
47
51 public static function isFeatureEnabled()
52 {
53 return
55 || (
56 Loader::includeModule('bitrix24')
57 && \Bitrix\Bitrix24\Feature::isFeatureEnabled('rest_access')
58 )
59 ;
60 }
61
69 public static function isAvailable($app = '') : bool
70 {
71 if (!static::isActiveRules())
72 {
73 return true;
74 }
75
76 if (!array_key_exists($app, static::$availableApp))
77 {
78 static::$availableApp[$app] = false;
79 if (Client::isSubscriptionAvailable())
80 {
81 static::$availableApp[$app] = true;
82 }
83 elseif (
84 !MarketSubscription::createByDefault()->isRequiredSubscriptionModelStarted()
85 && self::isFeatureEnabled()
86 )
87 {
88 static::$availableApp[$app] = true;
89 }
90 elseif ($app !== '')
91 {
92 if (in_array($app, Immune::getList(), true))
93 {
94 static::$availableApp[$app] = true;
95 }
96 else
97 {
98 $appInfo = AppTable::getByClientId($app);
99 if ($appInfo['CODE'] && in_array($appInfo['CODE'], Immune::getList(), true))
100 {
101 static::$availableApp[$app] = true;
102 }
103 elseif ($appInfo['STATUS'] === AppTable::STATUS_FREE && self::isFeatureEnabled())
104 {
105 static::$availableApp[$app] = true;
106 }
107 }
108 }
109 }
110
111 return static::$availableApp[$app];
112 }
113
114 public static function canInstallApp(array $installAppData): bool
115 {
116 $appCode = $installAppData['CODE'] ?? null;
117 if (!$appCode)
118 {
119 return false;
120 }
121
122 return static::isAvailable($appCode)
123 && static::isAvailableCount(static::ENTITY_TYPE_APP, $appCode)
124 || static::isAllowFreeApp($installAppData);
125 }
126
127 public static function isAllowFreeApp(array $freeAppData): bool
128 {
129 $isFreeApp = ($freeAppData['FREE'] ?? 'N') === 'Y';
130
131 return $isFreeApp
132 && self::isFeatureEnabled();
133 }
134
135 public static function isAvailableAPAuthByPasswordId(int $passwordId): bool
136 {
137 if (!ModuleManager::isModuleInstalled('bitrix24'))
138 {
139 return true;
140 }
141
142 if (Client::isSubscriptionAvailable())
143 {
144 return true;
145 }
146
147 if (self::isFeatureEnabled() && !MarketSubscription::createByDefault()->isRequiredSubscriptionModelStarted())
148 {
149 return true;
150 }
151
152 if (
153 self::isFeatureEnabled()
154 && ServiceContainer::getInstance()->getAPAuthPasswordService()->isSystemPasswordById($passwordId)
155 && (!\CRestServer::instance() || in_array(\CRestServer::instance()?->getMethod(), static::SYSTEM_METHODS, true))
156 )
157 {
158 return true;
159 }
160
161 return false;
162 }
163
171 public static function isAvailableCount(string $entityType, $entity = 0) : bool
172 {
173 if (!static::isActiveRules())
174 {
175 return true;
176 }
177
178 $key = $entityType . $entity;
179 if (!array_key_exists($key, static::$availableAppCount))
180 {
181 static::$availableAppCount[$key] = true;
182 if ($entityType === static::ENTITY_TYPE_APP)
183 {
184 $maxCount = static::getAvailableCount();
185 if ($maxCount >= 0)
186 {
188 if (!isset($appInfo['STATUS']) || $appInfo['STATUS'] !== AppTable::STATUS_LOCAL)
189 {
190 if (isset($appInfo['CODE']) && $appInfo['CODE'])
191 {
192 $entity = $appInfo['CODE'];
193 }
194
195 $entityList = static::getActiveEntity(true);
196 if ($entityList[static::ENTITY_COUNT] > $maxCount)
197 {
198 static::$availableAppCount[$key] = false;
199 }
200 elseif (
201 $entityList[static::ENTITY_COUNT] === $maxCount
202 && !in_array($entity, $entityList[$entityType], true)
203 )
204 {
205 static::$availableAppCount[$key] = false;
206 }
207
208 if (
209 static::$availableAppCount[$key] === false
210 && (
211 in_array($entity, Immune::getList(), true)
212 || (
213 !static::needCheckCount()
214 && in_array($entity, $entityList[$entityType], true)
215 )
216 )
217 )
218 {
219 static::$availableAppCount[$key] = true;
220 }
221 }
222 }
223 }
224 }
225
226 return static::$availableAppCount[$key];
227 }
228
233 public static function getAvailableCount() : int
234 {
235 $result = -1;
236 $subscriptionActive = Client::isSubscriptionAvailable();
237 if (!$subscriptionActive)
238 {
239 $restUnlimitedFinish = false;
240 $count = static::DEFAULT_AVAILABLE_COUNT;
241 if (Loader::includeModule('bitrix24'))
242 {
243 if (Client::isSubscriptionAccess())
244 {
245 $restUnlimitedFinish = Option::get(static::MODULE_ID, static::OPTION_REST_UNLIMITED_FINISH, null);
246 $count = (int) \Bitrix\Bitrix24\Feature::getVariable('rest_no_subscribe_access_limit');
247 if (\CBitrix24::getLicensePrefix() === 'ua')
248 {
249 $count = -1;
250 }
251 }
252 }
253 else
254 {
255 $count = (int) Option::get(
256 static::MODULE_ID,
257 static::OPTION_AVAILABLE_COUNT,
258 static::DEFAULT_AVAILABLE_COUNT
259 );
260 }
261 if (
262 (!$restUnlimitedFinish || $restUnlimitedFinish < time())
263 && $count >= 0
264 )
265 {
266 $result = $count;
267 }
268 }
269
270 return $result;
271 }
272
273 public static function getActiveEntity($force = false)
274 {
275 $option = Option::get(static::MODULE_ID, static::OPTION_APP_USAGE_LIST, null);
276 if ($force || is_null($option))
277 {
278 $result = static::calcUsageEntity();
279 Option::set(static::MODULE_ID, static::OPTION_APP_USAGE_LIST, Json::encode($result));
280 }
281 else
282 {
283 try
284 {
285 $result = Json::decode($option);
286 }
287 catch (\Exception $exception)
288 {
289 $result = [];
290 }
291 if (!is_array($result))
292 {
293 $result = [];
294 }
295 }
296
297 return $result;
298 }
299
300 private static function calcUsageEntity()
301 {
302 $result = [
303 static::ENTITY_TYPE_APP => [],
304 static::ENTITY_TYPE_APP_STATUS => [
311 ],
312 static::ENTITY_COUNT => 0
313 ];
314 $immuneList = Immune::getList();
315
316 $res = AppTable::getList(
317 [
318 'filter' => [
319 '=ACTIVE' => AppTable::ACTIVE,
320 ],
321 'select' => [
322 'CODE',
323 'STATUS',
324 ],
325 ]
326 );
327 while ($item = $res->fetch())
328 {
329 if (!in_array($item['CODE'], $immuneList, true))
330 {
331 if (!isset($result[static::ENTITY_TYPE_APP_STATUS][$item['STATUS']]))
332 {
333 $result[static::ENTITY_TYPE_APP_STATUS][$item['STATUS']] = 0;
334 }
335 $result[static::ENTITY_TYPE_APP_STATUS][$item['STATUS']]++;
336
337 if ($item['STATUS'] === AppTable::STATUS_LOCAL)
338 {
339 $result[static::ENTITY_TYPE_APP][] = $item['CODE'];
340 }
341
342 if ($item['STATUS'] === AppTable::STATUS_FREE)
343 {
344 $result[static::ENTITY_TYPE_APP][] = $item['CODE'];
345 $result[static::ENTITY_COUNT]++;
346 }
347 }
348 }
349
350 return $result;
351 }
352
360 public static function getHelperCode($action = '', $entityType = '', $entityData = []) : string
361 {
362 $isB24 = ModuleManager::isModuleInstalled('bitrix24') && Loader::includeModule('bitrix24');
363 $dateFinish = Client::getSubscriptionFinalDate();
364 $isSubscriptionDemoAvailable = Client::isSubscriptionDemoAvailable();
365 $region = Application::getInstance()->getLicense()->getRegion();
366
367 if ($action === static::ACTION_BUY)
368 {
369 if (
370 $isB24
371 && $isSubscriptionDemoAvailable
372 && \CBitrix24::isLicenseNeverPayed()
373 && $region === 'ru'
374 )
375 {
376 return 'limit_market_trial_demo';
377 }
378
379 if ($isB24 && Client::isSubscriptionDemo() && !Client::canBuySubscription())
380 {
381 return 'limit_subscription_market_bundle';
382 }
383
384 return 'limit_subscription_market_trial_access';
385 }
386
387 if ($entityType === static::ENTITY_TYPE_APP && !is_array($entityData))
388 {
389 $entityData = AppTable::getByClientId($entityData);
390 }
391
392 $code = '';
393 $entity = static::getActiveEntity();
394 $maxCount = static::getAvailableCount();
395 $isSubscriptionFinished = $dateFinish && $dateFinish < (new Date());
396 $isSubscriptionAccess = Client::isSubscriptionAccess();
397 $isSubscriptionAvailable = Client::isSubscriptionAvailable();
398 $canBuySubscription = Client::canBuySubscription();
399 $isDemoSubscription = Client::isSubscriptionDemo();
400 $isCanInstallInDemo = true;
401 if (
402 !empty($entityData['HOLD_INSTALL_BY_TRIAL'])
403 && $entityData['HOLD_INSTALL_BY_TRIAL'] === 'Y'
404 )
405 {
406 $isCanInstallInDemo = false;
407 }
408
409 $license = $isB24 ? \CBitrix24::getLicenseFamily() : '';
410 $isDemo = $license === 'demo';
411 $isMinLicense = $isB24 && mb_strpos($license, 'project') === 0;
412 $isMaxLicense = $isB24 && ($license === 'ent' || $license === 'pro' || mb_strpos($license, 'company') === 0);
413
414 $isMaxApplication = false;
415 if ($maxCount >= 0 && $entity[static::ENTITY_COUNT] >= $maxCount)
416 {
417 $isMaxApplication = true;
418 }
419
420 $isMaxApplicationDemo = false;
421 if ($entity[static::ENTITY_COUNT] >= static::DEFAULT_AVAILABLE_COUNT_DEMO)
422 {
423 $isMaxApplicationDemo = true;
424 }
425
426 $hasPaidApplication = false;
427 if (
428 (
429 isset($entity[static::ENTITY_TYPE_APP_STATUS][AppTable::STATUS_PAID])
430 && $entity[static::ENTITY_TYPE_APP_STATUS][AppTable::STATUS_PAID] > 0
431 )
432 || (
433 isset($entity[static::ENTITY_TYPE_APP_STATUS][AppTable::STATUS_SUBSCRIPTION])
434 && $entity[static::ENTITY_TYPE_APP_STATUS][AppTable::STATUS_SUBSCRIPTION] > 0
435 )
436 )
437 {
438 $hasPaidApplication = true;
439 }
440
441 $isFreeEntity = false;
442
443 if ($region !== 'ru' && ($entityType === static::ENTITY_TYPE_INTEGRATION || $entityType === static::ENTITY_TYPE_AP_CONNECT))
444 {
445 $isFreeEntity = true;
446 }
447 elseif (!empty($entityData))
448 {
449 if (
450 $entityData['ID'] > 0
451 && (isset($entityData['ACTIVE']) && $entityData['ACTIVE'])
452 && (
453 $entityData['STATUS'] === AppTable::STATUS_FREE
454 || ($entityData['STATUS'] === AppTable::STATUS_LOCAL && $region !== 'ru')
455 )
456 )
457 {
458 $isFreeEntity = true;
459 }
460 elseif (
461 (
462 !isset($entityData['ACTIVE'])
463 || !$entityData['ACTIVE']
464 )
465 && !(
466 $entityData['BY_SUBSCRIPTION'] === 'Y'
467 || ($entityData['FREE'] === 'N' && !empty($entityData['PRICE']))
468 )
469 )
470 {
471 $isFreeEntity = true;
472 }
473 }
474
475 $isUsedDemoLicense = false;
476 if ($isB24 && (int) Option::get('bitrix24', 'DEMO_START', 0) > 0)
477 {
478 $isUsedDemoLicense = true;
479 }
480
481 if (!static::isActiveRules())
482 {
483 if (
484 !empty($entityData)
485 && (
486 $entityData['BY_SUBSCRIPTION'] === 'Y'
487 || ($entityData['ID'] > 0 && $entityData['STATUS'] === AppTable::STATUS_SUBSCRIPTION)
488 )
489 )
490 {
491 if ($isSubscriptionDemoAvailable)
492 {
493 // activate demo subscription
494 $code = 'limit_subscription_market_access';
495 }
496 elseif ($isB24 && $isDemo)
497 {
498 // choose license with subscription
499 $code = 'limit_subscription_market_tarifwithmarket';
500 }
501 else
502 {
503 // choose subscription
504 $code = 'limit_subscription_market_marketpaid';
505 }
506 }
507 }
508 elseif (!$isSubscriptionAccess)
509 {
510 if ($isMinLicense)
511 {
512 if ($isUsedDemoLicense)
513 {
514 $code = 'limit_free_rest_hold_no_demo';
515 }
516 elseif ($entityType === static::ENTITY_TYPE_AP_CONNECT)
517 {
518 $code = 'limit_market_bus';
519 }
520 else
521 {
522 $code = 'limit_free_rest_hold';
523 }
524 }
525 }
526 elseif (!static::isAvailable())
527 {
528 if ($hasPaidApplication || !$isFreeEntity)
529 {
530 if ($isSubscriptionDemoAvailable)
531 {
532 // activate demo subscription
533 $code = 'limit_subscription_market_access_buy_marketplus';
534 }
535 elseif (!$isB24)
536 {
537 // choose subscription
538 $code = 'plus_need_trial';
539 }
540 elseif ($isMinLicense)
541 {
542 // choose license with subscription
543 $code = 'limit_subscription_market_tarifwithmarket';
544
545 if ($action === static::ACTION_OPEN)
546 {
547 $code = 'installed_plus_buy_license_with_plus';
548 }
549 }
550 else
551 {
552 $code = 'limit_subscription_market_marketpaid_trialend';
553 }
554 }
555 elseif ($isB24 && !$isUsedDemoLicense)
556 {
557 // activate demo license
558 if ($entityType === static::ENTITY_TYPE_AP_CONNECT)
559 {
560 $code = 'limit_market_bus';
561 }
562 else
563 {
564 $code = 'limit_free_rest_hold';
565 }
566 }
567 elseif ($isB24 && !$isMaxApplicationDemo)
568 {
569 $code = 'limit_free_rest_hold_no_demo';
570 }
571 elseif ($isSubscriptionDemoAvailable)
572 {
573 // activate demo subscription
574 $code = 'limit_subscription_market_marketpaid';
575 }
576 elseif ($isB24 && $isSubscriptionAccess)
577 {
578 // choose license with subscription
579 $code = 'limit_subscription_market_tarifwithmarket';
580 if ($action === static::ACTION_OPEN)
581 {
582 $code = 'limit_free_apps_buy_license_with_plus';
583 }
584 }
585 elseif (!$isB24 && $isSubscriptionAccess)
586 {
587 // choose subscription
588 $code = 'plus_need_trial';
589 }
590 }
591 elseif ($isB24 && !$isDemo && $isMaxApplication && $isFreeEntity && !$isMaxLicense)
592 {
593 if (!$isUsedDemoLicense)
594 {
595 // activate demo license
596 if ($entityType === static::ENTITY_TYPE_AP_CONNECT)
597 {
598 $code = 'limit_market_bus';
599 }
600 else
601 {
602 $code = 'limit_free_apps_need_demo';
603 }
604 }
605 elseif ($region === 'ru')
606 {
607 $code = 'limit_subscription_market_access_buy_marketplus';
608 }
609 else
610 {
611 $code = 'limit_free_apps_buy_license';
612 }
613 }
614 elseif (
615 $isSubscriptionDemoAvailable
616 && $isCanInstallInDemo
617 && ($hasPaidApplication || $isMaxApplication || !$isFreeEntity)
618 )
619 {
620 if (!$isFreeEntity)
621 {
622 // activate demo subscription
623 if ($region === 'ru')
624 {
625 if (!$isMinLicense)
626 {
627 $code = 'limit_subscription_market_access_buy_marketplus';
628 }
629 else
630 {
631 $code = 'limit_market_trial_demo';
632 }
633 }
634 else
635 {
636 $code = 'limit_subscription_market_access_buy_marketplus';
637 }
638 }
639 elseif ($isB24 && $isDemo)
640 {
641 // activate demo subscription
642 $code = 'limit_subscription_market_marketpaid';
643 }
644 else
645 {
646 // activate demo subscription
647 $code = 'limit_subscription_market_access';
648 }
649 }
650 elseif ($isDemoSubscription && !$isCanInstallInDemo)
651 {
652 // only paid subscription app
653 $code = 'subscription_market_paid_access';
654 }
655 elseif ($canBuySubscription)
656 {
657 $code = 'limit_subscription_market_access_buy_marketplus';
658 }
659 elseif ($isB24 && $isDemo)
660 {
661 if (!$isSubscriptionDemoAvailable)
662 {
663 // choose license with subscription
664 $code = 'limit_subscription_market_tarifwithmarket';
665 }
666 else
667 {
668 // activate demo subscription
669 $code = 'limit_subscription_market_access';
670 }
671 }
672 elseif (!$isSubscriptionAvailable)
673 {
674 $code = 'limit_free_rest_hold_no_demo';
675 }
676
677 return $code;
678 }
679
683 public static function resetToFree()
684 {
685 static::reset();
686 Option::delete(static::MODULE_ID, ['name' => static::OPTION_HOLD_CHECK_COUNT_APP]);
688
689 return true;
690 }
691
695 public static function onBitrix24LicenseChange($licenseType)
696 {
697 static::reset();
698 if (
699 !Client::isSubscriptionAccess()
700 && Loader::includeModule('bitrix24')
701 && in_array($licenseType, \CBitrix24::PAID_EDITIONS, true)
702 )
703 {
704 Option::set(static::MODULE_ID, static::OPTION_HOLD_CHECK_COUNT_APP, 'Y');
705 }
706 }
707
711 public static function needCheckCount()
712 {
713 if (!static::isActiveRules())
714 {
715 return false;
716 }
717
718 return Option::get(static::MODULE_ID, static::OPTION_HOLD_CHECK_COUNT_APP, 'N') === 'N';
719 }
720
724 public static function isActiveRules()
725 {
726 return
728 || Option::get(static::MODULE_ID, static::OPTION_ACCESS_ACTIVE, 'N') === 'Y'
729 ;
730 }
731
739 public static function calcUsageEntityAgent($period = false)
740 {
741 static::getActiveEntity(true);
742 return $period === true ? '\Bitrix\Rest\Engine\Access::calcUsageEntityAgent(true);' : '';
743 }
744
749 public static function reset() : bool
750 {
751 static::$availableApp = [];
752 static::$availableAppCount = [];
753
754 return true;
755 }
756}
$count
Определения admin_tab.php:4
static getInstance()
Определения application.php:98
Определения loader.php:13
static includeModule($moduleName)
Определения loader.php:67
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
Определения date.php:9
Определения json.php:9
Определения app.php:68
const STATUS_PAID
Определения app.php:86
static getByClientId($clientId)
Определения app.php:967
const STATUS_TRIAL
Определения app.php:88
const STATUS_LOCAL
Определения app.php:84
const ACTIVE
Определения app.php:69
const STATUS_DEMO
Определения app.php:87
const STATUS_FREE
Определения app.php:85
const STATUS_SUBSCRIPTION
Определения app.php:89
static isFeatureEnabled()
Определения access.php:51
const ACTION_OPEN
Определения access.php:31
static reset()
Определения access.php:749
static isActiveRules()
Определения access.php:724
const ENTITY_TYPE_WEBHOOK
Определения access.php:27
static isAvailableCount(string $entityType, $entity=0)
Определения access.php:171
static isAllowFreeApp(array $freeAppData)
Определения access.php:127
const OPTION_AVAILABLE_COUNT
Определения access.php:36
static calcUsageEntityAgent($period=false)
Определения access.php:739
static needCheckCount()
Определения access.php:711
static getHelperCode($action='', $entityType='', $entityData=[])
Определения access.php:360
const ENTITY_TYPE_APP_STATUS
Определения access.php:24
const OPTION_ACCESS_ACTIVE
Определения access.php:35
const MODULE_ID
Определения access.php:34
static getAvailableCount()
Определения access.php:233
const ENTITY_COUNT
Определения access.php:28
static canInstallApp(array $installAppData)
Определения access.php:114
static getActiveEntity($force=false)
Определения access.php:273
const OPTION_SUBSCRIPTION_AVAILABLE
Определения access.php:37
const ENTITY_TYPE_AP_CONNECT
Определения access.php:26
static resetToFree()
Определения access.php:683
const ACTION_INSTALL
Определения access.php:30
static isAvailableAPAuthByPasswordId(int $passwordId)
Определения access.php:135
static isAvailable($app='')
Определения access.php:69
const ACTION_BUY
Определения access.php:32
static onBitrix24LicenseChange($licenseType)
Определения access.php:695
const ENTITY_TYPE_INTEGRATION
Определения access.php:25
const ENTITY_TYPE_APP
Определения access.php:23
static getList()
Определения immune.php:23
static setLastCheckTimestamp($timestamp)
Определения notification.php:166
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$maxCount
Определения options.php:1622
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$entity
$region
Определения .description.php:13
$app
Определения proxy.php:8
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
if(file_exists(( $_fname=__DIR__ . "/classes/general/update_db_updater.php"))) if(($_fname=getLocalPath("init.php")) !==false) if(( $_fname=getLocalPath("php_interface/init.php", BX_PERSONAL_ROOT)) !==false) if(($_fname=getLocalPath("php_interface/" . SITE_ID . "/init.php", BX_PERSONAL_ROOT)) !==false) if((!(defined("STATISTIC_ONLY") &&STATISTIC_ONLY &&!str_starts_with( $GLOBALS["APPLICATION"]->GetCurPage(), BX_ROOT . "/admin/"))) &&COption::GetOptionString("main", "include_charset", "Y")=="Y" &&LANG_CHARSET !='') if(COption::GetOptionString("main", "set_p3p_header", "Y")=="Y") $license
Определения include.php:158
Определения accesscode.php:9
Определения access.php:3
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$option
Определения options.php:1711
$action
Определения file_dialog.php:21