Application.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  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
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\UpdateNotification\AppInfo;
  29. use OCA\UpdateNotification\Notification\Notifier;
  30. use OCA\UpdateNotification\UpdateChecker;
  31. use OCP\App\IAppManager;
  32. use OCP\AppFramework\App;
  33. use OCP\AppFramework\Bootstrap\IBootContext;
  34. use OCP\AppFramework\Bootstrap\IBootstrap;
  35. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  36. use OCP\AppFramework\IAppContainer;
  37. use OCP\AppFramework\QueryException;
  38. use OCP\IConfig;
  39. use OCP\IGroupManager;
  40. use OCP\IUser;
  41. use OCP\IUserSession;
  42. use OCP\Util;
  43. use Psr\Log\LoggerInterface;
  44. class Application extends App implements IBootstrap {
  45. public function __construct() {
  46. parent::__construct('updatenotification', []);
  47. }
  48. public function register(IRegistrationContext $context): void {
  49. $context->registerNotifierService(Notifier::class);
  50. }
  51. public function boot(IBootContext $context): void {
  52. $context->injectFn(function (IConfig $config,
  53. IUserSession $userSession,
  54. IAppManager $appManager,
  55. IGroupManager $groupManager,
  56. IAppContainer $appContainer,
  57. LoggerInterface $logger) {
  58. if ($config->getSystemValue('updatechecker', true) !== true) {
  59. // Updater check is disabled
  60. return;
  61. }
  62. $user = $userSession->getUser();
  63. if (!$user instanceof IUser) {
  64. // Nothing to do for guests
  65. return;
  66. }
  67. if (!$appManager->isEnabledForUser('notifications') &&
  68. $groupManager->isAdmin($user->getUID())) {
  69. try {
  70. $updateChecker = $appContainer->get(UpdateChecker::class);
  71. } catch (QueryException $e) {
  72. $logger->error($e->getMessage(), ['exception' => $e]);
  73. return;
  74. }
  75. if ($updateChecker->getUpdateState() !== []) {
  76. Util::addScript('updatenotification', 'legacy-notification');
  77. \OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables');
  78. }
  79. }
  80. });
  81. }
  82. }