1
0

UserGroupMembership.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\Check;
  22. use OCP\Files\Storage\IStorage;
  23. use OCP\IGroupManager;
  24. use OCP\IL10N;
  25. use OCP\IUser;
  26. use OCP\IUserSession;
  27. use OCP\WorkflowEngine\ICheck;
  28. class UserGroupMembership implements ICheck {
  29. /** @var string */
  30. protected $cachedUser;
  31. /** @var string[] */
  32. protected $cachedGroupMemberships;
  33. /** @var IUserSession */
  34. protected $userSession;
  35. /** @var IGroupManager */
  36. protected $groupManager;
  37. /** @var IL10N */
  38. protected $l;
  39. /**
  40. * @param IUserSession $userSession
  41. * @param IGroupManager $groupManager
  42. * @param IL10N $l
  43. */
  44. public function __construct(IUserSession $userSession, IGroupManager $groupManager, IL10N $l) {
  45. $this->userSession = $userSession;
  46. $this->groupManager = $groupManager;
  47. $this->l = $l;
  48. }
  49. /**
  50. * @param IStorage $storage
  51. * @param string $path
  52. * @param bool $isDir
  53. */
  54. public function setFileInfo(IStorage $storage, $path, $isDir = false) {
  55. // A different path doesn't change group memberships, so nothing to do here.
  56. }
  57. /**
  58. * @param string $operator
  59. * @param string $value
  60. * @return bool
  61. */
  62. public function executeCheck($operator, $value) {
  63. $user = $this->userSession->getUser();
  64. if ($user instanceof IUser) {
  65. $groupIds = $this->getUserGroups($user);
  66. return ($operator === 'is') === in_array($value, $groupIds);
  67. } else {
  68. return $operator !== 'is';
  69. }
  70. }
  71. /**
  72. * @param string $operator
  73. * @param string $value
  74. * @throws \UnexpectedValueException
  75. */
  76. public function validateCheck($operator, $value) {
  77. if (!in_array($operator, ['is', '!is'])) {
  78. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  79. }
  80. if (!$this->groupManager->groupExists($value)) {
  81. throw new \UnexpectedValueException($this->l->t('The given group does not exist'), 2);
  82. }
  83. }
  84. /**
  85. * @param IUser $user
  86. * @return string[]
  87. */
  88. protected function getUserGroups(IUser $user) {
  89. $uid = $user->getUID();
  90. if ($this->cachedUser !== $uid) {
  91. $this->cachedUser = $uid;
  92. $this->cachedGroupMemberships = $this->groupManager->getUserGroupIds($user);
  93. }
  94. return $this->cachedGroupMemberships;
  95. }
  96. }