@@ -42,96 +42,96 @@ |
||
| 42 | 42 | |
| 43 | 43 | class TwoFactor implements ISettings { |
| 44 | 44 | |
| 45 | - /** @var ProviderLoader */ |
|
| 46 | - private $providerLoader; |
|
| 47 | - |
|
| 48 | - /** @var MandatoryTwoFactor */ |
|
| 49 | - private $mandatoryTwoFactor; |
|
| 50 | - |
|
| 51 | - /** @var IUserSession */ |
|
| 52 | - private $userSession; |
|
| 53 | - |
|
| 54 | - /** @var string|null */ |
|
| 55 | - private $uid; |
|
| 56 | - |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - private $config; |
|
| 59 | - |
|
| 60 | - public function __construct(ProviderLoader $providerLoader, |
|
| 61 | - MandatoryTwoFactor $mandatoryTwoFactor, |
|
| 62 | - IUserSession $userSession, |
|
| 63 | - IConfig $config, |
|
| 64 | - ?string $UserId) { |
|
| 65 | - $this->providerLoader = $providerLoader; |
|
| 66 | - $this->mandatoryTwoFactor = $mandatoryTwoFactor; |
|
| 67 | - $this->userSession = $userSession; |
|
| 68 | - $this->uid = $UserId; |
|
| 69 | - $this->config = $config; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function getForm(): TemplateResponse { |
|
| 73 | - return new TemplateResponse('settings', 'settings/personal/security/twofactor', [ |
|
| 74 | - 'twoFactorProviderData' => $this->getTwoFactorProviderData(), |
|
| 75 | - 'themedark' => $this->config->getUserValue($this->uid, 'accessibility', 'theme', false) |
|
| 76 | - ]); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - public function getSection(): ?string { |
|
| 80 | - if (!$this->shouldShow()) { |
|
| 81 | - return null; |
|
| 82 | - } |
|
| 83 | - return 'security'; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function getPriority(): int { |
|
| 87 | - return 15; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - private function shouldShow(): bool { |
|
| 91 | - $user = $this->userSession->getUser(); |
|
| 92 | - if (is_null($user)) { |
|
| 93 | - // Actually impossible, but still … |
|
| 94 | - return false; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Anyone who's supposed to use 2FA should see 2FA settings |
|
| 98 | - if ($this->mandatoryTwoFactor->isEnforcedFor($user)) { |
|
| 99 | - return true; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // If there is at least one provider with personal settings but it's not |
|
| 103 | - // the backup codes provider, then these settings should show. |
|
| 104 | - try { |
|
| 105 | - $providers = $this->providerLoader->getProviders($user); |
|
| 106 | - } catch (Exception $e) { |
|
| 107 | - // Let's hope for the best |
|
| 108 | - return true; |
|
| 109 | - } |
|
| 110 | - foreach ($providers as $provider) { |
|
| 111 | - if ($provider instanceof IProvidesPersonalSettings |
|
| 112 | - && !($provider instanceof BackupCodesProvider)) { |
|
| 113 | - return true; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - return false; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - private function getTwoFactorProviderData(): array { |
|
| 120 | - $user = $this->userSession->getUser(); |
|
| 121 | - if (is_null($user)) { |
|
| 122 | - // Actually impossible, but still … |
|
| 123 | - return []; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return [ |
|
| 127 | - 'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) { |
|
| 128 | - return [ |
|
| 129 | - 'provider' => $provider, |
|
| 130 | - 'settings' => $provider->getPersonalSettings($user) |
|
| 131 | - ]; |
|
| 132 | - }, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) { |
|
| 133 | - return $provider instanceof IProvidesPersonalSettings; |
|
| 134 | - })) |
|
| 135 | - ]; |
|
| 136 | - } |
|
| 45 | + /** @var ProviderLoader */ |
|
| 46 | + private $providerLoader; |
|
| 47 | + |
|
| 48 | + /** @var MandatoryTwoFactor */ |
|
| 49 | + private $mandatoryTwoFactor; |
|
| 50 | + |
|
| 51 | + /** @var IUserSession */ |
|
| 52 | + private $userSession; |
|
| 53 | + |
|
| 54 | + /** @var string|null */ |
|
| 55 | + private $uid; |
|
| 56 | + |
|
| 57 | + /** @var IConfig */ |
|
| 58 | + private $config; |
|
| 59 | + |
|
| 60 | + public function __construct(ProviderLoader $providerLoader, |
|
| 61 | + MandatoryTwoFactor $mandatoryTwoFactor, |
|
| 62 | + IUserSession $userSession, |
|
| 63 | + IConfig $config, |
|
| 64 | + ?string $UserId) { |
|
| 65 | + $this->providerLoader = $providerLoader; |
|
| 66 | + $this->mandatoryTwoFactor = $mandatoryTwoFactor; |
|
| 67 | + $this->userSession = $userSession; |
|
| 68 | + $this->uid = $UserId; |
|
| 69 | + $this->config = $config; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function getForm(): TemplateResponse { |
|
| 73 | + return new TemplateResponse('settings', 'settings/personal/security/twofactor', [ |
|
| 74 | + 'twoFactorProviderData' => $this->getTwoFactorProviderData(), |
|
| 75 | + 'themedark' => $this->config->getUserValue($this->uid, 'accessibility', 'theme', false) |
|
| 76 | + ]); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + public function getSection(): ?string { |
|
| 80 | + if (!$this->shouldShow()) { |
|
| 81 | + return null; |
|
| 82 | + } |
|
| 83 | + return 'security'; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function getPriority(): int { |
|
| 87 | + return 15; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + private function shouldShow(): bool { |
|
| 91 | + $user = $this->userSession->getUser(); |
|
| 92 | + if (is_null($user)) { |
|
| 93 | + // Actually impossible, but still … |
|
| 94 | + return false; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Anyone who's supposed to use 2FA should see 2FA settings |
|
| 98 | + if ($this->mandatoryTwoFactor->isEnforcedFor($user)) { |
|
| 99 | + return true; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // If there is at least one provider with personal settings but it's not |
|
| 103 | + // the backup codes provider, then these settings should show. |
|
| 104 | + try { |
|
| 105 | + $providers = $this->providerLoader->getProviders($user); |
|
| 106 | + } catch (Exception $e) { |
|
| 107 | + // Let's hope for the best |
|
| 108 | + return true; |
|
| 109 | + } |
|
| 110 | + foreach ($providers as $provider) { |
|
| 111 | + if ($provider instanceof IProvidesPersonalSettings |
|
| 112 | + && !($provider instanceof BackupCodesProvider)) { |
|
| 113 | + return true; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + return false; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + private function getTwoFactorProviderData(): array { |
|
| 120 | + $user = $this->userSession->getUser(); |
|
| 121 | + if (is_null($user)) { |
|
| 122 | + // Actually impossible, but still … |
|
| 123 | + return []; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return [ |
|
| 127 | + 'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) { |
|
| 128 | + return [ |
|
| 129 | + 'provider' => $provider, |
|
| 130 | + 'settings' => $provider->getPersonalSettings($user) |
|
| 131 | + ]; |
|
| 132 | + }, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) { |
|
| 133 | + return $provider instanceof IProvidesPersonalSettings; |
|
| 134 | + })) |
|
| 135 | + ]; |
|
| 136 | + } |
|
| 137 | 137 | } |
@@ -31,25 +31,25 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | interface ISettings { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @return TemplateResponse returns the instance with all parameters set, ready to be rendered |
|
| 36 | - * @since 9.1 |
|
| 37 | - */ |
|
| 38 | - public function getForm(); |
|
| 34 | + /** |
|
| 35 | + * @return TemplateResponse returns the instance with all parameters set, ready to be rendered |
|
| 36 | + * @since 9.1 |
|
| 37 | + */ |
|
| 38 | + public function getForm(); |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @return string|null the section ID, e.g. 'sharing' or null to not show the setting |
|
| 42 | - * @since 9.1 |
|
| 43 | - */ |
|
| 44 | - public function getSection(); |
|
| 40 | + /** |
|
| 41 | + * @return string|null the section ID, e.g. 'sharing' or null to not show the setting |
|
| 42 | + * @since 9.1 |
|
| 43 | + */ |
|
| 44 | + public function getSection(); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @return int whether the form should be rather on the top or bottom of |
|
| 48 | - * the admin section. The forms are arranged in ascending order of the |
|
| 49 | - * priority values. It is required to return a value between 0 and 100. |
|
| 50 | - * |
|
| 51 | - * E.g.: 70 |
|
| 52 | - * @since 9.1 |
|
| 53 | - */ |
|
| 54 | - public function getPriority(); |
|
| 46 | + /** |
|
| 47 | + * @return int whether the form should be rather on the top or bottom of |
|
| 48 | + * the admin section. The forms are arranged in ascending order of the |
|
| 49 | + * priority values. It is required to return a value between 0 and 100. |
|
| 50 | + * |
|
| 51 | + * E.g.: 70 |
|
| 52 | + * @since 9.1 |
|
| 53 | + */ |
|
| 54 | + public function getPriority(); |
|
| 55 | 55 | } |