Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.98% covered (success)
92.98%
53 / 57
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
register_user_data
84.62% covered (success)
84.62%
22 / 26
0.00% covered (danger)
0.00%
0 / 1
7.18
1<?php
2
3/**
4 * @file
5 * Login action: register new user and send validation email.
6 * Logic inlined from legacy/login/register.php; no longer requires that file.
7 */
8
9declare(strict_types=1);
10
11require_once __DIR__.'/../legacy/login/validate.php';
12
13/**
14 * Registers a new user (activation pending). Returns ApplicationError code.
15 *
16 * Uses central DB (USER table); connection resolved inside.
17 *
18 * @param \ConxHelper $conx Connection helper.
19 * @param array<string, mixed> $userinfo Input: username, password. Output: objid, email, etc.
20 * @param \Psr\Log\LoggerInterface $logger Logger for debug output.
21 * @return int ApplicationError code.
22 */
23function register_user_data(\ConxHelper $conx, array &$userinfo, \Psr\Log\LoggerInterface $logger): int {
24    if (!check_free_email($conx, $userinfo['username'])) {
25        return ApplicationError::Exists;
26    }
27
28    $retval = ApplicationError::Success;
29    if ($conx->global === null) {
30        addlog(__FILE__, LogLevel::Error, "Connection to central database failed.");
31        return ApplicationError::Database;
32    }
33
34    $iteminfo = [
35        'email' => $userinfo['username'],
36        'password' => $userinfo['password'],
37        'license' => 1
38    ];
39
40    $userinfo['objid'] = '';
41    $ok = $conx->global->transactional(function (\transaction $transaction) use (&$userinfo, $iteminfo, $logger) {
42        $res = $transaction->insert('USER', $iteminfo, $userinfo['objid']);
43        $logger->debug('NEW USER:'.PHP_EOL.print_r($userinfo, true).PHP_EOL);
44        return (bool) $res;
45    });
46    
47    if (!$ok) {
48        $retval = ApplicationError::Database;
49    }
50
51    if (success($retval)) {
52        $results = [];
53        $res = $conx->global->query('USER', $iteminfo, $results);
54        if ($res && count($results) > 0) {
55            $userinfo = $results[0];
56        } else {
57            $retval = ApplicationError::Database;
58        }
59    }
60
61    return $retval;
62}
63
64/**
65 * Action callable: register new user (POST body username/password + GET lang).
66 *
67 * Registers user (activation pending) and sends validation mail.
68 * Does not grant a SESSION; the client must log in separately.
69 *
70 * @param string $body JSON body with username, password.
71 * @param array<string, mixed> $query Query params: lang (device optional, ignored).
72 * @param \ConxHelper $conx Connection helper (from LoginService; uses ->global for central DB).
73 * @param \Psr\Log\LoggerInterface $logger Logger (from LoggerFactory).
74 * @param \UppServices\SessionService $sessionService Session service (unused).
75 * @return array{output: string, contentType: string} JSON output and content type.
76 */
77return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
78    $retval = ApplicationError::Success;
79    $userinfo = [];
80
81    if ($body === '') {
82        $output = json_encode(['errorcode' => ApplicationError::Parameters]);
83        return ['output' => $output, 'contentType' => 'application/json; charset=utf-8'];
84    }
85
86    $data = json_decode($body, true);
87    if (json_last_error() !== \JSON_ERROR_NONE) {
88        $logger->error("Invalid JSON format in post request data");
89        $output = json_encode(['errorcode' => ApplicationError::Generic]);
90        return ['output' => $output, 'contentType' => 'application/json; charset=utf-8'];
91    }
92
93    $logger->debug("DUMP OF POST DATA:".PHP_EOL.print_r($data, true).PHP_EOL);
94
95    $username = $data['username'] ?? null;
96    if (success($retval) && !$username) {
97        $logger->error("Mandatory argument 'username' not provided in post data.");
98        $retval = ApplicationError::Parameters;
99    }
100
101    $password = $data['password'] ?? null;
102    if (success($retval) && !$password) {
103        $logger->error("Mandatory argument 'password' not provided in post data.");
104        $retval = ApplicationError::Parameters;
105    }
106
107    if (success($retval)) {
108        $userinfo = ['username' => $username, 'password' => $password];
109        $retval = register_user_data($conx, $userinfo, $logger);
110    }
111
112    if (success($retval)) {
113        $lang = $query['lang'] ?? 'EN';
114        send_validation_mail($conx, $lang, $userinfo['email']);
115    }
116
117    $result = ['errorcode' => $retval];
118    $output = json_encode($result);
119    return ['output' => $output, 'contentType' => 'application/json; charset=utf-8'];
120};