AjaxController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Ross Nicoll <jrn@jrn.me.uk>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_External\Controller;
  27. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\Response;
  31. use OCP\IGroupManager;
  32. use OCP\IRequest;
  33. use OCP\AppFramework\Http\JSONResponse;
  34. use OCA\Files_External\Lib\Auth\PublicKey\RSA;
  35. use OCP\IUserSession;
  36. class AjaxController extends Controller {
  37. /** @var RSA */
  38. private $rsaMechanism;
  39. /** @var GlobalAuth */
  40. private $globalAuth;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var IGroupManager */
  44. private $groupManager;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param RSA $rsaMechanism
  49. * @param GlobalAuth $globalAuth
  50. * @param IUserSession $userSession
  51. * @param IGroupManager $groupManager
  52. */
  53. public function __construct($appName,
  54. IRequest $request,
  55. RSA $rsaMechanism,
  56. GlobalAuth $globalAuth,
  57. IUserSession $userSession,
  58. IGroupManager $groupManager) {
  59. parent::__construct($appName, $request);
  60. $this->rsaMechanism = $rsaMechanism;
  61. $this->globalAuth = $globalAuth;
  62. $this->userSession = $userSession;
  63. $this->groupManager = $groupManager;
  64. }
  65. /**
  66. * @return array
  67. */
  68. private function generateSshKeys() {
  69. $key = $this->rsaMechanism->createKey();
  70. // Replace the placeholder label with a more meaningful one
  71. $key['publicKey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']);
  72. return $key;
  73. }
  74. /**
  75. * Generates an SSH public/private key pair.
  76. *
  77. * @NoAdminRequired
  78. */
  79. public function getSshKeys() {
  80. $key = $this->generateSshKeys();
  81. return new JSONResponse(
  82. array('data' => array(
  83. 'private_key' => $key['privatekey'],
  84. 'public_key' => $key['publickey']
  85. ),
  86. 'status' => 'success'
  87. ));
  88. }
  89. /**
  90. * @NoAdminRequired
  91. *
  92. * @param string $uid
  93. * @param string $user
  94. * @param string $password
  95. * @return bool
  96. */
  97. public function saveGlobalCredentials($uid, $user, $password) {
  98. $currentUser = $this->userSession->getUser();
  99. // Non-admins can only edit their own credentials
  100. $allowedToEdit = (
  101. $this->groupManager->isAdmin($currentUser->getUID()) || $currentUser->getUID() === $uid
  102. ) ? true : false;
  103. if ($allowedToEdit) {
  104. $this->globalAuth->saveAuth($uid, $user, $password);
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. }