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