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