1
0

UserGroupMembership.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. */
  53. public function setFileInfo(IStorage $storage, $path) {
  54. // A different path doesn't change group memberships, so nothing to do here.
  55. }
  56. /**
  57. * @param string $operator
  58. * @param string $value
  59. * @return bool
  60. */
  61. public function executeCheck($operator, $value) {
  62. $user = $this->userSession->getUser();
  63. if ($user instanceof IUser) {
  64. $groupIds = $this->getUserGroups($user);
  65. return ($operator === 'is') === in_array($value, $groupIds);
  66. } else {
  67. return $operator !== 'is';
  68. }
  69. }
  70. /**
  71. * @param string $operator
  72. * @param string $value
  73. * @throws \UnexpectedValueException
  74. */
  75. public function validateCheck($operator, $value) {
  76. if (!in_array($operator, ['is', '!is'])) {
  77. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  78. }
  79. if (!$this->groupManager->groupExists($value)) {
  80. throw new \UnexpectedValueException($this->l->t('The given group does not exist'), 2);
  81. }
  82. }
  83. /**
  84. * @param IUser $user
  85. * @return string[]
  86. */
  87. protected function getUserGroups(IUser $user) {
  88. $uid = $user->getUID();
  89. if ($this->cachedUser !== $uid) {
  90. $this->cachedUser = $uid;
  91. $this->cachedGroupMemberships = $this->groupManager->getUserGroupIds($user);
  92. }
  93. return $this->cachedGroupMemberships;
  94. }
  95. }