Application.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Provisioning_API\AppInfo;
  7. use OC\Group\Manager as GroupManager;
  8. use OCA\Provisioning_API\Capabilities;
  9. use OCA\Provisioning_API\Listener\UserDeletedListener;
  10. use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware;
  11. use OCA\Settings\Mailer\NewUserMailHelper;
  12. use OCP\AppFramework\App;
  13. use OCP\AppFramework\Bootstrap\IBootContext;
  14. use OCP\AppFramework\Bootstrap\IBootstrap;
  15. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  16. use OCP\AppFramework\Utility\IControllerMethodReflector;
  17. use OCP\AppFramework\Utility\ITimeFactory;
  18. use OCP\Defaults;
  19. use OCP\IConfig;
  20. use OCP\IGroupManager;
  21. use OCP\IURLGenerator;
  22. use OCP\IUser;
  23. use OCP\IUserManager;
  24. use OCP\L10N\IFactory;
  25. use OCP\Mail\IMailer;
  26. use OCP\Security\ICrypto;
  27. use OCP\Security\ISecureRandom;
  28. use OCP\User\Events\UserDeletedEvent;
  29. use OCP\Util;
  30. use Psr\Container\ContainerInterface;
  31. class Application extends App implements IBootstrap {
  32. public function __construct(array $urlParams = []) {
  33. parent::__construct('provisioning_api', $urlParams);
  34. }
  35. public function register(IRegistrationContext $context): void {
  36. $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
  37. $context->registerService(NewUserMailHelper::class, function (ContainerInterface $c) {
  38. return new NewUserMailHelper(
  39. $c->get(Defaults::class),
  40. $c->get(IURLGenerator::class),
  41. $c->get(IFactory::class),
  42. $c->get(IMailer::class),
  43. $c->get(ISecureRandom::class),
  44. $c->get(ITimeFactory::class),
  45. $c->get(IConfig::class),
  46. $c->get(ICrypto::class),
  47. Util::getDefaultEmailAddress('no-reply')
  48. );
  49. });
  50. $context->registerService(ProvisioningApiMiddleware::class, function (ContainerInterface $c) {
  51. $user = $c->get(IUserManager::class)->get($c->get('UserId'));
  52. $isAdmin = false;
  53. $isSubAdmin = false;
  54. if ($user instanceof IUser) {
  55. $groupManager = $c->get(IGroupManager::class);
  56. assert($groupManager instanceof GroupManager);
  57. $isAdmin = $groupManager->isAdmin($user->getUID());
  58. $isSubAdmin = $groupManager->getSubAdmin()->isSubAdmin($user);
  59. }
  60. return new ProvisioningApiMiddleware(
  61. $c->get(IControllerMethodReflector::class),
  62. $isAdmin,
  63. $isSubAdmin
  64. );
  65. });
  66. $context->registerMiddleware(ProvisioningApiMiddleware::class);
  67. $context->registerCapability(Capabilities::class);
  68. }
  69. public function boot(IBootContext $context): void {
  70. }
  71. }