Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UseraccountUpdate
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
15 / 15
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        Helper\User::testWorkstationAssignedRoles($entity);
41
42        if (isset($entity->changePassword)) {
43            $entity->password = $entity->getHash(reset($entity->changePassword));
44        }
45
46        $message = Response\Message::create($request);
47        $message->data = (new Useraccount())->writeUpdatedEntity($args['loginname'], $entity, $resolveReferences);
48
49        $response = Render::withLastModified($response, time(), '0');
50        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
51        return $response;
52    }
53
54    protected function testEntity($entity, $input, $args)
55    {
56        if (0 == count($input)) {
57            throw new Exception\Useraccount\UseraccountInvalidInput();
58        }
59        try {
60            $entity->testValid('de_DE', 1);
61        } catch (\Exception $exception) {
62            $exception->data['input'] = $input;
63            throw $exception;
64        }
65
66        if ($args['loginname'] != $entity->id && (new Useraccount())->readIsUserExisting($entity->id)) {
67            throw new Exception\Useraccount\UseraccountAlreadyExists();
68        }
69
70        Helper\UserAuth::testUseraccountExists($args['loginname']);
71        Helper\User::testWorkstationAccessRights($entity);
72        Helper\User::testWorkstationAssignedRights($entity);
73    }
74}