<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-01-21
 * Time: 17:39
 */

require "initialize.php";

$qry = new db();

if (isset($_POST['userUsername'])) {
    $username = safe_input($_POST["userUsername"]);
    $password = safe_input($_POST["userPassword"]);
}

$string = "
  SELECT * 
  FROM `udut_users` 
  WHERE `userUsername` = '" . $username . "' 
  AND `userPassword` = '" . $password . "' 
";

$auth = $qry->getOne($string);

if ($auth["userUsername"] != $username || $auth['userPassword'] != $password) {
    header("location: login");
} else {
    //checking roles and privileges
    $role = $qry->getOne("
      SELECT udut_users_roles.*, udut_users_privileges.* 
      FROM `udut_users_roles` 
      JOIN `udut_users_privileges`
      ON udut_users_privileges.`roleId` = udut_users_roles.`roleId`
      WHERE udut_users_roles.`userId` = '" . $auth['userId'] . "' 
    ");
    //declaring session
    $_SESSION['YUZA_ID'] = $auth['userId'];
    $_SESSION['YUZA_ROLE_LOCATION'] = $role["roleLocation"];

    header("location:home");
}