Admin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification\Settings;
  8. use OC\User\Backend;
  9. use OCA\UpdateNotification\UpdateChecker;
  10. use OCP\AppFramework\Http\TemplateResponse;
  11. use OCP\AppFramework\Services\IInitialState;
  12. use OCP\IAppConfig;
  13. use OCP\IConfig;
  14. use OCP\IDateTimeFormatter;
  15. use OCP\IGroupManager;
  16. use OCP\IUserManager;
  17. use OCP\L10N\IFactory;
  18. use OCP\Settings\ISettings;
  19. use OCP\Support\Subscription\IRegistry;
  20. use OCP\User\Backend\ICountUsersBackend;
  21. use OCP\Util;
  22. use Psr\Log\LoggerInterface;
  23. class Admin implements ISettings {
  24. public function __construct(
  25. private IConfig $config,
  26. private IAppConfig $appConfig,
  27. private UpdateChecker $updateChecker,
  28. private IGroupManager $groupManager,
  29. private IDateTimeFormatter $dateTimeFormatter,
  30. private IFactory $l10nFactory,
  31. private IRegistry $subscriptionRegistry,
  32. private IUserManager $userManager,
  33. private LoggerInterface $logger,
  34. private IInitialState $initialState,
  35. ) {
  36. }
  37. public function getForm(): TemplateResponse {
  38. $lastUpdateCheckTimestamp = $this->appConfig->getValueInt('core', 'lastupdatedat');
  39. $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
  40. $channels = [
  41. 'daily',
  42. 'beta',
  43. 'stable',
  44. 'production',
  45. ];
  46. $currentChannel = Util::getChannel();
  47. if ($currentChannel === 'git') {
  48. $channels[] = 'git';
  49. }
  50. $updateState = $this->updateChecker->getUpdateState();
  51. $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
  52. $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
  53. $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
  54. $defaultCustomerUpdateServerURLPrefix = 'https://updates.nextcloud.com/customers/';
  55. $isDefaultUpdateServerURL = $updateServerURL === $defaultUpdateServerURL
  56. || strpos($updateServerURL, $defaultCustomerUpdateServerURLPrefix) === 0;
  57. $hasValidSubscription = $this->subscriptionRegistry->delegateHasValidSubscription();
  58. $params = [
  59. 'isNewVersionAvailable' => !empty($updateState['updateAvailable']),
  60. 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0,
  61. 'lastChecked' => $lastUpdateCheck,
  62. 'currentChannel' => $currentChannel,
  63. 'channels' => $channels,
  64. 'newVersion' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'],
  65. 'newVersionString' => empty($updateState['updateVersionString']) ? '' : $updateState['updateVersionString'],
  66. 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'],
  67. 'changes' => $this->filterChanges($updateState['changes'] ?? []),
  68. 'webUpdaterEnabled' => !$this->config->getSystemValue('upgrade.disable-web', false),
  69. 'isWebUpdaterRecommended' => $this->isWebUpdaterRecommended(),
  70. 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'],
  71. 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'],
  72. 'isDefaultUpdateServerURL' => $isDefaultUpdateServerURL,
  73. 'updateServerURL' => $updateServerURL,
  74. 'notifyGroups' => $this->getSelectedGroups($notifyGroups),
  75. 'hasValidSubscription' => $hasValidSubscription,
  76. ];
  77. $this->initialState->provideInitialState('data', $params);
  78. return new TemplateResponse('updatenotification', 'admin', [], '');
  79. }
  80. protected function filterChanges(array $changes): array {
  81. $filtered = [];
  82. if (isset($changes['changelogURL'])) {
  83. $filtered['changelogURL'] = $changes['changelogURL'];
  84. }
  85. if (!isset($changes['whatsNew'])) {
  86. return $filtered;
  87. }
  88. $iterator = $this->l10nFactory->getLanguageIterator();
  89. do {
  90. $lang = $iterator->current();
  91. if (isset($changes['whatsNew'][$lang])) {
  92. $filtered['whatsNew'] = $changes['whatsNew'][$lang];
  93. return $filtered;
  94. }
  95. $iterator->next();
  96. } while ($lang !== 'en' && $iterator->valid());
  97. return $filtered;
  98. }
  99. /**
  100. * @param list<string> $groupIds
  101. * @return list<array{id: string, displayname: string}>
  102. */
  103. protected function getSelectedGroups(array $groupIds): array {
  104. $result = [];
  105. foreach ($groupIds as $groupId) {
  106. $group = $this->groupManager->get($groupId);
  107. if ($group === null) {
  108. continue;
  109. }
  110. $result[] = ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()];
  111. }
  112. return $result;
  113. }
  114. public function getSection(): string {
  115. return 'overview';
  116. }
  117. public function getPriority(): int {
  118. return 11;
  119. }
  120. private function isWebUpdaterRecommended(): bool {
  121. return $this->getUserCount() < 100;
  122. }
  123. /**
  124. * @see https://github.com/nextcloud/server/blob/39494fbf794d982f6f6551c984e6ca4c4e947d01/lib/private/Support/Subscription/Registry.php#L188-L216 implementation reference
  125. */
  126. private function getUserCount(): int {
  127. $userCount = 0;
  128. $backends = $this->userManager->getBackends();
  129. foreach ($backends as $backend) {
  130. // TODO: change below to 'if ($backend instanceof ICountUsersBackend) {'
  131. if ($backend->implementsActions(Backend::COUNT_USERS)) {
  132. /** @var ICountUsersBackend $backend */
  133. $backendUsers = $backend->countUsers();
  134. if ($backendUsers !== false) {
  135. $userCount += $backendUsers;
  136. }
  137. }
  138. }
  139. return $userCount;
  140. }
  141. }