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