|
7 | 7 |
|
8 | 8 | namespace OCA\Tables\Service\ColumnTypes; |
9 | 9 |
|
| 10 | +use OCA\Tables\Constants\UsergroupType; |
10 | 11 | use OCA\Tables\Db\Column; |
| 12 | +use OCA\Tables\Errors\BadRequestError; |
| 13 | +use OCA\Tables\Helper\CircleHelper; |
| 14 | +use OCP\IGroupManager; |
| 15 | +use OCP\IUserManager; |
| 16 | +use Psr\Log\LoggerInterface; |
11 | 17 |
|
12 | 18 | class UsergroupBusiness extends SuperBusiness { |
13 | 19 |
|
| 20 | + public function __construct( |
| 21 | + protected LoggerInterface $logger, |
| 22 | + protected CircleHelper $circleHelper, |
| 23 | + protected IUserManager $userManager, |
| 24 | + protected IGroupManager $groupManager, |
| 25 | + ) { |
| 26 | + parent::__construct($logger); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param mixed $value |
| 31 | + * @param Column $column |
| 32 | + * @param string $userId |
| 33 | + * @param int $tableId |
| 34 | + * @param int|null $rowId |
| 35 | + * |
| 36 | + * @throws BadRequestError |
| 37 | + */ |
| 38 | + public function validateValue(mixed $value, Column $column, string $userId, int $tableId, ?int $rowId): void { |
| 39 | + if ($value === null) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (is_string($value)) { |
| 44 | + $value = json_decode($value, true); |
| 45 | + } |
| 46 | + |
| 47 | + if (!is_array($value)) { |
| 48 | + throw new BadRequestError('Invalid value for usergroup column'); |
| 49 | + } |
| 50 | + |
| 51 | + foreach ($value as $userGroupEntry) { |
| 52 | + if (!isset($userGroupEntry['id']) || !is_string($userGroupEntry['id'])) { |
| 53 | + throw new BadRequestError('Invalid value for usergroup id'); |
| 54 | + } |
| 55 | + if (!isset($userGroupEntry['type']) || !is_int($userGroupEntry['type'])) { |
| 56 | + throw new BadRequestError('Invalid usergroup type'); |
| 57 | + } |
| 58 | + if ($userGroupEntry['type'] === UsergroupType::USER) { |
| 59 | + if (!$this->userManager->get($userGroupEntry['id'])) { |
| 60 | + throw new BadRequestError('User not found'); |
| 61 | + } |
| 62 | + } |
| 63 | + if ($userGroupEntry['type'] === UsergroupType::GROUP) { |
| 64 | + if (!$this->groupManager->get($userGroupEntry['id'])) { |
| 65 | + throw new BadRequestError('Group not found'); |
| 66 | + } |
| 67 | + } |
| 68 | + if ($userGroupEntry['type'] === UsergroupType::CIRCLE) { |
| 69 | + if (!$this->circleHelper->circleExists($userGroupEntry['id'], $userId)) { |
| 70 | + throw new BadRequestError('Circle not found'); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
14 | 76 | /** |
15 | 77 | * Parse frontend value (array) and transform for using it in the database (array) |
16 | 78 | * Uses json encode just because wrapping methods currently all assume that |
|
0 commit comments