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