Completed
Push — master ( 36d859...f1cb45 )
by Mikaël
08:46
created

Authorizations::scopeAuthorize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
cc 1
nc 1
nop 2
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');
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...
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