Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
48 / 64
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3/**
4 * @file
5 * Guest access action: validate QRSCAN and grant guest session.
6 */
7
8    declare(strict_types=1);
9
10    require_once __DIR__.'/../login/PosDevice.php';
11
12    /**
13     * Action callable: grant guest session from table QR (POST body + GET device).
14     *
15     * Validates an active QRSCAN row (objid = table, authorization = auth), ensures
16     * POSDEVICE, then grants a guest session via SessionService::grantAccess.
17     *
18     * @param string $body JSON body with table, auth.
19     * @param array<string, mixed> $query Query params: device.
20     * @param \ConxHelper $conx Connection helper (central DB via ->global).
21     * @param \Psr\Log\LoggerInterface $logger Logger (from LoggerFactory).
22     * @param \UppServices\SessionService $sessionService Session service for grant.
23     * @return array{output: string, contentType: string} JSON output and content type.
24     */
25    return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
26        $retval = ApplicationError::Success;
27        $qrscan = null;
28        $session = null;
29
30        if ($body === '') {
31            $output = json_encode(['errorcode' => ApplicationError::Parameters]);
32            return ['output' => $output, 'contentType' => 'application/json; charset=utf-8'];
33        }
34
35        $logger->info('Guest access from remote IP: '.getRemoteIp());
36        $data = json_decode($body, true);
37        if (!is_array($data)) {
38            parse_str($body, $parsed);
39            $data = is_array($parsed) ? $parsed : null;
40        }
41        $logger->debug('DUMP OF POST DATA:'.PHP_EOL.print_r($data, true).PHP_EOL);
42
43        if ($data === null) {
44            $logger->error('Invalid JSON format in post request data');
45            $output = json_encode(['errorcode' => ApplicationError::Generic]);
46            return ['output' => $output, 'contentType' => 'application/json; charset=utf-8'];
47        }
48
49        $deviceid = $query['device'] ?? null;
50        if (!$deviceid) {
51            $logger->error("Missing 'device' parameter in url request");
52            $retval = ApplicationError::Parameters;
53        }
54
55        if (success($retval)) {
56            $authorization = $data['auth'] ?? null;
57            if (!$authorization) {
58                $logger->error("Missing 'auth' parameter in post parameters");
59                $retval = ApplicationError::Parameters;
60            }
61        }
62
63        if (success($retval)) {
64            $tableid = $data['table'] ?? null;
65            if (!$tableid) {
66                $logger->error("Missing 'table' parameter in post parameters");
67                $retval = ApplicationError::Parameters;
68            }
69        }
70
71        // Validate table + authorization against active QRSCAN (central)
72        if (success($retval)) {
73            if ($conx->global === null) {
74                $logger->error('Connection to central database failed.');
75                $retval = ApplicationError::Database;
76            }
77            else {
78                $results = [];
79                $res = $conx->global->query('QRSCAN', [
80                    'objid' => $tableid,
81                    'authorization' => $authorization,
82                    'status' => 'AC',
83                ], $results);
84                if ($res) {
85                    if (count($results) > 0) {
86                        $qrscan = $results[0];
87                    }
88                    else {
89                        $logger->error("No active QRSCAN for id '".$tableid."' and authorization [".$authorization.']');
90                        $retval = ApplicationError::NotFound;
91                    }
92                }
93                else {
94                    $retval = ApplicationError::Database;
95                }
96            }
97        }
98
99        if (success($retval)) {
100            $retval = grant_posdevice($conx, (string) $deviceid);
101        }
102
103        if (success($retval)) {
104            $session = null;
105            $retval = $sessionService->grantAccess($conx, (string) $deviceid, (string) $qrscan['objid'], $session);
106        }
107
108        if (success($retval)) {
109            $result = [
110                'errorcode' => $retval,
111                'session' => $session,
112            ];
113        }
114        else {
115            $result = [
116                'errorcode' => $retval,
117            ];
118        }
119
120        $output = json_encode($result);
121        return ['output' => $output, 'contentType' => 'application/json; charset=utf-8'];
122    };