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