ShareDisableChecker.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace OC\Share20;
  3. use OCP\Cache\CappedMemoryCache;
  4. use OCP\IConfig;
  5. use OCP\IGroupManager;
  6. use OCP\IUserManager;
  7. /**
  8. * split of from the share manager to allow using it with minimal DI
  9. */
  10. class ShareDisableChecker {
  11. private CappedMemoryCache $sharingDisabledForUsersCache;
  12. public function __construct(
  13. private IConfig $config,
  14. private IUserManager $userManager,
  15. private IGroupManager $groupManager,
  16. ) {
  17. $this->sharingDisabledForUsersCache = new CappedMemoryCache();
  18. }
  19. /**
  20. * @param ?string $userId
  21. * @return bool
  22. */
  23. public function sharingDisabledForUser(?string $userId) {
  24. if ($userId === null) {
  25. return false;
  26. }
  27. if (isset($this->sharingDisabledForUsersCache[$userId])) {
  28. return $this->sharingDisabledForUsersCache[$userId];
  29. }
  30. if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
  31. $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
  32. $excludedGroups = json_decode($groupsList);
  33. if (is_null($excludedGroups)) {
  34. $excludedGroups = explode(',', $groupsList);
  35. $newValue = json_encode($excludedGroups);
  36. $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
  37. }
  38. $user = $this->userManager->get($userId);
  39. if (!$user) {
  40. return false;
  41. }
  42. $usersGroups = $this->groupManager->getUserGroupIds($user);
  43. if (!empty($usersGroups)) {
  44. $remainingGroups = array_diff($usersGroups, $excludedGroups);
  45. // if the user is only in groups which are disabled for sharing then
  46. // sharing is also disabled for the user
  47. if (empty($remainingGroups)) {
  48. $this->sharingDisabledForUsersCache[$userId] = true;
  49. return true;
  50. }
  51. }
  52. }
  53. $this->sharingDisabledForUsersCache[$userId] = false;
  54. return false;
  55. }
  56. }