Admin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 OCP\User\Backend\ICountUsersBackend;
  31. use OCA\UpdateNotification\UpdateChecker;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\AppFramework\Services\IInitialState;
  34. use OCP\IConfig;
  35. use OCP\IDateTimeFormatter;
  36. use OCP\IGroupManager;
  37. use OCP\L10N\IFactory;
  38. use OCP\Settings\ISettings;
  39. use OCP\Support\Subscription\IRegistry;
  40. use OCP\Util;
  41. use OCP\IUserManager;
  42. use Psr\Log\LoggerInterface;
  43. class Admin implements ISettings {
  44. private IConfig $config;
  45. private UpdateChecker $updateChecker;
  46. private IGroupManager $groupManager;
  47. private IDateTimeFormatter $dateTimeFormatter;
  48. private IFactory $l10nFactory;
  49. private IRegistry $subscriptionRegistry;
  50. private IUserManager $userManager;
  51. private LoggerInterface $logger;
  52. private IInitialState $initialState;
  53. public function __construct(
  54. IConfig $config,
  55. UpdateChecker $updateChecker,
  56. IGroupManager $groupManager,
  57. IDateTimeFormatter $dateTimeFormatter,
  58. IFactory $l10nFactory,
  59. IRegistry $subscriptionRegistry,
  60. IUserManager $userManager,
  61. LoggerInterface $logger,
  62. IInitialState $initialState
  63. ) {
  64. $this->config = $config;
  65. $this->updateChecker = $updateChecker;
  66. $this->groupManager = $groupManager;
  67. $this->dateTimeFormatter = $dateTimeFormatter;
  68. $this->l10nFactory = $l10nFactory;
  69. $this->subscriptionRegistry = $subscriptionRegistry;
  70. $this->userManager = $userManager;
  71. $this->logger = $logger;
  72. $this->initialState = $initialState;
  73. }
  74. public function getForm(): TemplateResponse {
  75. $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat');
  76. $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp);
  77. $channels = [
  78. 'daily',
  79. 'beta',
  80. 'stable',
  81. 'production',
  82. ];
  83. $currentChannel = Util::getChannel();
  84. if ($currentChannel === 'git') {
  85. $channels[] = 'git';
  86. }
  87. $updateState = $this->updateChecker->getUpdateState();
  88. $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
  89. $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/';
  90. $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL);
  91. $defaultCustomerUpdateServerURLPrefix = 'https://updates.nextcloud.com/customers/';
  92. $isDefaultUpdateServerURL = $updateServerURL === $defaultUpdateServerURL
  93. || strpos($updateServerURL, $defaultCustomerUpdateServerURLPrefix) === 0;
  94. $hasValidSubscription = $this->subscriptionRegistry->delegateHasValidSubscription();
  95. $params = [
  96. 'isNewVersionAvailable' => !empty($updateState['updateAvailable']),
  97. 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0,
  98. 'lastChecked' => $lastUpdateCheck,
  99. 'currentChannel' => $currentChannel,
  100. 'channels' => $channels,
  101. 'newVersion' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'],
  102. 'newVersionString' => empty($updateState['updateVersionString']) ? '' : $updateState['updateVersionString'],
  103. 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'],
  104. 'changes' => $this->filterChanges($updateState['changes'] ?? []),
  105. 'webUpdaterEnabled' => !$this->config->getSystemValue('upgrade.disable-web', false),
  106. 'isWebUpdaterRecommended' => $this->isWebUpdaterRecommended(),
  107. 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'],
  108. 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'],
  109. 'isDefaultUpdateServerURL' => $isDefaultUpdateServerURL,
  110. 'updateServerURL' => $updateServerURL,
  111. 'notifyGroups' => $this->getSelectedGroups($notifyGroups),
  112. 'hasValidSubscription' => $hasValidSubscription,
  113. ];
  114. $this->initialState->provideInitialState('data', $params);
  115. return new TemplateResponse('updatenotification', 'admin', [], '');
  116. }
  117. protected function filterChanges(array $changes): array {
  118. $filtered = [];
  119. if (isset($changes['changelogURL'])) {
  120. $filtered['changelogURL'] = $changes['changelogURL'];
  121. }
  122. if (!isset($changes['whatsNew'])) {
  123. return $filtered;
  124. }
  125. $iterator = $this->l10nFactory->getLanguageIterator();
  126. do {
  127. $lang = $iterator->current();
  128. if (isset($changes['whatsNew'][$lang])) {
  129. $filtered['whatsNew'] = $changes['whatsNew'][$lang];
  130. return $filtered;
  131. }
  132. $iterator->next();
  133. } while ($lang !== 'en' && $iterator->valid());
  134. return $filtered;
  135. }
  136. /**
  137. * @param list<string> $groupIds
  138. * @return list<array{id: string, displayname: string}>
  139. */
  140. protected function getSelectedGroups(array $groupIds): array {
  141. $result = [];
  142. foreach ($groupIds as $groupId) {
  143. $group = $this->groupManager->get($groupId);
  144. if ($group === null) {
  145. continue;
  146. }
  147. $result[] = ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()];
  148. }
  149. return $result;
  150. }
  151. public function getSection(): string {
  152. return 'overview';
  153. }
  154. public function getPriority(): int {
  155. return 11;
  156. }
  157. private function isWebUpdaterRecommended(): bool {
  158. return $this->getUserCount() < 100;
  159. }
  160. /**
  161. * @see https://github.com/nextcloud/server/blob/39494fbf794d982f6f6551c984e6ca4c4e947d01/lib/private/Support/Subscription/Registry.php#L188-L216 implementation reference
  162. */
  163. private function getUserCount(): int {
  164. $userCount = 0;
  165. $backends = $this->userManager->getBackends();
  166. foreach ($backends as $backend) {
  167. // TODO: change below to 'if ($backend instanceof ICountUsersBackend) {'
  168. if ($backend->implementsActions(Backend::COUNT_USERS)) {
  169. /** @var ICountUsersBackend $backend */
  170. $backendUsers = $backend->countUsers();
  171. if ($backendUsers !== false) {
  172. $userCount += $backendUsers;
  173. }
  174. }
  175. }
  176. return $userCount;
  177. }
  178. }