Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.21% |
50 / 58 |
|
50.00% |
1 / 2 |
CRAP | n/a |
0 / 0 |
|
| activate_user_impl | |
72.73% |
16 / 22 |
|
0.00% |
0 / 1 |
6.73 | |||
| build_activate_output_html | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Login action: activate user from email link (authorization + usermail). |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | require_once __DIR__.'/../../Legacy/common/authorize.php'; |
| 11 | require_once __DIR__.'/../../Legacy/common/awake.php'; |
| 12 | |
| 13 | /** |
| 14 | * Activate user in DB (status PA -> AC). |
| 15 | * |
| 16 | * Finds USER by email with status PA, updates status to AC in a transaction. |
| 17 | * Uses central DB (USER table). |
| 18 | * |
| 19 | * @param \ConxHelper $conx Connection helper. |
| 20 | * @param string $usermail User email to activate. |
| 21 | * @param array|null $user Output: filled with user row on success. |
| 22 | * @param \Psr\Log\LoggerInterface $logger Logger for errors. |
| 23 | * @return int ApplicationError::Success, ::NotFound, or ::Database. |
| 24 | */ |
| 25 | function activate_user_impl(\ConxHelper $conx, string $usermail, &$user, \Psr\Log\LoggerInterface $logger): int |
| 26 | { |
| 27 | if ($conx->global === null) { |
| 28 | return ApplicationError::Database; |
| 29 | } |
| 30 | |
| 31 | $retval = ApplicationError::Success; |
| 32 | $result = []; |
| 33 | $res = $conx->global->query('USER', ['email' => $usermail, 'status' => 'PA'], $result); |
| 34 | if ($res) { |
| 35 | if (count($result) === 0) { |
| 36 | $logger->error("No user entry found with email address '".$usermail."'"); |
| 37 | return ApplicationError::NotFound; |
| 38 | } |
| 39 | $user = $result[0]; |
| 40 | $transaction = $conx->global->begin(); |
| 41 | if (!$transaction) { |
| 42 | return ApplicationError::Database; |
| 43 | } |
| 44 | |
| 45 | $iteminfo = [ |
| 46 | 'objid' => $user['objid'], |
| 47 | 'status' => 'AC', |
| 48 | ]; |
| 49 | |
| 50 | if (!$transaction->update('USER', $iteminfo)) { |
| 51 | $retval = ApplicationError::Database; |
| 52 | } |
| 53 | |
| 54 | $transaction->flush(success($retval)); |
| 55 | } |
| 56 | else { |
| 57 | $retval = ApplicationError::Database; |
| 58 | } |
| 59 | return $retval; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Build redirect HTML for activate result (IonUrl on-activate). |
| 64 | * |
| 65 | * @param string $language Language code for the redirect URL. |
| 66 | * @param int $errorcode ApplicationError::Success or other (maps to SUCCESS/FAILURE). |
| 67 | * @return string HTML page with meta refresh to IonUrl on-activate. |
| 68 | */ |
| 69 | function build_activate_output_html(string $language, int $errorcode): string |
| 70 | { |
| 71 | $result = ($errorcode === ApplicationError::Success) ? 'SUCCESS' : 'FAILURE'; |
| 72 | $dsturl = AppConstants::IonUrl().'on-activate?lang='.$language.'&result='.$result; |
| 73 | $html = '<!DOCTYPE html>'; |
| 74 | $html .= '<html>'; |
| 75 | $html .= '<head><meta http-equiv=\'refresh\' content=\'0; URL='.$dsturl.'\'></head>'; |
| 76 | $html .= '<body><p>You are being redirected. If not, please follow <a href=\''.$dsturl.'\'>this link</a>.</p></body>'; |
| 77 | $html .= '</html>'; |
| 78 | return $html; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Action callable: activate user from email link (GET authorization, usermail, lang). |
| 83 | * |
| 84 | * Validates authorization, activates user (PA -> AC), notifies via awake_on_update, |
| 85 | * returns HTML redirect to IonUrl on-activate. |
| 86 | * |
| 87 | * @param string $body Request body (unused). |
| 88 | * @param array<string, mixed> $query Query params: authorization, usermail, lang. |
| 89 | * @param \ConxHelper $conx Connection helper (from LoginService; uses ->global for central DB). |
| 90 | * @param \Psr\Log\LoggerInterface $logger Logger (from LoggerFactory). |
| 91 | * @param \UppServices\SessionService $sessionService Session service (unused). |
| 92 | * @return array{output: string, contentType: string} HTML output and content type. |
| 93 | */ |
| 94 | return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array { |
| 95 | $retval = ApplicationError::Success; |
| 96 | $userinfo = null; |
| 97 | |
| 98 | $logger->info("Checking authorization key for user activation.."); |
| 99 | $auth = $query['authorization'] ?? null; |
| 100 | if (!$auth) { |
| 101 | $logger->error("No authorization key provided on URL. Operation cancelled."); |
| 102 | $retval = ApplicationError::Parameters; |
| 103 | } |
| 104 | |
| 105 | if (success($retval)) { |
| 106 | $item = use_authorization($conx, $auth); |
| 107 | if (!$item) { |
| 108 | $logger->error("Authorization key '".$auth."' not found or invalid."); |
| 109 | $retval = ApplicationError::Unauthorized; |
| 110 | } |
| 111 | else { |
| 112 | $logger->info("Authorization check test sucessfull."); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (success($retval)) { |
| 117 | $usermail = $query['usermail'] ?? null; |
| 118 | if (!$usermail) { |
| 119 | $logger->error("No usermail provided on URL. Operation cancelled."); |
| 120 | $retval = ApplicationError::Parameters; |
| 121 | } |
| 122 | else { |
| 123 | $retval = activate_user_impl($conx, $usermail, $userinfo, $logger); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (success($retval)) { |
| 128 | $retval = awake_on_update($conx, 'USER', [$userinfo], true); |
| 129 | } |
| 130 | |
| 131 | $lang = $query['lang'] ?? 'EN'; |
| 132 | $output = build_activate_output_html($lang, $retval); |
| 133 | return ['output' => $output, 'contentType' => 'text/html; charset=utf-8']; |
| 134 | }; |