share the permission checking code

This commit is contained in:
flash 2016-12-08 22:13:55 +01:00
parent 6a773ee382
commit 5e8c640cc9
3 changed files with 93 additions and 106 deletions

View file

@ -6,9 +6,9 @@
namespace Sakura\Forum;
use Illuminate\Database\Query\Builder;
use Sakura\DB;
use Sakura\User;
use Sakura\PermissionHandler;
use Traversable;
/**
@ -16,20 +16,14 @@ use Traversable;
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class ForumPerms
class ForumPerms extends PermissionHandler
{
private static $table = 'forum_perms';
private $forums = [];
private $user = 0;
private $ranks = [];
private $permCache = [];
private $validCache = [];
public const TABLE = 'forum_perms';
public function __construct(Forum $forum, User $user)
{
$this->forums = iterator_to_array($this->getForumIds($forum->id));
$this->user = $user->id;
$this->ranks = array_keys($user->ranks);
$this->additionalRequirements['where_in']['forum_id'] = iterator_to_array($this->getForumIds($forum->id));
parent::__construct($user);
}
public function getForumIds(int $id): Traversable
@ -45,45 +39,4 @@ class ForumPerms
yield $id;
}
}
public function __get(string $name): bool
{
return $this->check($name);
}
public function __isset(string $name): bool
{
return $this->valid($name);
}
public function valid(string $name): bool
{
if (!array_key_exists($name, $this->validCache)) {
$column = 'perm_' . camel_to_snake($name);
$this->validCache[$name] = DB::getSchemaBuilder()->hasColumn(static::$table, $column);
}
return $this->validCache[$name];
}
public function check(string $name): bool
{
if (!array_key_exists($name, $this->permCache)) {
$column = 'perm_' . camel_to_snake($name);
$result = DB::table(static::$table)
->whereIn('forum_id', $this->forums)
->where(function (Builder $query) {
$query->whereIn('rank_id', $this->ranks)
->orWhere('user_id', $this->user);
})
->whereNotNull($column)
->groupBy($column)
->min($column);
$this->permCache[$name] = intval($result) === 1;
}
return $this->permCache[$name];
}
}

86
app/PermissionHandler.php Normal file
View file

@ -0,0 +1,86 @@
<?php
/**
* Holds the permission handler.
* @package Sakura
*/
namespace Sakura;
use Illuminate\Database\Query\Builder;
/**
* Inheritable permission handler.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class PermissionHandler
{
public const TABLE = '';
public $user = 0;
public $ranks = [];
public $additionalRequirements = [];
private $permCache = [];
private $validCache = [];
public function __construct(User $user)
{
$this->user = $user->id;
$this->ranks = array_keys($user->ranks);
}
public function __get(string $name): bool
{
return $this->check($name);
}
public function __isset(string $name): bool
{
return $this->valid($name);
}
public function valid(string $name): bool
{
if (!array_key_exists($name, $this->validCache)) {
$column = 'perm_' . camel_to_snake($name);
$this->validCache[$name] = DB::getSchemaBuilder()->hasColumn(static::TABLE, $column);
}
return $this->validCache[$name];
}
public function check(string $name): bool
{
if (!array_key_exists($name, $this->permCache)) {
$column = 'perm_' . camel_to_snake($name);
$add_reqs = $this->additionalRequirements;
$result = DB::table(static::TABLE)
->where(function (Builder $query) {
$query->whereIn('rank_id', $this->ranks)
->orWhere('user_id', $this->user);
})
->where(function (Builder $query) use ($add_reqs) {
if (isset($add_reqs['where'])) {
foreach ($add_reqs['where'] as $col => $val) {
$query->where($col, $val);
}
}
if (isset($add_reqs['where_in'])) {
foreach ($add_reqs['where_in'] as $col => $val) {
$query->whereIn($col, $val);
}
}
})
->whereNotNull($column)
->groupBy($column)
->min($column);
$this->permCache[$name] = intval($result) === 1;
}
return $this->permCache[$name];
}
}

View file

@ -6,64 +6,12 @@
namespace Sakura;
use Illuminate\Database\Query\Builder;
/**
* User permission handler.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class UserPerms
class UserPerms extends PermissionHandler
{
private static $table = 'perms';
private $user = 0;
private $ranks = [];
private $permCache = [];
private $validCache = [];
public function __construct(User $user)
{
$this->user = $user->id;
$this->ranks = array_keys($user->ranks);
}
public function __get(string $name): bool
{
return $this->check($name);
}
public function __isset(string $name): bool
{
return $this->valid($name);
}
public function valid(string $name): bool
{
if (!array_key_exists($name, $this->validCache)) {
$column = 'perm_' . camel_to_snake($name);
$this->validCache[$name] = DB::getSchemaBuilder()->hasColumn(static::$table, $column);
}
return $this->validCache[$name];
}
public function check(string $name): bool
{
if (!array_key_exists($name, $this->permCache)) {
$column = 'perm_' . camel_to_snake($name);
$result = DB::table(static::$table)
->where(function (Builder $query) {
$query->whereIn('rank_id', $this->ranks)
->orWhere('user_id', $this->user);
})
->whereNotNull($column)
->groupBy($column)
->min($column);
$this->permCache[$name] = intval($result) === 1;
}
return $this->permCache[$name];
}
public const TABLE = 'perms';
}