You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Registry.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Support\Subscription;
  27. use OC\User\Backend;
  28. use OCP\AppFramework\QueryException;
  29. use OCP\IConfig;
  30. use OCP\IGroupManager;
  31. use OCP\IServerContainer;
  32. use OCP\IUserManager;
  33. use OCP\Notification\IManager;
  34. use OCP\User\Backend\ICountUsersBackend;
  35. use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
  36. use OCP\Support\Subscription\IRegistry;
  37. use OCP\Support\Subscription\ISubscription;
  38. use OCP\Support\Subscription\ISupportedApps;
  39. use Psr\Log\LoggerInterface;
  40. class Registry implements IRegistry {
  41. /** @var ISubscription */
  42. private $subscription = null;
  43. /** @var string */
  44. private $subscriptionService = null;
  45. /** @var IConfig */
  46. private $config;
  47. /** @var IServerContainer */
  48. private $container;
  49. /** @var IUserManager */
  50. private $userManager;
  51. /** @var IGroupManager */
  52. private $groupManager;
  53. /** @var LoggerInterface */
  54. private $logger;
  55. public function __construct(IConfig $config,
  56. IServerContainer $container,
  57. IUserManager $userManager,
  58. IGroupManager $groupManager,
  59. LoggerInterface $logger) {
  60. $this->config = $config;
  61. $this->container = $container;
  62. $this->userManager = $userManager;
  63. $this->groupManager = $groupManager;
  64. $this->logger = $logger;
  65. }
  66. private function getSubscription(): ?ISubscription {
  67. if ($this->subscription === null && $this->subscriptionService !== null) {
  68. try {
  69. $this->subscription = $this->container->query($this->subscriptionService);
  70. } catch (QueryException $e) {
  71. // Ignore this
  72. }
  73. }
  74. return $this->subscription;
  75. }
  76. /**
  77. * Register a subscription instance. In case it is called multiple times the
  78. * first one is used.
  79. *
  80. * @param ISubscription $subscription
  81. * @throws AlreadyRegisteredException
  82. *
  83. * @since 17.0.0
  84. */
  85. public function register(ISubscription $subscription): void {
  86. if ($this->subscription !== null || $this->subscriptionService !== null) {
  87. throw new AlreadyRegisteredException();
  88. }
  89. $this->subscription = $subscription;
  90. }
  91. public function registerService(string $subscriptionService): void {
  92. if ($this->subscription !== null || $this->subscriptionService !== null) {
  93. throw new AlreadyRegisteredException();
  94. }
  95. $this->subscriptionService = $subscriptionService;
  96. }
  97. /**
  98. * Fetches the list of app IDs that are supported by the subscription
  99. *
  100. * @since 17.0.0
  101. */
  102. public function delegateGetSupportedApps(): array {
  103. if ($this->getSubscription() instanceof ISupportedApps) {
  104. return $this->getSubscription()->getSupportedApps();
  105. }
  106. return [];
  107. }
  108. /**
  109. * Indicates if a valid subscription is available
  110. *
  111. * @since 17.0.0
  112. */
  113. public function delegateHasValidSubscription(): bool {
  114. // Allow overwriting this manually for environments where the subscription information cannot be fetched
  115. if ($this->config->getSystemValueBool('has_valid_subscription')) {
  116. return true;
  117. }
  118. if ($this->getSubscription() instanceof ISubscription) {
  119. return $this->getSubscription()->hasValidSubscription();
  120. }
  121. return false;
  122. }
  123. /**
  124. * Indicates if the subscription has extended support
  125. *
  126. * @since 17.0.0
  127. */
  128. public function delegateHasExtendedSupport(): bool {
  129. if ($this->getSubscription() instanceof ISubscription) {
  130. return $this->getSubscription()->hasExtendedSupport();
  131. }
  132. return false;
  133. }
  134. /**
  135. * Indicates if a hard user limit is reached and no new users should be created
  136. *
  137. * @param IManager|null $notificationManager
  138. * @since 21.0.0
  139. */
  140. public function delegateIsHardUserLimitReached(?IManager $notificationManager = null): bool {
  141. $subscription = $this->getSubscription();
  142. if ($subscription instanceof ISubscription &&
  143. $subscription->hasValidSubscription()) {
  144. $userLimitReached = $subscription->isHardUserLimitReached();
  145. if ($userLimitReached && $notificationManager instanceof IManager) {
  146. $this->notifyAboutReachedUserLimit($notificationManager);
  147. }
  148. return $userLimitReached;
  149. }
  150. $isOneClickInstance = $this->config->getSystemValueBool('one-click-instance', false);
  151. if (!$isOneClickInstance) {
  152. return false;
  153. }
  154. $userCount = $this->getUserCount();
  155. $hardUserLimit = $this->config->getSystemValue('one-click-instance.user-limit', 50);
  156. $userLimitReached = $userCount >= $hardUserLimit;
  157. if ($userLimitReached && $notificationManager instanceof IManager) {
  158. $this->notifyAboutReachedUserLimit($notificationManager);
  159. }
  160. return $userLimitReached;
  161. }
  162. private function getUserCount(): int {
  163. $userCount = 0;
  164. $backends = $this->userManager->getBackends();
  165. foreach ($backends as $backend) {
  166. if ($backend->implementsActions(Backend::COUNT_USERS)) {
  167. /** @var ICountUsersBackend $backend */
  168. $backendUsers = $backend->countUsers();
  169. if ($backendUsers !== false) {
  170. $userCount += $backendUsers;
  171. } else {
  172. // TODO what if the user count can't be determined?
  173. $this->logger->warning('Can not determine user count for ' . get_class($backend), ['app' => 'lib']);
  174. }
  175. }
  176. }
  177. $disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
  178. $disabledUsersCount = count($disabledUsers);
  179. $userCount = $userCount - $disabledUsersCount;
  180. if ($userCount < 0) {
  181. $userCount = 0;
  182. // this should never happen
  183. $this->logger->warning("Total user count was negative (users: $userCount, disabled: $disabledUsersCount)", ['app' => 'lib']);
  184. }
  185. return $userCount;
  186. }
  187. private function notifyAboutReachedUserLimit(IManager $notificationManager) {
  188. $admins = $this->groupManager->get('admin')->getUsers();
  189. foreach ($admins as $admin) {
  190. $notification = $notificationManager->createNotification();
  191. $notification->setApp('core')
  192. ->setUser($admin->getUID())
  193. ->setDateTime(new \DateTime())
  194. ->setObject('user_limit_reached', '1')
  195. ->setSubject('user_limit_reached');
  196. $notificationManager->notify($notification);
  197. }
  198. $this->logger->warning('The user limit was reached and the new user was not created', ['app' => 'lib']);
  199. }
  200. }