ProfileApiController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. */
  58. public function setVisibility(string $targetUserId, string $paramId, string $visibility): DataResponse {
  59. $requestingUser = $this->userSession->getUser();
  60. $targetUser = $this->userManager->get($targetUserId);
  61. if (!$this->userManager->userExists($targetUserId)) {
  62. throw new OCSNotFoundException('User does not exist');
  63. }
  64. if ($requestingUser !== $targetUser) {
  65. throw new OCSForbiddenException('Users can only edit their own visibility settings');
  66. }
  67. // Ensure that a profile config is created in the database
  68. $this->profileManager->getProfileConfig($targetUser, $targetUser);
  69. $config = $this->configMapper->get($targetUserId);
  70. if (!in_array($paramId, array_keys($config->getVisibilityMap()), true)) {
  71. throw new OCSBadRequestException('User does not have a profile parameter with ID: ' . $paramId);
  72. }
  73. $config->setVisibility($paramId, $visibility);
  74. $this->configMapper->update($config);
  75. return new DataResponse();
  76. }
  77. }