Lines
98.14%
53 / 54
Functions and Methods
50.00%
1 / 2
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| RoleAdd | 98.14% | 53 / 54 | 50.00% | 1 / 2 | 9 | 0.00% | 0 / 1 | |||
| readResponse | 100.00% | 32 / 32 | 100.00% | 1 / 1 | 5 | |||||
| writeNewRole | 95.45% | 21 / 22 | 0.00% | 0 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsadmin; | |
| 4 | ||
| 5 | use BO\Slim\Render; | |
| 6 | use BO\Zmsentities\Exception\UserAccountMissingRights; | |
| 7 | use BO\Zmsentities\Role; | |
| 8 | ||
| 9 | class RoleAdd extends BaseController | |
| 10 | { | |
| 11 | /** | |
| 12 | * @return \Psr\Http\Message\ResponseInterface | |
| 13 | */ | |
| 14 | #[\Override] | |
| 15 | public function readResponse( | |
| 16 | \Psr\Http\Message\RequestInterface $request, | |
| 17 | \Psr\Http\Message\ResponseInterface $response, | |
| 18 | array $args | |
| 19 | ) { | |
| 20 | $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 1])->getEntity(); | |
| 21 | if (!$workstation->getUseraccount()->hasPermissions(['superuser'])) { | |
| 22 | throw new UserAccountMissingRights(); | |
| 23 | } | |
| 24 | ||
| 25 | $validator = $request->getAttribute('validator'); | |
| 26 | $confirmSuccess = $validator->getParameter('success')->isString()->getValue(); | |
| 27 | $permissionList = \App::$http->readGetResult('/permissions/', [])->getCollection(); | |
| 28 | ||
| 29 | $submitted = null; | |
| 30 | $result = null; | |
| 31 | if ($request->getMethod() === 'POST') { | |
| 32 | $input = $request->getParsedBody(); | |
| 33 | $submitted = is_array($input) ? $input : []; | |
| 34 | $result = $this->writeNewRole($submitted); | |
| 35 | if ($result instanceof Role) { | |
| 36 | return Render::redirect( | |
| 37 | 'roleEdit', | |
| 38 | ['id' => $result->id], | |
| 39 | ['success' => 'role_added'] | |
| 40 | ); | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | return Render::withHtml( | |
| 45 | $response, | |
| 46 | 'page/roleForm.twig', | |
| 47 | [ | |
| 48 | 'title' => 'Rolle hinzufügen', | |
| 49 | 'menuActive' => 'roles', | |
| 50 | 'workstation' => $workstation, | |
| 51 | 'permissionList' => $permissionList, | |
| 52 | 'role' => $submitted, | |
| 53 | 'formAction' => 'add', | |
| 54 | 'success' => $confirmSuccess, | |
| 55 | 'exception' => $result, | |
| 56 | ] | |
| 57 | ); | |
| 58 | } | |
| 59 | ||
| 60 | protected function writeNewRole(array $input): Role|array|null | |
| 61 | { | |
| 62 | $data = $input; | |
| 63 | unset($data['id'], $data['assignedUserCount']); | |
| 64 | $permissions = $data['permissions'] ?? []; | |
| 65 | $data['permissions'] = is_array($permissions) | |
| 66 | ? array_values(array_unique($permissions)) | |
| 67 | : []; | |
| 68 | $entity = (new Role($data))->withCleanedUpFormData(); | |
| 69 | ||
| 70 | $roles = \App::$http->readGetResult('/roles/', [])->getCollection(); | |
| 71 | foreach ($roles as $existing) { | |
| 72 | if ((string) $existing->name === $entity->name) { | |
| 73 | return [ | |
| 74 | 'template' => 'exception/bo/zmsentities/exception/schemavalidation.twig', | |
| 75 | 'include' => true, | |
| 76 | 'data' => [ | |
| 77 | 'name' => [ | |
| 78 | 'messages' => ['Eine Rolle mit diesem Namen existiert bereits.'], | |
| 79 | ], | |
| 80 | ], | |
| 81 | ]; | |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 85 | return $this->handleEntityWrite(function () use ($entity) { | |
| 86 | return \App::$http->readPostResult('/roles/', $entity)->getEntity(); | |
| 87 | }); | |
| 88 | } | |
| 89 | } |