Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.37% covered (success)
97.37%
37 / 38
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Profile
97.37% covered (success)
97.37%
37 / 38
50.00% covered (danger)
50.00%
1 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 readResponse
96.97% covered (success)
96.97%
32 / 33
0.00% covered (danger)
0.00%
0 / 1
5
 writeUpdatedEntity
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
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;
12
13class Profile extends BaseController
14{
15    /**
16     * @SuppressWarnings(Param)
17     * @return \Psr\Http\Message\ResponseInterface
18     */
19    #[\Override]
20    public function readResponse(
21        \Psr\Http\Message\RequestInterface $request,
22        \Psr\Http\Message\ResponseInterface $response,
23        array $args
24    ): \Psr\Http\Message\ResponseInterface {
25        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
26        $confirmSuccess = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
27        $error = $request->getAttribute('validator')->getParameter('error')->isString()->getValue();
28        $entity = new Useraccount($workstation->useraccount);
29
30        if ($request->getMethod() === 'POST') {
31            $input = $request->getParsedBody();
32            $result = $this->writeUpdatedEntity($input, $entity->getId());
33            if ($result instanceof Useraccount) {
34                return \BO\Slim\Render::redirect('profile', [], [
35                    'success' => 'useraccount_saved'
36                ]);
37            }
38        }
39
40        // TODO: there should be common functions to access configuration and user or account data
41        // Currently we depend on these magic string like "useraccount".
42        // A better approach would be a function called readUserAccountData($accountId)
43        $userAccount = \App::$http->readGetResult('/useraccount/' . $entity->getId() . '/')->getEntity();
44        $userAccountData = $userAccount->getArrayCopy();
45        $workstationUserAccount = $entity->getArrayCopy();
46        if (empty($userAccountData['departments'] ?? [])) {
47            $userAccountData['departments'] = $workstationUserAccount['departments'] ?? [];
48        }
49        $config = \App::$http->readGetResult('/config/', [], \App::CONFIG_SECURE_TOKEN)->getEntity();
50        $allowedProviderList = explode(',', $config->getPreference('oidc', 'provider') ?? '');
51
52        return \BO\Slim\Render::withHtml(
53            $response,
54            'page/profile.twig',
55            array(
56                'title' => 'Nutzerprofil',
57                'menuActive' => 'profile',
58                'workstation' => $workstation,
59                'useraccount' => $userAccountData,
60                'success' => $confirmSuccess,
61                'error' => $error,
62                'exception' => (isset($result)) ? $result : null,
63                'metadata' => $this->getSchemaConstraintList(Loader::asArray(Useraccount::$schema)),
64                'isFromOidc' => in_array($userAccount->getOidcProviderFromName(), $allowedProviderList)
65            )
66        );
67    }
68
69    protected function writeUpdatedEntity($input)
70    {
71        $entity = (new Useraccount($input))->withCleanedUpFormData();
72        // TODO: Remove the password fields when password authentication is removed in the future
73        $entity->setPassword($input);
74        return $this->handleEntityWrite(function () use ($entity) {
75            return \App::$http->readPostResult('/workstation/password/', $entity)->getEntity();
76        });
77    }
78}