1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaPermission\Traits; |
4
|
|
|
|
5
|
|
|
use Avec\Models\Chamber as ChamberModel; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
8
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
9
|
|
|
use Illuminate\Database\Query\JoinClause; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
use Spatie\Permission\PermissionRegistrar; |
12
|
|
|
|
13
|
|
|
trait Authorizations |
14
|
|
|
{ |
15
|
|
|
public function authorizations(): MorphMany |
16
|
|
|
{ |
17
|
|
|
/** @var \Spatie\Permission\PermissionRegistrar $registrar */ |
18
|
|
|
$registrar = app(PermissionRegistrar::class); |
19
|
|
|
|
20
|
|
|
return $this->morphMany(get_class($registrar->getPermissionClass()), 'authorizable'); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Scope the query to entries the user is authorized to retrieve. |
25
|
|
|
* |
26
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
27
|
|
|
* @param $user |
28
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
29
|
|
|
*/ |
30
|
|
|
public function scopeAuthorize(EloquentBuilder $query, $user): EloquentBuilder |
31
|
|
|
{ |
32
|
|
|
$permissionsTable = config('permission.table_names.permissions'); |
33
|
|
|
$modeHasPermissionsTable = config('permission.table_names.model_has_permissions'); |
34
|
|
|
$roleHasPermissionsTable = config('permission.table_names.role_has_permissions'); |
35
|
|
|
$modelHasRolesTable = config('permission.table_names.model_has_roles'); |
36
|
|
|
|
37
|
|
|
return $query->join("{$permissionsTable} as p", function (JoinClause $join) use ( |
38
|
|
|
$user, $modeHasPermissionsTable, $roleHasPermissionsTable, $modelHasRolesTable |
39
|
|
|
) { |
40
|
|
|
$join->on('p.authorizable_id', '=', 'chambers.id') |
41
|
|
|
->where('p.authorizable_type', '=', static::class) |
42
|
|
|
->where(function (JoinClause $query) use ( |
43
|
|
|
$user, $modeHasPermissionsTable, $roleHasPermissionsTable, $modelHasRolesTable |
44
|
|
|
) { |
45
|
|
|
$query->whereExists(function (QueryBuilder $query) use ($user, $modeHasPermissionsTable) { |
46
|
|
|
$query->select(DB::raw(1)) |
47
|
|
|
->from("$modeHasPermissionsTable as mhp") |
48
|
|
|
->whereRaw('mhp.permission_id = p.id') |
49
|
|
|
->where('mhp.model_type', '=', get_class($user)) |
50
|
|
|
->where('mhp.model_id', '=', $user->getKey()); |
51
|
|
|
})->orWhereExists(function (QueryBuilder $query) use ( |
52
|
|
|
$user, $roleHasPermissionsTable, $modelHasRolesTable |
53
|
|
|
) { |
54
|
|
|
$query->select(DB::raw(1)) |
55
|
|
|
->from("{$roleHasPermissionsTable} as rhp") |
56
|
|
|
->whereRaw('rhp.permission_id = p.id') |
57
|
|
|
->join("{$modelHasRolesTable} as mhr", function (JoinClause $join) use ($user) { |
58
|
|
|
$join->on('rhp.role_id', '=', 'mhr.role_id') |
59
|
|
|
->where('mhr.model_type', '=', get_class($user)) |
60
|
|
|
->where('mhr.model_id', '=', $user->getKey()); |
61
|
|
|
}); |
62
|
|
|
}); |
63
|
|
|
}); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.