Last active
May 22, 2023 06:07
-
-
Save zalviandyr/8666747dce59563bed753d495c2df3cc to your computer and use it in GitHub Desktop.
vendor/alurkerja-laravolt/rbac/src/Traits/HasDatabaseRoleAndPermission.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Laravolt\Rbac\Traits; | |
use Illuminate\Database\Eloquent\Collection; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Support\Facades\Cache; | |
use Illuminate\Support\Str; | |
use Laravolt\Platform\Models\Permission; | |
trait HasDatabaseRoleAndPermission | |
{ | |
public function roles(): \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
{ | |
return $this->belongsToMany(config('rbac.models.role'), 'acl_role_user', 'user_id', 'role_id'); | |
} | |
public function permissions(): Collection | |
{ | |
// save users permissions result for 1 hour (3600 seconds) | |
return Cache::remember("users.{$this->getKey()}.permissions", 3600, function () { | |
/** @var Permission $permissionModel */ | |
$permissionModel = app(config('rbac.models.permission')); | |
$userModel = app(config('rbac.models.user')); | |
$userTable = $userModel->getTable(); | |
return $permissionModel | |
->newModelQuery() | |
->selectRaw('acl_permissions.*') | |
->join('acl_permission_role', 'acl_permissions.id', '=', 'acl_permission_role.permission_id') | |
->join('acl_role_user', 'acl_role_user.role_id', '=', 'acl_permission_role.role_id') | |
->join($userTable, "$userTable.id", '=', 'acl_role_user.user_id') | |
->where("$userTable.id", $this->getKey()) | |
->get() | |
->unique(); | |
}); | |
} | |
public function getPermissionsAttribute(): Collection | |
{ | |
return $this->permissions(); | |
} | |
public function assignRole($role): self | |
{ | |
if (is_array($role)) { | |
foreach ($role as $r) { | |
$this->assignRole($r); | |
} | |
return $this; | |
} | |
if (is_string($role) && !Str::isUuid($role)) { | |
$role = app(config('rbac.models.role'))->firstOrCreate(['name' => $role]); | |
} | |
$this->roles()->syncWithoutDetaching($role); | |
return $this; | |
} | |
public function revokeRole($role): self | |
{ | |
if (is_array($role)) { | |
foreach ($role as $r) { | |
$this->revokeRole($r); | |
} | |
return $this; | |
} | |
if (is_string($role) && !Str::isUuid($role)) { | |
$role = app(config('rbac.models.role'))->where('name', $role)->first(); | |
} | |
$this->roles()->detach($role); | |
return $this; | |
} | |
public function hasRole($role, $checkAll = false): bool | |
{ | |
if (is_array($role)) { | |
$match = 0; | |
foreach ($role as $r) { | |
$match += (int)$this->hasRole($r, $checkAll); | |
} | |
if ($checkAll) { | |
return $match == count($role); | |
} | |
return $match > 0; | |
} | |
if (Str::isUuid($role)) { | |
$role = $this->roles->firstWhere('id', $role); | |
} | |
if (is_string($role)) { | |
$role = $this->roles->firstWhere('name', $role); | |
} | |
if (is_int($role)) { | |
$role = $this->roles->firstWhere('id', $role); | |
} | |
if (!$role instanceof Model) { | |
return false; | |
} | |
foreach ($this->roles as $assignedRole) { | |
if ($role->is($assignedRole)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public function syncRoles($roles): self | |
{ | |
$ids = collect($roles)->transform(function ($role) { | |
if (is_numeric($role)) { | |
return (int)$role; | |
} | |
if (Str::isUuid($role)) { | |
return $role; | |
} | |
if (is_string($role)) { | |
$role = app(config('rbac.models.role'))->firstOrCreate(['name' => $role]); | |
return $role->getKey(); | |
} | |
if ($role instanceof Model) { | |
return $role->getKey(); | |
} | |
return $role; | |
})->filter(function ($id) { | |
return $id > 0; | |
}); | |
$this->roles()->sync($ids); | |
return $this; | |
} | |
public function hasPermission($permission, $checkAll = false): bool | |
{ | |
return once(function () use ($permission, $checkAll) { | |
return $this->_hasPermission($permission, $checkAll); | |
}); | |
} | |
public function getActionPermissionFromUrl($url , $action) | |
{ | |
$seg = explode("/", $url); | |
$module = $seg[2]; | |
$table = $seg[3]; | |
return $this->hasActionPermission($module, $table, $action); | |
} | |
public function hasActionPermission($module, $table, $action): bool | |
{ | |
return once(function () use ($module, $table, $action) { | |
return $this->_hasActionPermission($module, $table, $action); | |
}); | |
} | |
protected function _hasActionPermission($module, $table, $action): bool | |
{ | |
// dump($module); | |
// dd($this->permissions()->where('module', $module)->first()); | |
// dd($this->permissions()->toArray()); | |
return (bool)$this->permissions()->where('module', $module)->where('table', $table)->where('action', $action)->first(); | |
} | |
protected | |
function _hasPermission($permission, $checkAll = false): bool | |
{ | |
if (is_array($permission)) { | |
$match = 0; | |
foreach ($permission as $perm) { | |
$match += (int)$this->hasPermission($perm); | |
} | |
if ($checkAll) { | |
return $match == count($permission); | |
} | |
return $match > 0; | |
} | |
if (Str::isUuid($permission)) { | |
return (bool)$this->permissions()->firstWhere('id', $permission); | |
} | |
if (is_string($permission)) { | |
return (bool)$this->permissions()->firstWhere('name', $permission); | |
} | |
if (is_int($permission)) { | |
return (bool)$this->permissions()->firstWhere('id', $permission); | |
} | |
if ($permission instanceof Model) { | |
return (bool)$this->permissions()->firstWhere('id', $permission->getKey()); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment