1
0

ProfileApiController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Core\Db\ProfileConfigMapper;
  9. use OC\Profile\ProfileManager;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCS\OCSBadRequestException;
  14. use OCP\AppFramework\OCS\OCSForbiddenException;
  15. use OCP\AppFramework\OCS\OCSNotFoundException;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\IRequest;
  18. use OCP\IUserManager;
  19. use OCP\IUserSession;
  20. class ProfileApiController extends OCSController {
  21. public function __construct(
  22. IRequest $request,
  23. private ProfileConfigMapper $configMapper,
  24. private ProfileManager $profileManager,
  25. private IUserManager $userManager,
  26. private IUserSession $userSession,
  27. ) {
  28. parent::__construct('core', $request);
  29. }
  30. /**
  31. * @NoAdminRequired
  32. * @NoSubAdminRequired
  33. * @PasswordConfirmationRequired
  34. * @UserRateThrottle(limit=40, period=600)
  35. *
  36. * Update the visibility of a parameter
  37. *
  38. * @param string $targetUserId ID of the user
  39. * @param string $paramId ID of the parameter
  40. * @param string $visibility New visibility
  41. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  42. * @throws OCSBadRequestException Updating visibility is not possible
  43. * @throws OCSForbiddenException Not allowed to edit other users visibility
  44. * @throws OCSNotFoundException Account not found
  45. *
  46. * 200: Visibility updated successfully
  47. */
  48. #[ApiRoute(verb: 'PUT', url: '/{targetUserId}', root: '/profile')]
  49. public function setVisibility(string $targetUserId, string $paramId, string $visibility): DataResponse {
  50. $requestingUser = $this->userSession->getUser();
  51. $targetUser = $this->userManager->get($targetUserId);
  52. if (!$this->userManager->userExists($targetUserId)) {
  53. throw new OCSNotFoundException('Account does not exist');
  54. }
  55. if ($requestingUser !== $targetUser) {
  56. throw new OCSForbiddenException('People can only edit their own visibility settings');
  57. }
  58. // Ensure that a profile config is created in the database
  59. $this->profileManager->getProfileConfig($targetUser, $targetUser);
  60. $config = $this->configMapper->get($targetUserId);
  61. if (!in_array($paramId, array_keys($config->getVisibilityMap()), true)) {
  62. throw new OCSBadRequestException('Account does not have a profile parameter with ID: ' . $paramId);
  63. }
  64. $config->setVisibility($paramId, $visibility);
  65. $this->configMapper->update($config);
  66. return new DataResponse();
  67. }
  68. }