Registry.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Support\Subscription;
  8. use OC\User\Backend;
  9. use OCP\AppFramework\QueryException;
  10. use OCP\IConfig;
  11. use OCP\IGroupManager;
  12. use OCP\IServerContainer;
  13. use OCP\IUserManager;
  14. use OCP\Notification\IManager;
  15. use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
  16. use OCP\Support\Subscription\IRegistry;
  17. use OCP\Support\Subscription\ISubscription;
  18. use OCP\Support\Subscription\ISupportedApps;
  19. use OCP\User\Backend\ICountMappedUsersBackend;
  20. use OCP\User\Backend\ICountUsersBackend;
  21. use Psr\Log\LoggerInterface;
  22. class Registry implements IRegistry {
  23. /** @var ISubscription */
  24. private $subscription = null;
  25. /** @var string */
  26. private $subscriptionService = null;
  27. /** @var IConfig */
  28. private $config;
  29. /** @var IServerContainer */
  30. private $container;
  31. /** @var IUserManager */
  32. private $userManager;
  33. /** @var IGroupManager */
  34. private $groupManager;
  35. /** @var LoggerInterface */
  36. private $logger;
  37. public function __construct(IConfig $config,
  38. IServerContainer $container,
  39. IUserManager $userManager,
  40. IGroupManager $groupManager,
  41. LoggerInterface $logger) {
  42. $this->config = $config;
  43. $this->container = $container;
  44. $this->userManager = $userManager;
  45. $this->groupManager = $groupManager;
  46. $this->logger = $logger;
  47. }
  48. private function getSubscription(): ?ISubscription {
  49. if ($this->subscription === null && $this->subscriptionService !== null) {
  50. try {
  51. $this->subscription = $this->container->query($this->subscriptionService);
  52. } catch (QueryException $e) {
  53. // Ignore this
  54. }
  55. }
  56. return $this->subscription;
  57. }
  58. /**
  59. * Register a subscription instance. In case it is called multiple times the
  60. * first one is used.
  61. *
  62. * @param ISubscription $subscription
  63. * @throws AlreadyRegisteredException
  64. *
  65. * @since 17.0.0
  66. */
  67. public function register(ISubscription $subscription): void {
  68. if ($this->subscription !== null || $this->subscriptionService !== null) {
  69. throw new AlreadyRegisteredException();
  70. }
  71. $this->subscription = $subscription;
  72. }
  73. public function registerService(string $subscriptionService): void {
  74. if ($this->subscription !== null || $this->subscriptionService !== null) {
  75. throw new AlreadyRegisteredException();
  76. }
  77. $this->subscriptionService = $subscriptionService;
  78. }
  79. /**
  80. * Fetches the list of app IDs that are supported by the subscription
  81. *
  82. * @since 17.0.0
  83. */
  84. public function delegateGetSupportedApps(): array {
  85. if ($this->getSubscription() instanceof ISupportedApps) {
  86. return $this->getSubscription()->getSupportedApps();
  87. }
  88. return [];
  89. }
  90. /**
  91. * Indicates if a valid subscription is available
  92. *
  93. * @since 17.0.0
  94. */
  95. public function delegateHasValidSubscription(): bool {
  96. // Allow overwriting this manually for environments where the subscription information cannot be fetched
  97. if ($this->config->getSystemValueBool('has_valid_subscription')) {
  98. return true;
  99. }
  100. if ($this->getSubscription() instanceof ISubscription) {
  101. return $this->getSubscription()->hasValidSubscription();
  102. }
  103. return false;
  104. }
  105. /**
  106. * Indicates if the subscription has extended support
  107. *
  108. * @since 17.0.0
  109. */
  110. public function delegateHasExtendedSupport(): bool {
  111. if ($this->getSubscription() instanceof ISubscription) {
  112. return $this->getSubscription()->hasExtendedSupport();
  113. }
  114. return false;
  115. }
  116. /**
  117. * Indicates if a hard user limit is reached and no new users should be created
  118. *
  119. * @param IManager|null $notificationManager
  120. * @since 21.0.0
  121. */
  122. public function delegateIsHardUserLimitReached(?IManager $notificationManager = null): bool {
  123. $subscription = $this->getSubscription();
  124. if ($subscription instanceof ISubscription &&
  125. $subscription->hasValidSubscription()) {
  126. $userLimitReached = $subscription->isHardUserLimitReached();
  127. if ($userLimitReached && $notificationManager instanceof IManager) {
  128. $this->notifyAboutReachedUserLimit($notificationManager);
  129. }
  130. return $userLimitReached;
  131. }
  132. $isOneClickInstance = $this->config->getSystemValueBool('one-click-instance', false);
  133. if (!$isOneClickInstance) {
  134. return false;
  135. }
  136. $userCount = $this->getUserCount();
  137. $hardUserLimit = $this->config->getSystemValueInt('one-click-instance.user-limit', 50);
  138. $userLimitReached = $userCount >= $hardUserLimit;
  139. if ($userLimitReached && $notificationManager instanceof IManager) {
  140. $this->notifyAboutReachedUserLimit($notificationManager);
  141. }
  142. return $userLimitReached;
  143. }
  144. private function getUserCount(): int {
  145. $userCount = 0;
  146. $backends = $this->userManager->getBackends();
  147. foreach ($backends as $backend) {
  148. if ($backend instanceof ICountMappedUsersBackend) {
  149. $userCount += $backend->countMappedUsers();
  150. } elseif ($backend->implementsActions(Backend::COUNT_USERS)) {
  151. /** @var ICountUsersBackend $backend */
  152. $backendUsers = $backend->countUsers();
  153. if ($backendUsers !== false) {
  154. $userCount += $backendUsers;
  155. } else {
  156. // TODO what if the user count can't be determined?
  157. $this->logger->warning('Can not determine user count for ' . get_class($backend), ['app' => 'lib']);
  158. }
  159. }
  160. }
  161. $disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
  162. $disabledUsersCount = count($disabledUsers);
  163. $userCount = $userCount - $disabledUsersCount;
  164. if ($userCount < 0) {
  165. $userCount = 0;
  166. // this should never happen
  167. $this->logger->warning("Total user count was negative (users: $userCount, disabled: $disabledUsersCount)", ['app' => 'lib']);
  168. }
  169. return $userCount;
  170. }
  171. private function notifyAboutReachedUserLimit(IManager $notificationManager): void {
  172. $admins = $this->groupManager->get('admin')->getUsers();
  173. $notification = $notificationManager->createNotification();
  174. $notification->setApp('core')
  175. ->setObject('user_limit_reached', '1')
  176. ->setSubject('user_limit_reached');
  177. if ($notificationManager->getCount($notification) > 0
  178. && !$this->reIssue()
  179. ) {
  180. return;
  181. }
  182. $notificationManager->markProcessed($notification);
  183. $notification->setDateTime(new \DateTime());
  184. foreach ($admins as $admin) {
  185. $notification->setUser($admin->getUID());
  186. $notificationManager->notify($notification);
  187. }
  188. $this->logger->warning('The account limit was reached and the new account was not created', ['app' => 'lib']);
  189. }
  190. protected function reIssue(): bool {
  191. $lastNotification = (int)$this->config->getAppValue('lib', 'last_subscription_reminder', '0');
  192. if ((time() - $lastNotification) >= 86400) {
  193. $this->config->setAppValue('lib', 'last_subscription_reminder', (string)time());
  194. return true;
  195. }
  196. return false;
  197. }
  198. }