Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| CreateRoleEndpoint | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Roles\Endpoints; |
| 4 | |
| 5 | use Olz\Api\OlzCreateEntityTypedEndpoint; |
| 6 | use Olz\Entity\Roles\Role; |
| 7 | use Olz\Entity\Users\User; |
| 8 | use PhpTypeScriptApi\Fields\ValidationError; |
| 9 | use PhpTypeScriptApi\HttpError; |
| 10 | |
| 11 | /** |
| 12 | * @phpstan-import-type OlzRoleId from RoleEndpointTrait |
| 13 | * @phpstan-import-type OlzRoleData from RoleEndpointTrait |
| 14 | * |
| 15 | * @extends OlzCreateEntityTypedEndpoint<OlzRoleId, OlzRoleData> |
| 16 | */ |
| 17 | class CreateRoleEndpoint extends OlzCreateEntityTypedEndpoint { |
| 18 | use RoleEndpointTrait; |
| 19 | |
| 20 | protected function handle(mixed $input): mixed { |
| 21 | $parent_role = $input['data']['parentRole'] ?? null; |
| 22 | if (!$this->authUtils()->hasRoleEditPermission($parent_role)) { |
| 23 | throw new HttpError(403, "Kein Zugriff!"); |
| 24 | } |
| 25 | |
| 26 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 27 | $role_repo = $this->entityManager()->getRepository(Role::class); |
| 28 | |
| 29 | // Username validation |
| 30 | $new_username = $input['data']['username']; |
| 31 | if (!$this->authUtils()->isUsernameAllowed($new_username)) { |
| 32 | throw new ValidationError(['username' => ["Der Benutzername darf nur Buchstaben, Zahlen, und die Zeichen -_. enthalten."]]); |
| 33 | } |
| 34 | if (!$this->authUtils()->isUsernameUnique($new_username, null)) { |
| 35 | throw new ValidationError(['username' => ["Dieser Benutzername ist bereits vergeben."]]); |
| 36 | } |
| 37 | |
| 38 | $entity = new Role(); |
| 39 | $this->entityUtils()->createOlzEntity($entity, $input['meta']); |
| 40 | $entity->setOldUsername(null); |
| 41 | $entity->setPermissions(''); |
| 42 | $this->updateEntityWithData($entity, $input['data']); |
| 43 | |
| 44 | $this->entityManager()->persist($entity); |
| 45 | $this->entityManager()->flush(); |
| 46 | $this->persistUploads($entity, $input['data']); |
| 47 | |
| 48 | return [ |
| 49 | 'id' => $entity->getId() ?? 0, |
| 50 | ]; |
| 51 | } |
| 52 | } |