<commit_before><?php

header("Content-type:application/json");
require_once 'details/user_management.php';

if (!is_logged_in())
    exit('{"error":{"code":403,"reason":"Not authenticated"}}');

if (!is_editor())
    exit('{"error":{"code":403,"reason":"No permission"}}');

$error400 = '{"error":{"code":400,"reason":"Bad request"}}';

if (!isset($_POST['user']))
    exit($error400);
else
    $json = $_POST['user'];

$user = json_decode($json, true);

if (json_last_error() !== JSON_ERROR_NONE)
    exit($error400);

if (!isset($user['username']) || !isset($user['password']))
    exit($error400);

$db = new PDO('sqlite:../sql/OIS.db');

if (empty($user['id']))
{
    $invoiceStmt = "INSERT INTO user(username, password, role_id) VALUES (:username, :password, :role_id);";
    $hasId = false;
}
else
{
    $invoiceStmt = "UPDATE OR FAIL user SET username = :username, password = :password, role_id = :role_id WHERE user.id = :userId;";
    $hasId = true;
}
$stmt = $db->prepare($invoiceStmt);

if (!$stmt)
    exit('Sorry, I dont know how to write sql statements');

$stmt->bindParam(':username', $user['username'], PDO::PARAM_STR);
$hashedPassword = encrypt_credentials($user['username'], $user['password']);
$stmt->bindParam(':password', $hashedPassword, PDO::PARAM_STR);
$role_id = !isset($user['role_id']) ? 1 : $user['role_id'];
$stmt->bindParam(':role_id', $role_id, PDO::PARAM_INT);

if ($hasId)
    $stmt->bindParam(':userId', $user['id'], PDO::PARAM_INT);

$stmt->execute();

echo($user);<commit_msg>Add optional Role to updateUser.php.<commit_after><?php

header("Content-type:application/json");
require_once 'details/user_management.php';

if (!is_logged_in())
    exit('{"error":{"code":403,"reason":"Not authenticated"}}');

if (!is_editor())
    exit('{"error":{"code":550,"reason":"Permission denied"}}');

$error400 = '{"error":{"code":400,"reason":"Bad request"}}';

if (!isset($_POST['user']))
    exit($error400);
else
    $json = $_POST['user'];

$user = json_decode($json, true);

if (json_last_error() !== JSON_ERROR_NONE)
    exit($error400);

if (!isset($user['username']) || !isset($user['password']) || !isset($user['id']))
    exit($error400);

$db = new PDO('sqlite:../sql/OIS.db');

if (empty($user['id'])) {
    $invoiceStmt = "INSERT INTO user(username, password, role_id) VALUES (:username, :password, :role_id);";
    $hasId = false;
    $hasPassword = true;
    $hasRole = true;
} else {
    if (!empty($user['password'])) {
        if (!empty($user['role_id']))
            $invoiceStmt = "UPDATE OR FAIL user SET username = :username, password = :password, role_id = :role_id WHERE user.id = :userId;";
        else {
            $invoiceStmt = "UPDATE OR FAIL user SET username = :username, password = :password WHERE user.id = :userId;";
            $hasRole = false;
        }
        $hasPassword = true;
        $hasRole = true;
    } else {
        if (!empty($user['role_id']))
            $invoiceStmt = "UPDATE OR FAIL user SET username = :username, role_id = :role_id WHERE user.id = :userId;";
        else {
            $invoiceStmt = "UPDATE OR FAIL user SET username = :username WHERE user.id = :userId;";
            $hasRole = false;
        }

        $hasPassword = false;
        $hasRole = true;
    }
    $hasId = true;
}
$stmt = $db->prepare($invoiceStmt);

if (!$stmt)
    exit($error400);

$stmt->bindParam(':username', $user['username'], PDO::PARAM_STR);

if ($hasPassword) {
    $hashedPassword = encrypt_credentials($user['username'], $user['password']);
    $stmt->bindParam(':password', $hashedPassword, PDO::PARAM_STR);
}

if ($hasRole)
    $stmt->bindParam(':role_id', $user['role_id'], PDO::PARAM_INT);

if ($hasId)
    $stmt->bindParam(':userId', $user['id'], PDO::PARAM_INT);

$stmt->execute();

if ($hasId)
    echo $user['id'];
else
    echo $db->lastInsertId();
{'fraction_non_alphanumeric': 0.21307632195884346, 'fraction_numerical': 0.011982287053920291, 'mean_word_length': 4.501432664756447, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4580839', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1299, 'n_words': 270}