AssignedUser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 5
eloc 21
c 3
b 2
f 0
dl 0
loc 41
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 32 5
1
<?php
2
3
namespace Uccello\Core\Support\Scopes;
4
5
use Illuminate\Database\Eloquent\Scope;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Facades\Auth;
9
use Uccello\Core\Models\Domain;
10
use Uccello\Core\Models\Entity;
11
12
class AssignedUser implements Scope
13
{
14
    /**
15
     * Apply the scope to a given Eloquent query builder.
16
     *
17
     * @param  \Illuminate\Database\Eloquent\Builder  $query
18
     * @param  \Illuminate\Database\Eloquent\Model  $model
19
     * @return void
20
     */
21
    public function apply(Builder $builder, Model $model)
22
    {
23
        $user = Auth::user();
24
25
        // Records assigned to a group to which the user belongs
26
        if ($user && !$user->is_admin) {
0 ignored issues
show
Bug introduced by
Accessing is_admin on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
27
            $builder->whereIn($model->getTable().'.assigned_user_id', $user->getAllowedGroupUuids());
0 ignored issues
show
Bug introduced by
The method getAllowedGroupUuids() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
            $builder->whereIn($model->getTable().'.assigned_user_id', $user->/** @scrutinizer ignore-call */ getAllowedGroupUuids());
Loading history...
28
29
            // Records assigned to an user with roles subordonate to the roles of the user
30
            $builder->orWhereIn($model->getTable().'.assigned_user_id', function ($query) use ($user) {
31
                $entityTable = with(new Entity)->getTable();
32
                $privilegesTable = env('UCCELLO_TABLE_PREFIX', 'uccello_').'privileges';
33
34
                $domain = request()->has('domain') ? request('domain') : Domain::first();
35
                $subordonateRolesIds = $user->subordonateRolesIdsOnDomain($domain);
0 ignored issues
show
Bug introduced by
The method subordonateRolesIdsOnDomain() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
                /** @scrutinizer ignore-call */ 
36
                $subordonateRolesIds = $user->subordonateRolesIdsOnDomain($domain);
Loading history...
36
37
                $query->select($entityTable.'.id')
38
                    ->from($entityTable)
39
                    ->join($privilegesTable, function ($join) use($entityTable, $privilegesTable, $subordonateRolesIds) {
40
                        $join->on("$privilegesTable.user_id", '=', $entityTable.'.record_id')
41
                        ->whereIn("$privilegesTable.role_id", $subordonateRolesIds);
42
                    })
43
                    ->where("$entityTable.module_id", ucmodule('user')->id ?? null);
44
            });
45
46
            // Records created by the user
47
            if (!empty($model->module)) {
48
                $builder->orWhereIn($model->getTable().'.'.$model->getKeyName(), function ($query) use($model) {
49
                    $query->select('record_id')
50
                    ->from(with(new Entity)->getTable())
51
                    ->where('module_id', $model->module->id ?? null)
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
52
                    ->where('creator_id', auth()->id());
53
                });
54
            }
55
        }
56
    }
57
}
58