Application.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\UpdateNotification\AppInfo;
  30. use OCA\UpdateNotification\Listener\AppUpdateEventListener;
  31. use OCA\UpdateNotification\Listener\BeforeTemplateRenderedEventListener;
  32. use OCA\UpdateNotification\Notification\AppUpdateNotifier;
  33. use OCA\UpdateNotification\Notification\Notifier;
  34. use OCA\UpdateNotification\UpdateChecker;
  35. use OCP\App\Events\AppUpdateEvent;
  36. use OCP\App\IAppManager;
  37. use OCP\AppFramework\App;
  38. use OCP\AppFramework\Bootstrap\IBootContext;
  39. use OCP\AppFramework\Bootstrap\IBootstrap;
  40. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  41. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  42. use OCP\IConfig;
  43. use OCP\IGroupManager;
  44. use OCP\IUser;
  45. use OCP\IUserSession;
  46. use OCP\Util;
  47. use Psr\Container\ContainerExceptionInterface;
  48. use Psr\Container\ContainerInterface;
  49. use Psr\Log\LoggerInterface;
  50. class Application extends App implements IBootstrap {
  51. public const APP_NAME = 'updatenotification';
  52. public function __construct() {
  53. parent::__construct(self::APP_NAME, []);
  54. }
  55. public function register(IRegistrationContext $context): void {
  56. $context->registerNotifierService(Notifier::class);
  57. $context->registerNotifierService(AppUpdateNotifier::class);
  58. $context->registerEventListener(AppUpdateEvent::class, AppUpdateEventListener::class);
  59. $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedEventListener::class);
  60. }
  61. public function boot(IBootContext $context): void {
  62. $context->injectFn(function (IConfig $config,
  63. IUserSession $userSession,
  64. IAppManager $appManager,
  65. IGroupManager $groupManager,
  66. ContainerInterface $container,
  67. LoggerInterface $logger) {
  68. if ($config->getSystemValue('updatechecker', true) !== true) {
  69. // Updater check is disabled
  70. return;
  71. }
  72. $user = $userSession->getUser();
  73. if (!$user instanceof IUser) {
  74. // Nothing to do for guests
  75. return;
  76. }
  77. if (!$appManager->isEnabledForUser('notifications') &&
  78. $groupManager->isAdmin($user->getUID())) {
  79. try {
  80. $updateChecker = $container->get(UpdateChecker::class);
  81. } catch (ContainerExceptionInterface $e) {
  82. $logger->error($e->getMessage(), ['exception' => $e]);
  83. return;
  84. }
  85. if ($updateChecker->getUpdateState() !== []) {
  86. Util::addScript('updatenotification', 'legacy-notification');
  87. \OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables');
  88. }
  89. }
  90. });
  91. }
  92. }