You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.6 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Greta Doci <gretadoci@gmail.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Settings\Personal;
  28. use function array_filter;
  29. use function array_map;
  30. use function is_null;
  31. use OC\Authentication\Exceptions\InvalidTokenException;
  32. use OC\Authentication\Token\INamedToken;
  33. use OC\Authentication\Token\IProvider as IAuthTokenProvider;
  34. use OC\Authentication\Token\IToken;
  35. use OC\Authentication\TwoFactorAuth\Manager as TwoFactorManager;
  36. use OC\Authentication\TwoFactorAuth\ProviderLoader;
  37. use OCP\AppFramework\Http\TemplateResponse;
  38. use OCP\Authentication\TwoFactorAuth\IProvider;
  39. use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings;
  40. use OCP\IConfig;
  41. use OCP\IInitialStateService;
  42. use OCP\ISession;
  43. use OCP\IUserManager;
  44. use OCP\IUserSession;
  45. use OCP\Session\Exceptions\SessionNotAvailableException;
  46. use OCP\Settings\ISettings;
  47. class Security implements ISettings {
  48. /** @var IUserManager */
  49. private $userManager;
  50. /** @var ProviderLoader */
  51. private $providerLoader;
  52. /** @var IUserSession */
  53. private $userSession;
  54. /** @var string|null */
  55. private $uid;
  56. /** @var IConfig */
  57. private $config;
  58. public function __construct(IUserManager $userManager,
  59. ProviderLoader $providerLoader,
  60. IUserSession $userSession,
  61. IConfig $config,
  62. ?string $UserId) {
  63. $this->userManager = $userManager;
  64. $this->providerLoader = $providerLoader;
  65. $this->userSession = $userSession;
  66. $this->uid = $UserId;
  67. $this->config = $config;
  68. }
  69. public function getForm(): TemplateResponse {
  70. $user = $this->userManager->get($this->uid);
  71. $passwordChangeSupported = false;
  72. if ($user !== null) {
  73. $passwordChangeSupported = $user->canChangePassword();
  74. }
  75. return new TemplateResponse('settings', 'settings/personal/security', [
  76. 'passwordChangeSupported' => $passwordChangeSupported,
  77. 'twoFactorProviderData' => $this->getTwoFactorProviderData(),
  78. 'themedark' => $this->config->getUserValue($this->uid, 'accessibility', 'theme', false)
  79. ]);
  80. }
  81. public function getSection(): string {
  82. return 'security';
  83. }
  84. public function getPriority(): int {
  85. return 10;
  86. }
  87. private function getTwoFactorProviderData(): array {
  88. $user = $this->userSession->getUser();
  89. if (is_null($user)) {
  90. // Actually impossible, but still …
  91. return [];
  92. }
  93. return [
  94. 'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) {
  95. return [
  96. 'provider' => $provider,
  97. 'settings' => $provider->getPersonalSettings($user)
  98. ];
  99. }, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) {
  100. return $provider instanceof IProvidesPersonalSettings;
  101. }))
  102. ];
  103. }
  104. }