Security.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Settings\Personal;
  24. use function array_filter;
  25. use function array_map;
  26. use function is_null;
  27. use OC\Authentication\Exceptions\InvalidTokenException;
  28. use OC\Authentication\Token\INamedToken;
  29. use OC\Authentication\Token\IProvider as IAuthTokenProvider;
  30. use OC\Authentication\Token\IToken;
  31. use OC\Authentication\TwoFactorAuth\Manager as TwoFactorManager;
  32. use OC\Authentication\TwoFactorAuth\ProviderLoader;
  33. use OCP\AppFramework\Http\TemplateResponse;
  34. use OCP\Authentication\TwoFactorAuth\IProvider;
  35. use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings;
  36. use OCP\IInitialStateService;
  37. use OCP\ISession;
  38. use OCP\IUserManager;
  39. use OCP\IUserSession;
  40. use OCP\Session\Exceptions\SessionNotAvailableException;
  41. use OCP\Settings\ISettings;
  42. use OCP\IConfig;
  43. class Security implements ISettings {
  44. /** @var IUserManager */
  45. private $userManager;
  46. /** @var TwoFactorManager */
  47. private $twoFactorManager;
  48. /** @var IAuthTokenProvider */
  49. private $tokenProvider;
  50. /** @var ProviderLoader */
  51. private $providerLoader;
  52. /** @var IUserSession */
  53. private $userSession;
  54. /** @var ISession */
  55. private $session;
  56. /** @var IInitialStateService */
  57. private $initialStateService;
  58. /**
  59. * @var string|null
  60. */
  61. private $uid;
  62. /**
  63. *@var IConfig
  64. */
  65. private $config;
  66. public function __construct(IUserManager $userManager,
  67. TwoFactorManager $providerManager,
  68. IAuthTokenProvider $tokenProvider,
  69. ProviderLoader $providerLoader,
  70. IUserSession $userSession,
  71. ISession $session,
  72. IConfig $config,
  73. IInitialStateService $initialStateService,
  74. ?string $UserId) {
  75. $this->userManager = $userManager;
  76. $this->twoFactorManager = $providerManager;
  77. $this->tokenProvider = $tokenProvider;
  78. $this->providerLoader = $providerLoader;
  79. $this->userSession = $userSession;
  80. $this->session = $session;
  81. $this->initialStateService = $initialStateService;
  82. $this->uid = $UserId;
  83. $this->config = $config;
  84. }
  85. /**
  86. * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
  87. * @since 9.1
  88. */
  89. public function getForm() {
  90. $user = $this->userManager->get($this->uid);
  91. $passwordChangeSupported = false;
  92. if ($user !== null) {
  93. $passwordChangeSupported = $user->canChangePassword();
  94. }
  95. $this->initialStateService->provideInitialState(
  96. 'settings',
  97. 'app_tokens',
  98. $this->getAppTokens()
  99. );
  100. return new TemplateResponse('settings', 'settings/personal/security', [
  101. 'passwordChangeSupported' => $passwordChangeSupported,
  102. 'twoFactorProviderData' => $this->getTwoFactorProviderData(),
  103. 'themedark' => $this->config->getUserValue($this->uid, 'accessibility', 'theme', false)
  104. ]);
  105. }
  106. /**
  107. * @return string the section ID, e.g. 'sharing'
  108. * @since 9.1
  109. */
  110. public function getSection() {
  111. return 'security';
  112. }
  113. /**
  114. * @return int whether the form should be rather on the top or bottom of
  115. * the admin section. The forms are arranged in ascending order of the
  116. * priority values. It is required to return a value between 0 and 100.
  117. *
  118. * E.g.: 70
  119. * @since 9.1
  120. */
  121. public function getPriority() {
  122. return 10;
  123. }
  124. private function getTwoFactorProviderData(): array {
  125. $user = $this->userSession->getUser();
  126. if (is_null($user)) {
  127. // Actually impossible, but still …
  128. return [];
  129. }
  130. return [
  131. 'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) {
  132. return [
  133. 'provider' => $provider,
  134. 'settings' => $provider->getPersonalSettings($user)
  135. ];
  136. }, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) {
  137. return $provider instanceof IProvidesPersonalSettings;
  138. }))
  139. ];
  140. }
  141. private function getAppTokens(): array {
  142. $tokens = $this->tokenProvider->getTokenByUser($this->uid);
  143. try {
  144. $sessionId = $this->session->getId();
  145. } catch (SessionNotAvailableException $ex) {
  146. return [];
  147. }
  148. try {
  149. $sessionToken = $this->tokenProvider->getToken($sessionId);
  150. } catch (InvalidTokenException $ex) {
  151. return [];
  152. }
  153. return array_map(function (IToken $token) use ($sessionToken) {
  154. $data = $token->jsonSerialize();
  155. $data['canDelete'] = true;
  156. $data['canRename'] = $token instanceof INamedToken;
  157. if ($sessionToken->getId() === $token->getId()) {
  158. $data['canDelete'] = false;
  159. $data['canRename'] = false;
  160. $data['current'] = true;
  161. }
  162. return $data;
  163. }, $tokens);
  164. }
  165. }