Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Profile
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
4
 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 as Entity;
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 Entity($workstation->useraccount);
29
30        if ($request->getMethod() === 'POST') {
31            $input = $request->getParsedBody();
32            $result = $this->writeUpdatedEntity($input, $entity->getId());
33            if ($result instanceof Entity) {
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        $config = \App::$http->readGetResult('/config/', [], \App::CONFIG_SECURE_TOKEN)->getEntity();
45        $allowedProviderList = explode(',', $config->getPreference('oidc', 'provider') ?? '');
46
47        return \BO\Slim\Render::withHtml(
48            $response,
49            'page/profile.twig',
50            array(
51                'title' => 'Nutzerprofil',
52                'menuActive' => 'profile',
53                'workstation' => $workstation,
54                'useraccount' => $entity->getArrayCopy(),
55                'success' => $confirmSuccess,
56                'error' => $error,
57                'exception' => (isset($result)) ? $result : null,
58                'metadata' => $this->getSchemaConstraintList(Loader::asArray(Entity::$schema)),
59                'isFromOidc' => in_array($userAccount->getOidcProviderFromName(), $allowedProviderList)
60            )
61        );
62    }
63
64    protected function writeUpdatedEntity($input)
65    {
66        $entity = (new Entity($input))->withCleanedUpFormData();
67        // TODO: Remove the password fields when password authentication is removed in the future
68        $entity->setPassword($input);
69        return $this->handleEntityWrite(function () use ($entity) {
70            return \App::$http->readPostResult('/workstation/password/', $entity)->getEntity();
71        });
72    }
73}