2015-12-29 01:27:49 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Permission Handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Perms
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
class Perms
|
|
|
|
{
|
|
|
|
// Modes
|
2015-12-29 21:52:19 +00:00
|
|
|
const SITE = 'permissions\permissions_site';
|
|
|
|
const MANAGE = 'permissions\permissions_manage';
|
2016-01-03 21:19:37 +00:00
|
|
|
const FORUM = 'forum_permissions\forum_perms';
|
2015-12-29 01:27:49 +00:00
|
|
|
|
|
|
|
// Variables
|
|
|
|
protected $table = '';
|
|
|
|
protected $column = '';
|
2015-12-29 21:52:19 +00:00
|
|
|
|
2015-12-29 01:27:49 +00:00
|
|
|
// Constructor
|
2015-12-29 21:52:19 +00:00
|
|
|
public function __construct($mode)
|
|
|
|
{
|
|
|
|
$this->mode($mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the mode
|
2016-01-02 17:55:31 +00:00
|
|
|
public function mode($mode)
|
|
|
|
{
|
2015-12-29 01:27:49 +00:00
|
|
|
// Split the mode variable
|
|
|
|
$mode = explode('\\', $mode);
|
|
|
|
|
|
|
|
// Assign $table, $column and $selectors
|
|
|
|
$this->table = $mode[0];
|
|
|
|
$this->column = $mode[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checking permissions
|
2015-12-29 21:52:19 +00:00
|
|
|
public function check($flag, $perm)
|
|
|
|
{
|
|
|
|
return ($flag & $perm) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getting a rank's permissions
|
|
|
|
public function rank($rid, $conditions = [], $perm = 0)
|
|
|
|
{
|
|
|
|
// Merge rank id and additional conditions
|
|
|
|
$conditions = array_merge(['rank_id' => [$rid, '='], 'user_id' => [0, '=']], $conditions);
|
|
|
|
|
|
|
|
// Fetch from the db
|
|
|
|
$get = Database::fetch($this->table, false, $conditions);
|
|
|
|
|
|
|
|
// Check if anything was returned
|
|
|
|
if ($get && array_key_exists($this->column, $get) && $get['rank_id']) {
|
|
|
|
// Perform a bitwise OR
|
|
|
|
$perm = $perm | bindec((string) $get[$this->column]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the value
|
|
|
|
return $perm;
|
2015-12-29 01:27:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 21:52:19 +00:00
|
|
|
// Getting a user's permissions
|
|
|
|
public function user($uid, $conditions = [], $perm = 0)
|
|
|
|
{
|
|
|
|
// Create a user object
|
|
|
|
$user = User::construct($uid);
|
|
|
|
|
|
|
|
// Get data from ranks
|
|
|
|
foreach ($user->ranks() as $rank) {
|
|
|
|
$perm = $perm | $this->rank($rank, $conditions, $perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge user id and additional conditions
|
|
|
|
$conditions = array_merge(['user_id' => [$uid, '='], 'rank_id' => [0, '=']], $conditions);
|
|
|
|
|
|
|
|
// Fetch from the db
|
|
|
|
$get = Database::fetch($this->table, false, $conditions);
|
|
|
|
|
|
|
|
// Check if anything was returned
|
|
|
|
if ($get && array_key_exists($this->column, $get) && $get['user_id']) {
|
|
|
|
// Perform a bitwise OR
|
|
|
|
$perm = $perm | bindec((string) $get[$this->column]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the value
|
|
|
|
return $perm;
|
2015-12-29 01:27:49 +00:00
|
|
|
}
|
|
|
|
}
|