Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.25% covered (success)
98.25%
56 / 57
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UseraccountAdd
98.25% covered (success)
98.25%
56 / 57
50.00% covered (danger)
50.00%
1 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
1 / 1
9
 writeNewEntity
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2
3/**
4 * @package Zmsadmin
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsadmin;
9
10use BO\Slim\Render;
11use BO\Zmsentities\Collection\RoleList;
12use BO\Zmsentities\Exception\UserAccountMissingRights;
13use BO\Zmsentities\Schema\Loader;
14use BO\Zmsentities\Useraccount as Entity;
15
16class UseraccountAdd extends BaseController
17{
18    /**
19     * @SuppressWarnings(unused)
20     * @return \Psr\Http\Message\ResponseInterface
21     */
22    #[\Override]
23    public function readResponse(
24        \Psr\Http\Message\RequestInterface $request,
25        \Psr\Http\Message\ResponseInterface $response,
26        array $args
27    ): \Psr\Http\Message\ResponseInterface {
28        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 1])->getEntity();
29        if (! $workstation->getUseraccount()->hasPermissions(['useraccount'])) {
30            throw new UserAccountMissingRights();
31        }
32
33        $confirmSuccess = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
34        $selectedDepartment = $request->getAttribute('validator')->getParameter('department')->isNumber()->getValue();
35        $ownerList = \App::$http->readGetResult('/owner/', ['resolveReferences' => 2])->getCollection();
36
37        $input = $request->getParsedBody();
38        $submittedUserAccount = null;
39        if ($request->getMethod() === 'POST') {
40            $input['password'] = (
41                isset($input['changePassword']) && is_array($input['changePassword'])
42            ) ? ($input['changePassword'][0] ?? null) : null;
43            $submittedUserAccount = $input; // Preserve submitted data for form re-population
44            $result = $this->writeNewEntity($input);
45            if ($result instanceof Entity) {
46                return Render::redirect(
47                    'useraccountEdit',
48                    array(
49                        'loginname' => $result->id
50                    ),
51                    array(
52                        'success' => 'useraccount_added'
53                    )
54                );
55            }
56        }
57
58        $config = \App::$http->readGetResult('/config/', [], \App::CONFIG_SECURE_TOKEN)->getEntity();
59        $allowedProviderList = explode(',', $config->getPreference('oidc', 'provider') ?? '');
60
61        $roleList = new RoleList();
62        $roleResult = \App::$http->readGetResult('/roles/', []);
63        if ($roleResult) {
64            $loaded = $roleResult->getCollection();
65            if ($loaded !== null) {
66                $roleList = $loaded;
67            }
68        }
69
70        return Render::withHtml(
71            $response,
72            'page/useraccountEdit.twig',
73            [
74                'ownerList' => $ownerList->toDepartmentListByOrganisationName(),
75                'workstation' => $workstation,
76                'success' => $confirmSuccess,
77                'action' => 'add',
78                'title' => 'Nutzer: Einrichtung und Administration',
79                'menuActive' => 'useraccount',
80                'exception' => (isset($result)) ? $result : null,
81                'userAccount' => $submittedUserAccount, // Use submitted data to preserve form values on error
82                'selectedDepartment' => $selectedDepartment,
83                'oidcProviderList' => array_filter($allowedProviderList),
84                'metadata' => $this->getSchemaConstraintList(Loader::asArray(Entity::$schema)),
85                'roleList' => $roleList,
86            ]
87        );
88    }
89
90    protected function writeNewEntity($input)
91    {
92        $entity = new Entity($input);
93        if (isset($input['oidcProvider']) && '' != $input['oidcProvider']) {
94            $entity->id = $entity->id . '@' . $input['oidcProvider'];
95        }
96        $entity = $entity->withCleanedUpFormData(true);
97        return $this->handleEntityWrite(function () use ($entity) {
98            return \App::$http->readPostResult('/useraccount/', $entity)->getEntity();
99        });
100    }
101}