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.

Security.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. class Security implements ISettings {
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var TwoFactorManager */
  46. private $twoFactorManager;
  47. /** @var IAuthTokenProvider */
  48. private $tokenProvider;
  49. /** @var ProviderLoader */
  50. private $providerLoader;
  51. /** @var IUserSession */
  52. private $userSession;
  53. /** @var ISession */
  54. private $session;
  55. /** @var IInitialStateService */
  56. private $initialStateService;
  57. /**
  58. * @var string|null
  59. */
  60. private $uid;
  61. public function __construct(IUserManager $userManager,
  62. TwoFactorManager $providerManager,
  63. IAuthTokenProvider $tokenProvider,
  64. ProviderLoader $providerLoader,
  65. IUserSession $userSession,
  66. ISession $session,
  67. IInitialStateService $initialStateService,
  68. ?string $UserId) {
  69. $this->userManager = $userManager;
  70. $this->twoFactorManager = $providerManager;
  71. $this->tokenProvider = $tokenProvider;
  72. $this->providerLoader = $providerLoader;
  73. $this->userSession = $userSession;
  74. $this->session = $session;
  75. $this->initialStateService = $initialStateService;
  76. $this->uid = $UserId;
  77. }
  78. /**
  79. * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
  80. * @since 9.1
  81. */
  82. public function getForm() {
  83. $user = $this->userManager->get($this->uid);
  84. $passwordChangeSupported = false;
  85. if ($user !== null) {
  86. $passwordChangeSupported = $user->canChangePassword();
  87. }
  88. $this->initialStateService->provideInitialState(
  89. 'settings',
  90. 'app_tokens',
  91. $this->getAppTokens()
  92. );
  93. return new TemplateResponse('settings', 'settings/personal/security', [
  94. 'passwordChangeSupported' => $passwordChangeSupported,
  95. 'twoFactorProviderData' => $this->getTwoFactorProviderData(),
  96. ]);
  97. }
  98. /**
  99. * @return string the section ID, e.g. 'sharing'
  100. * @since 9.1
  101. */
  102. public function getSection() {
  103. return 'security';
  104. }
  105. /**
  106. * @return int whether the form should be rather on the top or bottom of
  107. * the admin section. The forms are arranged in ascending order of the
  108. * priority values. It is required to return a value between 0 and 100.
  109. *
  110. * E.g.: 70
  111. * @since 9.1
  112. */
  113. public function getPriority() {
  114. return 10;
  115. }
  116. private function getTwoFactorProviderData(): array {
  117. $user = $this->userSession->getUser();
  118. if (is_null($user)) {
  119. // Actually impossible, but still …
  120. return [];
  121. }
  122. return [
  123. 'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) {
  124. return [
  125. 'provider' => $provider,
  126. 'settings' => $provider->getPersonalSettings($user)
  127. ];
  128. }, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) {
  129. return $provider instanceof IProvidesPersonalSettings;
  130. }))
  131. ];
  132. }
  133. private function getAppTokens(): array {
  134. $tokens = $this->tokenProvider->getTokenByUser($this->uid);
  135. try {
  136. $sessionId = $this->session->getId();
  137. } catch (SessionNotAvailableException $ex) {
  138. return [];
  139. }
  140. try {
  141. $sessionToken = $this->tokenProvider->getToken($sessionId);
  142. } catch (InvalidTokenException $ex) {
  143. return [];
  144. }
  145. return array_map(function (IToken $token) use ($sessionToken) {
  146. $data = $token->jsonSerialize();
  147. $data['canDelete'] = true;
  148. $data['canRename'] = $token instanceof INamedToken;
  149. if ($sessionToken->getId() === $token->getId()) {
  150. $data['canDelete'] = false;
  151. $data['canRename'] = false;
  152. $data['current'] = true;
  153. }
  154. return $data;
  155. }, $tokens);
  156. }
  157. }