TwoFactor.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Settings\Personal\Security;
  8. use Exception;
  9. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  10. use OC\Authentication\TwoFactorAuth\ProviderLoader;
  11. use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
  12. use OCP\AppFramework\Http\TemplateResponse;
  13. use OCP\Authentication\TwoFactorAuth\IProvider;
  14. use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings;
  15. use OCP\IConfig;
  16. use OCP\IUserSession;
  17. use OCP\Settings\ISettings;
  18. use function array_filter;
  19. use function array_map;
  20. use function is_null;
  21. class TwoFactor implements ISettings {
  22. /** @var ProviderLoader */
  23. private $providerLoader;
  24. /** @var MandatoryTwoFactor */
  25. private $mandatoryTwoFactor;
  26. /** @var IUserSession */
  27. private $userSession;
  28. /** @var string|null */
  29. private $uid;
  30. /** @var IConfig */
  31. private $config;
  32. public function __construct(ProviderLoader $providerLoader,
  33. MandatoryTwoFactor $mandatoryTwoFactor,
  34. IUserSession $userSession,
  35. IConfig $config,
  36. ?string $UserId) {
  37. $this->providerLoader = $providerLoader;
  38. $this->mandatoryTwoFactor = $mandatoryTwoFactor;
  39. $this->userSession = $userSession;
  40. $this->uid = $UserId;
  41. $this->config = $config;
  42. }
  43. public function getForm(): TemplateResponse {
  44. return new TemplateResponse('settings', 'settings/personal/security/twofactor', [
  45. 'twoFactorProviderData' => $this->getTwoFactorProviderData(),
  46. ]);
  47. }
  48. public function getSection(): ?string {
  49. if (!$this->shouldShow()) {
  50. return null;
  51. }
  52. return 'security';
  53. }
  54. public function getPriority(): int {
  55. return 15;
  56. }
  57. private function shouldShow(): bool {
  58. $user = $this->userSession->getUser();
  59. if (is_null($user)) {
  60. // Actually impossible, but still …
  61. return false;
  62. }
  63. // Anyone who's supposed to use 2FA should see 2FA settings
  64. if ($this->mandatoryTwoFactor->isEnforcedFor($user)) {
  65. return true;
  66. }
  67. // If there is at least one provider with personal settings but it's not
  68. // the backup codes provider, then these settings should show.
  69. try {
  70. $providers = $this->providerLoader->getProviders($user);
  71. } catch (Exception $e) {
  72. // Let's hope for the best
  73. return true;
  74. }
  75. foreach ($providers as $provider) {
  76. if ($provider instanceof IProvidesPersonalSettings
  77. && !($provider instanceof BackupCodesProvider)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. private function getTwoFactorProviderData(): array {
  84. $user = $this->userSession->getUser();
  85. if (is_null($user)) {
  86. // Actually impossible, but still …
  87. return [];
  88. }
  89. return [
  90. 'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) {
  91. return [
  92. 'provider' => $provider,
  93. 'settings' => $provider->getPersonalSettings($user)
  94. ];
  95. }, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) {
  96. return $provider instanceof IProvidesPersonalSettings;
  97. }))
  98. ];
  99. }
  100. }