UserGroupMembership.php 3.0 KB

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