ShareDisableChecker.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $excludeGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no');
  31. if ($excludeGroups && $excludeGroups !== 'no') {
  32. $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
  33. $excludedGroups = json_decode($groupsList);
  34. if (is_null($excludedGroups)) {
  35. $excludedGroups = explode(',', $groupsList);
  36. $newValue = json_encode($excludedGroups);
  37. $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
  38. }
  39. $user = $this->userManager->get($userId);
  40. if (!$user) {
  41. return false;
  42. }
  43. $usersGroups = $this->groupManager->getUserGroupIds($user);
  44. if ($excludeGroups !== 'allow') {
  45. if (!empty($usersGroups)) {
  46. $remainingGroups = array_diff($usersGroups, $excludedGroups);
  47. // if the user is only in groups which are disabled for sharing then
  48. // sharing is also disabled for the user
  49. if (empty($remainingGroups)) {
  50. $this->sharingDisabledForUsersCache[$userId] = true;
  51. return true;
  52. }
  53. }
  54. } else {
  55. if (!empty($usersGroups)) {
  56. $remainingGroups = array_intersect($usersGroups, $excludedGroups);
  57. // if the user is in any group which is allowed for sharing then
  58. // sharing is also allowed for the user
  59. if (!empty($remainingGroups)) {
  60. $this->sharingDisabledForUsersCache[$userId] = false;
  61. return false;
  62. }
  63. }
  64. $this->sharingDisabledForUsersCache[$userId] = true;
  65. return true;
  66. }
  67. }
  68. $this->sharingDisabledForUsersCache[$userId] = false;
  69. return false;
  70. }
  71. }