add roles
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
require_once('DataBase.php');
|
||||
|
||||
class Role
|
||||
{
|
||||
public int $id;
|
||||
public string $name;
|
||||
|
||||
function __construct(array $table)
|
||||
{
|
||||
$this->id = $table['id'];
|
||||
$this->name = $table['name'];
|
||||
}
|
||||
}
|
||||
|
||||
function AddRole(string $name): void
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = "INSERT INTO `roles` (`name`) VALUES (:name);";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':name', $name, PDO::PARAM_STR);
|
||||
$query->execute();
|
||||
|
||||
CloseDataBase();
|
||||
}
|
||||
|
||||
function GetRole(int $id): Role
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = "SELECT * FROM `roles` WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
$result = $query->fetch();
|
||||
|
||||
CloseDataBase();
|
||||
|
||||
if (empty($result))
|
||||
return null;
|
||||
|
||||
return new Role($result);
|
||||
}
|
||||
|
||||
function GetRoles(): array
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = 'SELECT * FROM `roles`';
|
||||
$query = $db->prepare($sql);
|
||||
$query->execute();
|
||||
$result = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
CloseDataBase();
|
||||
|
||||
$roles = array();
|
||||
|
||||
foreach ($result as $role) {
|
||||
array_push($roles, new User($role));
|
||||
}
|
||||
|
||||
return $roles;
|
||||
}
|
||||
|
||||
function UpdateRole(Role $role): void
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = "UPDATE `roles` SET `name`=:name WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':login', $role->name, PDO::PARAM_STR);
|
||||
$query->bindValue(':id', $role->id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
|
||||
CloseDataBase();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user