Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.86% |
29 / 35 |
|
0.00% |
0 / 1 |
CRAP | n/a |
0 / 0 |
|
| grant_posdevice | |
82.86% |
29 / 35 |
|
0.00% |
0 / 1 |
10.50 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Ensures an active POSDEVICE row exists for an operational device id. |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | /** |
| 11 | * Ensures an active POSDEVICE row exists for the given operational device id. |
| 12 | * |
| 13 | * Used by staff login and guest access before grantSession / grantAccess so |
| 14 | * SESSION can resolve the device. Failed credential checks must not call this |
| 15 | * (staff login only grants after credentials succeed). |
| 16 | * |
| 17 | * @param \ConxHelper $conx Connection helper (central DB via ->global). |
| 18 | * @param string $deviceid Operational device id (calc and uuid). |
| 19 | * @return int ApplicationError code. |
| 20 | */ |
| 21 | function grant_posdevice(\ConxHelper $conx, string $deviceid): int |
| 22 | { |
| 23 | $results = []; |
| 24 | |
| 25 | if ($conx->global === null) { |
| 26 | addlog(__FILE__, LogLevel::Error, 'Connection to central database failed.'); |
| 27 | return ApplicationError::Database; |
| 28 | } |
| 29 | |
| 30 | $conn = $conx->global->mysqli; |
| 31 | if (!$conn) { |
| 32 | return ApplicationError::Database; |
| 33 | } |
| 34 | |
| 35 | $escaped = mysqli_real_escape_string($conn, $deviceid); |
| 36 | $sql = "select objid, name from POSDEVICE where status='AC' and (uuid='".$escaped."' or calc='".$escaped."') order by created desc limit 1"; |
| 37 | |
| 38 | $res = $conx->global->runsql(['POSDEVICE'], $sql, $results); |
| 39 | if (!$res) { |
| 40 | return ApplicationError::Database; |
| 41 | } |
| 42 | |
| 43 | if (count($results) > 0) { |
| 44 | return ApplicationError::Success; |
| 45 | } |
| 46 | |
| 47 | $name = null; |
| 48 | if ($deviceid !== '' && str_ends_with($deviceid, ')')) { |
| 49 | $open = strrpos($deviceid, '('); |
| 50 | if ($open !== false) { |
| 51 | $parsed = substr($deviceid, $open + 1, -1); |
| 52 | $name = $parsed !== '' ? $parsed : null; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $iteminfo = [ |
| 57 | 'status' => 'AC', |
| 58 | 'calc' => $deviceid, |
| 59 | 'uuid' => $deviceid, |
| 60 | 'name' => $name, |
| 61 | ]; |
| 62 | |
| 63 | $inserted = ''; |
| 64 | $ok = $conx->global->transactional(function (\transaction $transaction) use ($iteminfo, &$inserted) { |
| 65 | return (bool) $transaction->insert('POSDEVICE', $iteminfo, $inserted); |
| 66 | }); |
| 67 | |
| 68 | if (!$ok) { |
| 69 | addlog(__FILE__, LogLevel::Error, 'Could not create POSDEVICE for deviceid ['.$deviceid.'].'); |
| 70 | return ApplicationError::Database; |
| 71 | } |
| 72 | |
| 73 | addlog(__FILE__, LogLevel::Information, 'Created POSDEVICE for deviceid ['.$deviceid.'].'); |
| 74 | return ApplicationError::Success; |
| 75 | } |