1
0

ProfileApiController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /** @var ProfileConfigMapper */
  37. private $configMapper;
  38. /** @var ProfileManager */
  39. private $profileManager;
  40. /** @var IUserManager */
  41. private $userManager;
  42. /** @var IUserSession */
  43. private $userSession;
  44. public function __construct(
  45. IRequest $request,
  46. ProfileConfigMapper $configMapper,
  47. ProfileManager $profileManager,
  48. IUserManager $userManager,
  49. IUserSession $userSession
  50. ) {
  51. parent::__construct('core', $request);
  52. $this->configMapper = $configMapper;
  53. $this->profileManager = $profileManager;
  54. $this->userManager = $userManager;
  55. $this->userSession = $userSession;
  56. }
  57. /**
  58. * @NoAdminRequired
  59. * @NoSubAdminRequired
  60. * @PasswordConfirmationRequired
  61. * @UserRateThrottle(limit=40, period=600)
  62. */
  63. public function setVisibility(string $targetUserId, string $paramId, string $visibility): DataResponse {
  64. $requestingUser = $this->userSession->getUser();
  65. $targetUser = $this->userManager->get($targetUserId);
  66. if (!$this->userManager->userExists($targetUserId)) {
  67. throw new OCSNotFoundException('User does not exist');
  68. }
  69. if ($requestingUser !== $targetUser) {
  70. throw new OCSForbiddenException('Users can only edit their own visibility settings');
  71. }
  72. // Ensure that a profile config is created in the database
  73. $this->profileManager->getProfileConfig($targetUser, $targetUser);
  74. $config = $this->configMapper->get($targetUserId);
  75. if (!in_array($paramId, array_keys($config->getVisibilityMap()), true)) {
  76. throw new OCSBadRequestException('User does not have a profile parameter with ID: ' . $paramId);
  77. }
  78. $config->setVisibility($paramId, $visibility);
  79. $this->configMapper->update($config);
  80. return new DataResponse();
  81. }
  82. }