1
0

ProfileApiController.php 2.9 KB

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