You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserGroupMembership.php 2.9KB

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\IGroupManager;
  23. use OCP\IL10N;
  24. use OCP\IUser;
  25. use OCP\IUserSession;
  26. use OCP\WorkflowEngine\ICheck;
  27. use OCP\WorkflowEngine\IManager;
  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 string $operator
  51. * @param string $value
  52. * @return bool
  53. */
  54. public function executeCheck($operator, $value) {
  55. $user = $this->userSession->getUser();
  56. if ($user instanceof IUser) {
  57. $groupIds = $this->getUserGroups($user);
  58. return ($operator === 'is') === in_array($value, $groupIds);
  59. } else {
  60. return $operator !== 'is';
  61. }
  62. }
  63. /**
  64. * @param string $operator
  65. * @param string $value
  66. * @throws \UnexpectedValueException
  67. */
  68. public function validateCheck($operator, $value) {
  69. if (!in_array($operator, ['is', '!is'])) {
  70. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  71. }
  72. if (!$this->groupManager->groupExists($value)) {
  73. throw new \UnexpectedValueException($this->l->t('The given group does not exist'), 2);
  74. }
  75. }
  76. /**
  77. * @param IUser $user
  78. * @return string[]
  79. */
  80. protected function getUserGroups(IUser $user) {
  81. $uid = $user->getUID();
  82. if ($this->cachedUser !== $uid) {
  83. $this->cachedUser = $uid;
  84. $this->cachedGroupMemberships = $this->groupManager->getUserGroupIds($user);
  85. }
  86. return $this->cachedGroupMemberships;
  87. }
  88. public function supportedEntities(): array {
  89. // universal by default
  90. return [];
  91. }
  92. public function isAvailableForScope(int $scope): bool {
  93. // admin only by default
  94. return $scope === IManager::SCOPE_ADMIN;
  95. }
  96. }