Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Profile | |
100.00% |
42 / 42 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| readResponse | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
4 | |||
| writeUpdatedEntity | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Zmsadmin |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsadmin; |
| 9 | |
| 10 | use BO\Zmsentities\Schema\Loader; |
| 11 | use BO\Zmsentities\Useraccount as Entity; |
| 12 | |
| 13 | class Profile extends BaseController |
| 14 | { |
| 15 | /** |
| 16 | * @SuppressWarnings(Param) |
| 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' => 2])->getEntity(); |
| 25 | $confirmSuccess = $request->getAttribute('validator')->getParameter('success')->isString()->getValue(); |
| 26 | $error = $request->getAttribute('validator')->getParameter('error')->isString()->getValue(); |
| 27 | $entity = new Entity($workstation->useraccount); |
| 28 | |
| 29 | if ($request->getMethod() === 'POST') { |
| 30 | $input = $request->getParsedBody(); |
| 31 | $result = $this->writeUpdatedEntity($input, $entity->getId()); |
| 32 | if ($result instanceof Entity) { |
| 33 | return \BO\Slim\Render::redirect('profile', [], [ |
| 34 | 'success' => 'useraccount_saved' |
| 35 | ]); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // TODO: there should be common functions to access configuration and user or account data |
| 40 | // Currently we depend on these magic string like "useraccount". |
| 41 | // A better approach would be a function called readUserAccountData($accountId) |
| 42 | $userAccount = \App::$http->readGetResult('/useraccount/' . $entity->getId() . '/')->getEntity(); |
| 43 | $config = \App::$http->readGetResult('/config/', [], \App::CONFIG_SECURE_TOKEN)->getEntity(); |
| 44 | $allowedProviderList = explode(',', $config->getPreference('oidc', 'provider') ?? ''); |
| 45 | |
| 46 | return \BO\Slim\Render::withHtml( |
| 47 | $response, |
| 48 | 'page/profile.twig', |
| 49 | array( |
| 50 | 'title' => 'Nutzerprofil', |
| 51 | 'menuActive' => 'profile', |
| 52 | 'workstation' => $workstation, |
| 53 | 'useraccount' => $entity->getArrayCopy(), |
| 54 | 'success' => $confirmSuccess, |
| 55 | 'error' => $error, |
| 56 | 'exception' => (isset($result)) ? $result : null, |
| 57 | 'metadata' => $this->getSchemaConstraintList(Loader::asArray(Entity::$schema)), |
| 58 | 'isFromOidc' => in_array($userAccount->getOidcProviderFromName(), $allowedProviderList) |
| 59 | ) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | protected function writeUpdatedEntity($input) |
| 64 | { |
| 65 | $entity = (new Entity($input))->withCleanedUpFormData(); |
| 66 | $entity->setPassword($input); |
| 67 | try { |
| 68 | $entity = \App::$http->readPostResult('/workstation/password/', $entity)->getEntity(); |
| 69 | } catch (\BO\Zmsclient\Exception $exception) { |
| 70 | $template = Helper\TwigExceptionHandler::getExceptionTemplate($exception); |
| 71 | if ( |
| 72 | '' != $exception->template |
| 73 | && \App::$slim->getContainer()->get('view')->getLoader()->exists($template) |
| 74 | ) { |
| 75 | return [ |
| 76 | 'template' => $template, |
| 77 | 'data' => $exception->data |
| 78 | ]; |
| 79 | } |
| 80 | throw $exception; |
| 81 | } |
| 82 | return $entity; |
| 83 | } |
| 84 | } |