Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UseraccountUpdate
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 testEntity
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsapi;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12use BO\Zmsdb\Useraccount;
13
14/**
15 * @SuppressWarnings(Coupling)
16 * @return \Psr\Http\Message\ResponseInterface
17 */
18class UseraccountUpdate extends BaseController
19{
20    /**
21     * @SuppressWarnings(Param)
22     * @return \Psr\Http\Message\ResponseInterface
23     */
24    #[\Override]
25    public function readResponse(
26        \Psr\Http\Message\RequestInterface $request,
27        \Psr\Http\Message\ResponseInterface $response,
28        array $args
29    ) {
30        (new Helper\User($request, 2))->checkPermissions('useraccount');
31
32        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
33        $input = Validator::input()->isJson()->assertValid()->getValue();
34        $entity = new \BO\Zmsentities\Useraccount($input);
35        if (! (new Useraccount())->readIsUserExisting($args['loginname'])) {
36            throw new Exception\Useraccount\UseraccountNotFound();
37        }
38
39        $this->testEntity($entity, $input, $args);
40
41        if (isset($entity->changePassword)) {
42            $entity->password = $entity->getHash(reset($entity->changePassword));
43        }
44
45        $message = Response\Message::create($request);
46        $message->data = (new Useraccount())->writeUpdatedEntity($args['loginname'], $entity, $resolveReferences);
47
48        $response = Render::withLastModified($response, time(), '0');
49        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
50        return $response;
51    }
52
53    protected function testEntity($entity, $input, $args)
54    {
55        if (0 == count($input)) {
56            throw new Exception\Useraccount\UseraccountInvalidInput();
57        }
58        try {
59            $entity->testValid('de_DE', 1);
60        } catch (\Exception $exception) {
61            $exception->data['input'] = $input;
62            throw $exception;
63        }
64
65        if ($args['loginname'] != $entity->id && (new Useraccount())->readIsUserExisting($entity->id)) {
66            throw new Exception\Useraccount\UseraccountAlreadyExists();
67        }
68
69        Helper\UserAuth::testUseraccountExists($args['loginname']);
70        Helper\User::testWorkstationAccessRights($entity);
71        Helper\User::testWorkstationAssignedRights($entity);
72    }
73}