Completed
Push — master ( f67f4f...77dcfd )
by Curtis
40s queued 12s
created

Role::menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Models\enso\Roles;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Auth;
7
use App\Models\User;
8
use App\Models\enso\core\UserGroup;
9
use App\Models\enso\Menus\Menu;
10
use App\Models\enso\Permissions\Permission;
11
use LaravelEnso\Rememberable\App\Traits\Rememberable;
12
use LaravelEnso\Roles\App\Exceptions\RoleConflict;
13
use LaravelEnso\Roles\App\Services\ConfigWriter;
14
use LaravelEnso\Tables\App\Traits\TableCache;
15
16
/**
17
 * @property int $id
18
 * @property int $menu_id
19
 * @property string $name
20
 * @property string $display_name
21
 * @property string $description
22
 * @property string $created_at
23
 * @property string $updated_at
24
 * @property Menu $menu
25
 * @property PermissionRole[] $permissionRoles
26
 * @property RoleUserGroup[] $roleUserGroups
27
 * @property User[] $users
28
 */
29
class Role extends Model
30
{
31
    use Rememberable, TableCache;
0 ignored issues
show
Bug introduced by
The trait LaravelEnso\Rememberable\App\Traits\Rememberable requires the property $cacheLifetime which is not provided by App\Models\enso\Roles\Role.
Loading history...
32
33
    protected $fillable = ['menu_id', 'name', 'display_name', 'description'];
34
35
    public function menu()
36
    {
37
        return $this->belongsTo(Menu::class);
38
    }
39
40
    public function userGroups()
41
    {
42
        return $this->belongsToMany(UserGroup::class);
43
    }
44
45
    public function users()
46
    {
47
        return $this->hasMany(User::class);
48
    }
49
50
    public function permissions()
51
    {
52
        return $this->belongsToMany(Permission::class)->withTimestamps();
53
    }
54
55
    public function scopeVisible($query)
56
    {
57
        $fromAdminGroup = Auth::user()->belongsToAdminGroup();
0 ignored issues
show
Bug introduced by
The method belongsToAdminGroup() 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

57
        $fromAdminGroup = Auth::user()->/** @scrutinizer ignore-call */ belongsToAdminGroup();
Loading history...
58
59
        return $query->when(! $fromAdminGroup, fn ($query) => $query->whereHas(
60
            'userGroups',
61
            fn ($groups) => $groups->whereId(Auth::user()->group_id)
0 ignored issues
show
Bug introduced by
Accessing group_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
62
        ));
63
    }
64
65
    public function syncPermissions($permissionList)
66
    {
67
        $this->permissions()
68
            ->sync($permissionList);
69
    }
70
71
    public function addDefaultPermissions()
72
    {
73
        $this->permissions()
74
            ->sync(Permission::implicit()->pluck('id'));
75
    }
76
77
    public function delete()
78
    {
79
        if ($this->users()->exists()) {
80
            throw RoleConflict::inUse();
81
        }
82
83
        parent::delete();
84
    }
85
86
    public function writeConfig()
87
    {
88
        (new ConfigWriter($this))->handle();
0 ignored issues
show
Bug introduced by
$this of type App\Models\enso\Roles\Role is incompatible with the type LaravelEnso\Roles\App\Models\Role expected by parameter $role of LaravelEnso\Roles\App\Se...igWriter::__construct(). ( Ignorable by Annotation )

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

88
        (new ConfigWriter(/** @scrutinizer ignore-type */ $this))->handle();
Loading history...
89
    }
90
}
91