Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
35 / 40
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkstationLogin
87.50% covered (warning)
87.50%
35 / 40
66.67% covered (warning)
66.67%
2 / 3
5.05
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 getLoggedInWorkstation
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 testLoginHash
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
6.28
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsbackend\Workstation\Api;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12
13/**
14 * @SuppressWarnings(Coupling)
15 */
16class WorkstationLogin extends \BO\Zmsbackend\Api\BaseController
17{
18    /**
19     * @SuppressWarnings(Param)
20     * @return \Psr\Http\Message\ResponseInterface
21     */
22    #[\Override]
23    public function readResponse(
24        \Psr\Http\Message\RequestInterface $request,
25        \Psr\Http\Message\ResponseInterface $response,
26        array $args
27    ) {
28        $validator = $request->getAttribute('validator');
29        $resolveReferences = $validator->getParameter('resolveReferences')->isNumber()->setDefault(1)->getValue();
30        $input = Validator::input()->isJson()->assertValid()->getValue();
31        $entity = new \BO\Zmsentities\Useraccount($input);
32        $entity->testValid();
33
34        \BO\Zmsbackend\Connection\Select::getWriteConnection();
35        $workstation = self::getLoggedInWorkstation($request, $entity, $resolveReferences);
36        \BO\Zmsbackend\Connection\Select::writeCommit(); // @codeCoverageIgnore
37
38        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
39        $message->data = $workstation;
40
41        $response = Render::withLastModified($response, time(), '0');
42        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
43        return $response;
44    }
45
46    public static function getLoggedInWorkstation(\Psr\Http\Message\RequestInterface $request, \BO\Zmsentities\Useraccount $entity, int $resolveReferences)
47    {
48        \BO\Zmsbackend\Helper\UserAuth::testUseraccountExists($entity->getId());
49        $useraccount = \BO\Zmsbackend\Helper\UserAuth::getVerifiedUseraccount($entity);
50        \BO\Zmsbackend\Helper\UserAuth::testPasswordMatching($useraccount, $entity->password);
51
52        $workstation = (new \BO\Zmsbackend\Helper\User($request, $resolveReferences))->readWorkstation();
53        \BO\Zmsbackend\Helper\User::testWorkstationIsOveraged($workstation);
54
55        static::testLoginHash($workstation);
56        $workstation = (new \BO\Zmsbackend\Workstation\Service\Workstation())->writeEntityLoginByName(
57            $useraccount->id,
58            $useraccount->password,
59            \App::getNow(),
60            (new \DateTime())->setTimestamp(time() + \App::SESSION_DURATION),
61            $resolveReferences
62        );
63
64        \BO\Zmsbackend\Log\Service\Log::writeLogEntry(
65            "LOGIN (WorkstattionLogin::getLoggedInWorkstation) " . $useraccount->id,
66            0,
67            \BO\Zmsbackend\Log\Service\Log::PROCESS,
68            $workstation->getScope()->getId(),
69            $workstation->getUseraccount()->getId()
70        );
71
72        return $workstation;
73    }
74
75    /**
76     * @return void
77     */
78    public static function testLoginHash($workstation)
79    {
80        $useraccount = $workstation->getUseraccount();
81        if (isset($useraccount->id)) {
82            $logInHash = (new \BO\Zmsbackend\Workstation\Service\Workstation())->readLoggedInHashByName($useraccount->id);
83            if (null !== $logInHash) {
84                $exception = new \BO\Zmsbackend\Useraccount\Exception\UserAlreadyLoggedIn();
85                $exception->data = $workstation;
86                throw $exception;
87            }
88        }
89    }
90}