BootstrapSQLiteAuth/init.php

100 lines
3.0 KiB
PHP

<?php
global $c;
$c = new DatabaseConnection(
"sqlite",
"sqlite.db");
class UserAuth
{
public static function getUser()
{
global $c;
try
{
$strToken = Cookie::get("token");
if ($strToken !== null)
if (strlen($strToken) > 0)
{
$varTokenUsers = $c->query(
"SELECT *
from tokens as t
join user as u on u.email = t.email
where
t.token = ?
and (
t.expires is null
or t.expires > current_timestamp
)",
$strToken);
$varUser = null;
if (count($varTokenUsers) == 1)
$varUser = $varTokenUsers[0];
else return null;
try
{
$varUserDetails = $c->query(
"SELECT *
from user_info as ui
where
ui.email = ?",
$varUser["email"]);
if (count($varUserDetails) == 1)
$varUser = array_merge($varUser, $varUserDetails[0]);
}
catch (Exception $x) {}
return $varUser;
}
}
catch (Exception $x) {}
return null;
}
public static function hasPermission($strPermission)
{
global $c;
$varUser = UserAuth::getUser();
if ($varUser == null)
return false;
$c->query(
"CREATE table if not exists permission (
id integer primary key autoincrement,
email text not null,
name text not null)");
$varPermissions = $c->query(
"SELECT *
from permission
where
email like ?
and (
name like ?
or name like '*'
)",
$varUser["email"],
$strPermission);
if (count($varPermissions) > 0)
return true;
return false;
}
public static function requirePermission($strPermission)
{
if (!UserAuth::hasPermission($strPermission))
{
BootstrapRender::message("You do not have permission to do that, please sign into an account that does.", "warning");
Respond::redirect("/user/signin");
}
}
}
?>