FEATURE #2048: add basics of permissions management
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Permission extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'string_id',
|
||||
'parent_id',
|
||||
'group',
|
||||
'name',
|
||||
];
|
||||
|
||||
public function parent() {
|
||||
return $this->hasOne(Permission::class, 'parent_id');
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ class User extends Model implements AuthenticableInterface, CanResetPassword
|
||||
const ROLE_VISITOR = 'visitor';
|
||||
const ROLE_EDITOR = 'editor';
|
||||
const ROLE_ADMIN = 'admin';
|
||||
protected static $abilities;
|
||||
public $timestamps = true;
|
||||
protected $fillable = [
|
||||
'email',
|
||||
@@ -60,4 +61,25 @@ class User extends Model implements AuthenticableInterface, CanResetPassword
|
||||
{
|
||||
$this->notify(new ResetPasswordNotification($token));
|
||||
}
|
||||
|
||||
public function permissions()
|
||||
{
|
||||
return $this->belongsToMany(Permission::class, 'user_has_permissions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current user has given ability
|
||||
*
|
||||
* @param $ability
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAbility($ability)
|
||||
{
|
||||
if (empty(self::$abilities)) {
|
||||
self::$abilities = $this->permissions->pluck('string_id')->toArray();
|
||||
}
|
||||
|
||||
return in_array($ability, self::$abilities);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user