Browse Source

Added Roles and Permissions to user class

tags/v1.3^2
Teknikode 6 years ago
parent
commit
77d787188c

+ 32
- 0
includes/classes/Role.class.php View File

@@ -0,0 +1,32 @@
<?php
class Role
{
protected $permissions;
protected function __construct() {
$this->permissions = array();
}
// return a role object with associated permissions
public static function getRolePerms($role_id, $db) {
$role = new Role();
$sql = "SELECT t2.perm_desc FROM role_perm as t1
JOIN permissions as t2 ON t1.perm_id = t2.perm_id
WHERE t1.role_id = :role_id";
$sth = $GLOBALS["DB"]->prepare($sql);
$sth->execute(array(":role_id" => $role_id));
$db->select_raw("role_perm as rp JOIN permissions as p ON rp.perm_id = p.perm_id", "WHERE rp.role_id=?", array($role_id), "p.perm_desc");
foreach ($results as $result)
{
$role->permissions[$result["perm_desc"]] = true;
}
return $role;
}
// check if a permission is set
public function hasPerm($permission) {
return isset($this->permissions[$permission]);
}
}
?>

+ 9
- 2
includes/classes/User.class.php View File

@@ -1,6 +1,5 @@
<?php
require_once('DB.class.php');
require_once('Role.class.php');
class User {
public $id;
@@ -17,6 +16,7 @@ class User {
public $about;
public $blog_title;
public $blog_desc;
public $roles;
//Constructor is called whenever a new object is created.
//Takes an associative array with the DB row as an argument.
@@ -36,6 +36,13 @@ class User {
$this->quote = (isset($data['quote'])) ? $data['quote'] : "";
$this->blog_title = (isset($data['blog_title'])) ? $data['blog_title'] : "";
$this->blog_desc = (isset($data['blog_desc'])) ? $data['blog_desc'] : "";
$this->roles = array();
$results = $this->db->select("user_role as ur JOIN roles as r ON ur.role_id = r.role_id", "WHERE ur.user_id=?", array($this->id), "ur.role_id, r.role_name");
$users = array();
foreach ($results as $result)
{
$this->roles[$result["role_name"]] = Role::getRolePerms($result["role_id"], $this->db);
}
}
public function save($db, $isNewUser = false) {

+ 79
- 3
includes/classes/UserTools.class.php View File

@@ -136,10 +136,86 @@ class UserTools {
//returns a User object. Takes the users id as an input
public function get($id)
{
$result = $this->db->select('users', "id=?", array($id));
return new User($result);
$result = $this->db->select('users', "id=?", array($id));
return new User($result);
}
// check if user has a specific privilege
public function hasPrivilege($perm)
{
foreach ($this->roles as $role)
{
if ($role->hasPerm($perm))
{
return true;
}
}
return false;
}
// check if a user has a specific role
public function hasRole($role_name)
{
return isset($this->roles[$role_name]);
}
// insert a new role permission association
public function insertPerm($role_id, $perm_id)
{
$data = array(
"role_id" => $role_id,
"perm_id" => $perm_id
);
$this->db->insert($data, "role_perm");
return true;
}
// delete ALL role permissions
public function deletePerms()
{
$db->delete('role_perm', '1=?', array(1));
return true;
}
// insert a new role
public function insertRole($role_name)
{
$data = array(
"role_name" => $role_name
);
$this->db->insert($data, "roles");
return true;
}
// insert array of roles for specified user id
public function insertUserRoles($user_id, $roles)
{
foreach ($roles as $role_id)
{
$data = array(
"user_id" => $user_id,
"role_id" => $role_id
);
$this->db->insert($data, "user_role");
}
return true;
}
// delete array of roles, and all associations
public static function deleteRoles($roles)
{
foreach ($roles as $role_id)
{
$db->delete('roles as t1 JOIN user_role as t2 on t1.role_id = t2.role_id JOIN role_perm as t3 on t1.role_id = t3.role_id', 't1.role_id=?', array($role_id), "t1, t2, t3");
}
return true;
}
// delete ALL roles for specified user id
public static function deleteUserRoles($user_id)
{
$db->delete('user_role', 'user_id=?', array($user_id));
return true;
}
}
?>

Loading…
Cancel
Save