Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.12% |
39 / 41 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Login action: login with stored session (device + username + session). |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | /** |
| 11 | * Action callable: login with stored session (GET device, username, session). |
| 12 | * |
| 13 | * Validates session and device, loads user, grants session, returns JSON with errorcode and session. |
| 14 | * |
| 15 | * @param string $body Request body (unused). |
| 16 | * @param array<string, mixed> $query Query params: device, username, session. |
| 17 | * @param \ConxHelper $conx Connection helper (from LoginService; uses ->global for central DB). |
| 18 | * @param \Psr\Log\LoggerInterface $logger Logger (from LoggerFactory). |
| 19 | * @param \UppServices\SessionService $sessionService Session service for grant. |
| 20 | * @return array{output: string, contentType: string} JSON output and content type. |
| 21 | */ |
| 22 | return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array { |
| 23 | $retval = ApplicationError::Success; |
| 24 | $userinfo = null; |
| 25 | |
| 26 | $deviceid = $query['device'] ?? null; |
| 27 | if (!$deviceid) { |
| 28 | $logger->error("Missing 'device' parameter in url request"); |
| 29 | $retval = ApplicationError::Parameters; |
| 30 | } |
| 31 | |
| 32 | if (success($retval)) { |
| 33 | $username = $query['username'] ?? null; |
| 34 | if (!$username) { |
| 35 | $logger->error("Mandatory argument 'username' not provided in post data."); |
| 36 | $retval = ApplicationError::Parameters; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if (success($retval)) { |
| 41 | $session = $query['session'] ?? null; |
| 42 | if (!$session) { |
| 43 | $logger->error("Mandatory argument 'session' not provided in post data."); |
| 44 | $retval = ApplicationError::Parameters; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if (success($retval)) { |
| 49 | $results = []; |
| 50 | $res = $conx->global->query("SESSION", ["id" => $session, "deviceid" => $deviceid], $results); |
| 51 | if (!$res || count($results) === 0) { |
| 52 | $logger->info("Invalid session / deviceid for stored login (".$session."/".$deviceid.")"); |
| 53 | $retval = ApplicationError::Unauthorized; |
| 54 | } |
| 55 | else { |
| 56 | $userid = $results[0]['user']; |
| 57 | $results = []; |
| 58 | $res = $conx->global->query("USER", ["objid" => $userid, "email" => $username, "status" => ['PA', 'AC']], $results); |
| 59 | |
| 60 | if ($res && count($results) > 0) { |
| 61 | $userinfo = $results[0]; |
| 62 | $logger->info("Login succesfull for user '".$username."' -- objid [".$userinfo['objid']."]"); |
| 63 | } |
| 64 | else { |
| 65 | $logger->error("No valid users/waiters found for email '".$username."' with previous session '".$session."'."); |
| 66 | $retval = ApplicationError::Unauthorized; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (success($retval)) { |
| 72 | $userinfo['session'] = null; |
| 73 | $retval = $sessionService->grantSession($conx, $deviceid, $userinfo['objid'], $userinfo['session']); |
| 74 | } |
| 75 | |
| 76 | if (success($retval)) { |
| 77 | $userinfo['photo'] = toUrl($userinfo['photo']); |
| 78 | $result = ["errorcode" => $retval, "session" => $userinfo['session']]; |
| 79 | } |
| 80 | else { |
| 81 | $result = ["errorcode" => $retval]; |
| 82 | } |
| 83 | |
| 84 | $output = json_encode($result); |
| 85 | return ['output' => $output, 'contentType' => 'application/json; charset=utf-8']; |
| 86 | }; |