Application.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author zulan <git@zulan.net>
  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\Provisioning_API\AppInfo;
  29. use OC\Group\Manager as GroupManager;
  30. use OCA\Provisioning_API\Capabilities;
  31. use OCA\Provisioning_API\Listener\UserDeletedListener;
  32. use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware;
  33. use OCA\Settings\Mailer\NewUserMailHelper;
  34. use OCP\AppFramework\App;
  35. use OCP\AppFramework\Bootstrap\IBootContext;
  36. use OCP\AppFramework\Bootstrap\IBootstrap;
  37. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  38. use OCP\AppFramework\Utility\IControllerMethodReflector;
  39. use OCP\AppFramework\Utility\ITimeFactory;
  40. use OCP\Defaults;
  41. use OCP\IConfig;
  42. use OCP\IGroupManager;
  43. use OCP\IURLGenerator;
  44. use OCP\IUser;
  45. use OCP\IUserManager;
  46. use OCP\L10N\IFactory;
  47. use OCP\Mail\IMailer;
  48. use OCP\Security\ICrypto;
  49. use OCP\Security\ISecureRandom;
  50. use OCP\User\Events\UserDeletedEvent;
  51. use OCP\Util;
  52. use Psr\Container\ContainerInterface;
  53. class Application extends App implements IBootstrap {
  54. public function __construct(array $urlParams = []) {
  55. parent::__construct('provisioning_api', $urlParams);
  56. }
  57. public function register(IRegistrationContext $context): void {
  58. $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
  59. $context->registerService(NewUserMailHelper::class, function (ContainerInterface $c) {
  60. return new NewUserMailHelper(
  61. $c->get(Defaults::class),
  62. $c->get(IURLGenerator::class),
  63. $c->get(IFactory::class),
  64. $c->get(IMailer::class),
  65. $c->get(ISecureRandom::class),
  66. $c->get(ITimeFactory::class),
  67. $c->get(IConfig::class),
  68. $c->get(ICrypto::class),
  69. Util::getDefaultEmailAddress('no-reply')
  70. );
  71. });
  72. $context->registerService(ProvisioningApiMiddleware::class, function (ContainerInterface $c) {
  73. $user = $c->get(IUserManager::class)->get($c->get('UserId'));
  74. $isAdmin = false;
  75. $isSubAdmin = false;
  76. if ($user instanceof IUser) {
  77. $groupManager = $c->get(IGroupManager::class);
  78. assert($groupManager instanceof GroupManager);
  79. $isAdmin = $groupManager->isAdmin($user->getUID());
  80. $isSubAdmin = $groupManager->getSubAdmin()->isSubAdmin($user);
  81. }
  82. return new ProvisioningApiMiddleware(
  83. $c->get(IControllerMethodReflector::class),
  84. $isAdmin,
  85. $isSubAdmin
  86. );
  87. });
  88. $context->registerMiddleware(ProvisioningApiMiddleware::class);
  89. $context->registerCapability(Capabilities::class);
  90. }
  91. public function boot(IBootContext $context): void {
  92. }
  93. }