Admin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\UpdateNotification\Settings;
  29. use OC\User\Backend;
  30. use OCA\UpdateNotification\UpdateChecker;
  31. use OCP\AppFramework\Http\TemplateResponse;
  32. use OCP\AppFramework\Services\IInitialState;
  33. use OCP\IAppConfig;
  34. use OCP\IConfig;
  35. use OCP\IDateTimeFormatter;
  36. use OCP\IGroupManager;
  37. use OCP\IUserManager;
  38. use OCP\L10N\IFactory;
  39. use OCP\Settings\ISettings;
  40. use OCP\Support\Subscription\IRegistry;
  41. use OCP\User\Backend\ICountUsersBackend;
  42. use OCP\Util;
  43. use Psr\Log\LoggerInterface;
  44. class Admin implements ISettings {
  45. public function __construct(
  46. private IConfig $config,
  47. private IAppConfig $appConfig,
  48. private UpdateChecker $updateChecker,
  49. private IGroupManager $groupManager,
  50. private IDateTimeFormatter $dateTimeFormatter,
  51. private IFactory $l10nFactory,
  52. private IRegistry $subscriptionRegistry,
  53. private IUserManager $userManager,
  54. private LoggerInterface $logger,
  55. private IInitialState $initialState
  56. ) {
  57. }
  58. public function getForm(): TemplateResponse {
  59. $lastUpdateCheckTimestamp = $this->appConfig->getValueInt('core', 'lastupdatedat');
  60. $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
  61. $channels = [
  62. 'daily',
  63. 'beta',
  64. 'stable',
  65. 'production',
  66. ];
  67. $currentChannel = Util::getChannel();
  68. if ($currentChannel === 'git') {
  69. $channels[] = 'git';
  70. }
  71. $updateState = $this->updateChecker->getUpdateState();
  72. $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
  73. $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
  74. $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
  75. $defaultCustomerUpdateServerURLPrefix = 'https://updates.nextcloud.com/customers/';
  76. $isDefaultUpdateServerURL = $updateServerURL === $defaultUpdateServerURL
  77. || strpos($updateServerURL, $defaultCustomerUpdateServerURLPrefix) === 0;
  78. $hasValidSubscription = $this->subscriptionRegistry->delegateHasValidSubscription();
  79. $params = [
  80. 'isNewVersionAvailable' => !empty($updateState['updateAvailable']),
  81. 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0,
  82. 'lastChecked' => $lastUpdateCheck,
  83. 'currentChannel' => $currentChannel,
  84. 'channels' => $channels,
  85. 'newVersion' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'],
  86. 'newVersionString' => empty($updateState['updateVersionString']) ? '' : $updateState['updateVersionString'],
  87. 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'],
  88. 'changes' => $this->filterChanges($updateState['changes'] ?? []),
  89. 'webUpdaterEnabled' => !$this->config->getSystemValue('upgrade.disable-web', false),
  90. 'isWebUpdaterRecommended' => $this->isWebUpdaterRecommended(),
  91. 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'],
  92. 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'],
  93. 'isDefaultUpdateServerURL' => $isDefaultUpdateServerURL,
  94. 'updateServerURL' => $updateServerURL,
  95. 'notifyGroups' => $this->getSelectedGroups($notifyGroups),
  96. 'hasValidSubscription' => $hasValidSubscription,
  97. ];
  98. $this->initialState->provideInitialState('data', $params);
  99. return new TemplateResponse('updatenotification', 'admin', [], '');
  100. }
  101. protected function filterChanges(array $changes): array {
  102. $filtered = [];
  103. if (isset($changes['changelogURL'])) {
  104. $filtered['changelogURL'] = $changes['changelogURL'];
  105. }
  106. if (!isset($changes['whatsNew'])) {
  107. return $filtered;
  108. }
  109. $iterator = $this->l10nFactory->getLanguageIterator();
  110. do {
  111. $lang = $iterator->current();
  112. if (isset($changes['whatsNew'][$lang])) {
  113. $filtered['whatsNew'] = $changes['whatsNew'][$lang];
  114. return $filtered;
  115. }
  116. $iterator->next();
  117. } while ($lang !== 'en' && $iterator->valid());
  118. return $filtered;
  119. }
  120. /**
  121. * @param list<string> $groupIds
  122. * @return list<array{id: string, displayname: string}>
  123. */
  124. protected function getSelectedGroups(array $groupIds): array {
  125. $result = [];
  126. foreach ($groupIds as $groupId) {
  127. $group = $this->groupManager->get($groupId);
  128. if ($group === null) {
  129. continue;
  130. }
  131. $result[] = ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()];
  132. }
  133. return $result;
  134. }
  135. public function getSection(): string {
  136. return 'overview';
  137. }
  138. public function getPriority(): int {
  139. return 11;
  140. }
  141. private function isWebUpdaterRecommended(): bool {
  142. return $this->getUserCount() < 100;
  143. }
  144. /**
  145. * @see https://github.com/nextcloud/server/blob/39494fbf794d982f6f6551c984e6ca4c4e947d01/lib/private/Support/Subscription/Registry.php#L188-L216 implementation reference
  146. */
  147. private function getUserCount(): int {
  148. $userCount = 0;
  149. $backends = $this->userManager->getBackends();
  150. foreach ($backends as $backend) {
  151. // TODO: change below to 'if ($backend instanceof ICountUsersBackend) {'
  152. if ($backend->implementsActions(Backend::COUNT_USERS)) {
  153. /** @var ICountUsersBackend $backend */
  154. $backendUsers = $backend->countUsers();
  155. if ($backendUsers !== false) {
  156. $userCount += $backendUsers;
  157. }
  158. }
  159. }
  160. return $userCount;
  161. }
  162. }