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