Registry.php 7.4 KB

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