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\Zmsapi;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12use BO\Zmsdb\Log;
13use BO\Zmsdb\Workstation;
14use BO\Zmsdb\Useraccount;
15
16/**
17 * @SuppressWarnings(Coupling)
18 */
19class WorkstationLogin extends BaseController
20{
21    /**
22     * @SuppressWarnings(Param)
23     * @return \Psr\Http\Message\ResponseInterface
24     */
25    #[\Override]
26    public function readResponse(
27        \Psr\Http\Message\RequestInterface $request,
28        \Psr\Http\Message\ResponseInterface $response,
29        array $args
30    ) {
31        $validator = $request->getAttribute('validator');
32        $resolveReferences = $validator->getParameter('resolveReferences')->isNumber()->setDefault(1)->getValue();
33        $input = Validator::input()->isJson()->assertValid()->getValue();
34        $entity = new \BO\Zmsentities\Useraccount($input);
35        $entity->testValid();
36
37        \BO\Zmsdb\Connection\Select::getWriteConnection();
38        $workstation = self::getLoggedInWorkstation($request, $entity, $resolveReferences);
39        \BO\Zmsdb\Connection\Select::writeCommit(); // @codeCoverageIgnore
40
41        $message = Response\Message::create($request);
42        $message->data = $workstation;
43
44        $response = Render::withLastModified($response, time(), '0');
45        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
46        return $response;
47    }
48
49    public static function getLoggedInWorkstation($request, $entity, $resolveReferences)
50    {
51        Helper\UserAuth::testUseraccountExists($entity->getId());
52        $useraccount = Helper\UserAuth::getVerifiedUseraccount($entity);
53        Helper\UserAuth::testPasswordMatching($useraccount, $entity->password);
54
55        $workstation = (new Helper\User($request, $resolveReferences))->readWorkstation();
56        Helper\User::testWorkstationIsOveraged($workstation);
57
58        static::testLoginHash($workstation);
59        $workstation = (new Workstation())->writeEntityLoginByName(
60            $useraccount->id,
61            $useraccount->password,
62            \App::getNow(),
63            (new \DateTime())->setTimestamp(time() + \App::SESSION_DURATION),
64            $resolveReferences
65        );
66
67        \BO\Zmsdb\Log::writeLogEntry(
68            "LOGIN (WorkstattionLogin::getLoggedInWorkstation) " . $useraccount->id,
69            0,
70            Log::PROCESS,
71            $workstation->getScope()->getId(),
72            $workstation->getUseraccount()->getId()
73        );
74
75        return $workstation;
76    }
77
78    public static function testLoginHash($workstation)
79    {
80        $useraccount = $workstation->getUseraccount();
81        if (isset($useraccount->id)) {
82            $logInHash = (new Workstation())->readLoggedInHashByName($useraccount->id);
83            if (null !== $logInHash) {
84                $exception = new \BO\Zmsapi\Exception\Useraccount\UserAlreadyLoggedIn();
85                $exception->data = $workstation;
86                throw $exception;
87            }
88        }
89    }
90}