DIDS-Coursework/DIS-COMP4039-CW2-psxrp11-20450011/psxrp11-20450011_InstallationFiles/newUser.php

36 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
session_start();
require_once 'config.php';
header('Content-Type: application/json');
if (isset($_SESSION["username"]) && isset($_SESSION["admin"]))
{
$username = $_POST["username"];
$password = $_POST["password"];
$admin = $_POST["admin"];
$conn = dbConn();
$stmt = $conn->prepare("INSERT INTO Users (Users_username, Users_password, Users_admin) VALUES (:username, :password, :admin)");
$stmt->bindParam(":username", $username);
$stmt->bindParam(":password", $password);
$isAdmin = $admin ? 1 : 0;
$stmt->bindParam(":admin", $isAdmin);
$stmt->execute();
$logSQL = "INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES (:type, :username, NOW())";
$logStmt = $conn->prepare($logSQL);
$type = !$admin ? "Add user" : "Add admin";
$logStmt->bindParam(":type", $type);
$logStmt->bindParam(":username", $_SESSION["username"]);
$logStmt->execute();
echo json_encode(array("message" => "ok", "admin" => $admin));
}
else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"]))
{
echo json_encode(array("message" => "not logged in as admin"));
}
else
{
echo json_encode(array("message" => "Not logged in "));
}