Authorizations::scopeAuthorize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 9.1781
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BBSLab\NovaPermission\Traits;
4
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Illuminate\Database\Query\Builder as QueryBuilder;
8
use Illuminate\Database\Query\JoinClause;
9
use Illuminate\Support\Facades\DB;
10
use Spatie\Permission\PermissionRegistrar;
11
12
trait Authorizations
13
{
14
    public function authorizations(): MorphMany
15
    {
16
        /** @var \Spatie\Permission\PermissionRegistrar $registrar */
17
        $registrar = app(PermissionRegistrar::class);
18
19
        return $this->morphMany(get_class($registrar->getPermissionClass()), 'authorizable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
20
    }
21
22
    /**
23
     * Scope the query to entries the user is authorized to retrieve.
24
     *
25
     * @param  \Illuminate\Database\Eloquent\Builder  $query
26
     * @param $user
27
     * @return \Illuminate\Database\Eloquent\Builder
28
     */
29
    public function scopeAuthorize(EloquentBuilder $query, $user): EloquentBuilder
30
    {
31
        $permissionsTable = config('permission.table_names.permissions');
32
        $rolesTable = config('permission.table_names.roles');
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, $rolesTable
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, $rolesTable
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
                    })->orWhereExists(function (QueryBuilder $query) use (
63
                        $user, $modelHasRolesTable, $rolesTable
64
                    ) {
65
                        $query->select(DB::raw(1))
66
                            ->from($modelHasRolesTable)
67
                            ->join($rolesTable, "{$rolesTable}.id", '=', "{$modelHasRolesTable}.role_id")
68
                            ->where("{$rolesTable}.override_permission", '=', true)
69
                            ->where("{$modelHasRolesTable}.model_type", '=', get_class($user))
70
                            ->where("{$modelHasRolesTable}.model_id", '=', $user->getkey());
71
                    });
72
                });
73
        });
74
    }
75
}
76