ProfileApiController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Controller;
  26. use OC\Core\Db\ProfileConfigMapper;
  27. use OC\Profile\ProfileManager;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCS\OCSBadRequestException;
  31. use OCP\AppFramework\OCS\OCSForbiddenException;
  32. use OCP\AppFramework\OCS\OCSNotFoundException;
  33. use OCP\AppFramework\OCSController;
  34. use OCP\IRequest;
  35. use OCP\IUserManager;
  36. use OCP\IUserSession;
  37. class ProfileApiController extends OCSController {
  38. public function __construct(
  39. IRequest $request,
  40. private ProfileConfigMapper $configMapper,
  41. private ProfileManager $profileManager,
  42. private IUserManager $userManager,
  43. private IUserSession $userSession,
  44. ) {
  45. parent::__construct('core', $request);
  46. }
  47. /**
  48. * @NoAdminRequired
  49. * @NoSubAdminRequired
  50. * @PasswordConfirmationRequired
  51. * @UserRateThrottle(limit=40, period=600)
  52. *
  53. * Update the visibility of a parameter
  54. *
  55. * @param string $targetUserId ID of the user
  56. * @param string $paramId ID of the parameter
  57. * @param string $visibility New visibility
  58. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  59. * @throws OCSBadRequestException Updating visibility is not possible
  60. * @throws OCSForbiddenException Not allowed to edit other users visibility
  61. * @throws OCSNotFoundException User not found
  62. *
  63. * 200: Visibility updated successfully
  64. */
  65. public function setVisibility(string $targetUserId, string $paramId, string $visibility): DataResponse {
  66. $requestingUser = $this->userSession->getUser();
  67. $targetUser = $this->userManager->get($targetUserId);
  68. if (!$this->userManager->userExists($targetUserId)) {
  69. throw new OCSNotFoundException('User does not exist');
  70. }
  71. if ($requestingUser !== $targetUser) {
  72. throw new OCSForbiddenException('Users can only edit their own visibility settings');
  73. }
  74. // Ensure that a profile config is created in the database
  75. $this->profileManager->getProfileConfig($targetUser, $targetUser);
  76. $config = $this->configMapper->get($targetUserId);
  77. if (!in_array($paramId, array_keys($config->getVisibilityMap()), true)) {
  78. throw new OCSBadRequestException('User does not have a profile parameter with ID: ' . $paramId);
  79. }
  80. $config->setVisibility($paramId, $visibility);
  81. $this->configMapper->update($config);
  82. return new DataResponse();
  83. }
  84. }