Completed
Push — master ( bc883f...ec87df )
by Chris
02:16
created

Permission::roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Spatie\Permission\Guard;
6
use Illuminate\Support\Collection;
7
use Spatie\Permission\Traits\HasRoles;
8
use Illuminate\Database\Eloquent\Model;
9
use Spatie\Permission\PermissionRegistrar;
10
use Spatie\Permission\Traits\RefreshesPermissionCache;
11
use Illuminate\Database\Eloquent\Relations\MorphToMany;
12
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
13
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
14
use Spatie\Permission\Exceptions\PermissionAlreadyExists;
15
use Spatie\Permission\Contracts\Permission as PermissionContract;
16
17
class Permission extends Model implements PermissionContract
18
{
19
    use HasRoles;
20
    use RefreshesPermissionCache;
21
22
    public $guarded = ['id'];
23
24
    public function __construct(array $attributes = [])
25
    {
26
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
27
28
        parent::__construct($attributes);
29
30
        $this->setTable(config('permission.table_names.permissions'));
31
    }
32
33 View Code Duplication
    public static function create(array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
36
37
        if (static::getPermissions()->where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
38
            throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
39
        }
40
41
        if (isNotLumen() && app()::VERSION < '5.4') {
42
            return parent::create($attributes);
43
        }
44
45
        return static::query()->create($attributes);
46
    }
47
48
    /**
49
     * A permission can be applied to roles.
50
     */
51
    public function roles(): BelongsToMany
52
    {
53
        return $this->belongsToMany(
54
            config('permission.models.role'),
55
            config('permission.table_names.role_has_permissions')
56
        );
57
    }
58
59
    /**
60
     * A permission belongs to some users of the model associated with its guard.
61
     */
62
    public function users(): MorphToMany
63
    {
64
        return $this->morphedByMany(
65
            getModelForGuard($this->attributes['guard_name']),
66
            'model',
67
            config('permission.table_names.model_has_permissions'),
68
            'permission_id',
69
            'model_id'
70
        );
71
    }
72
73
    /**
74
     * Find a permission by its name (and optionally guardName).
75
     *
76
     * @param string $name
77
     * @param string|null $guardName
78
     *
79
     * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
80
     *
81
     * @return \Spatie\Permission\Contracts\Permission
82
     */
83 View Code Duplication
    public static function findByName(string $name, $guardName = null): PermissionContract
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
86
87
        $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();
88
89
        if (! $permission) {
90
            throw PermissionDoesNotExist::create($name, $guardName);
91
        }
92
93
        return $permission;
94
    }
95
96
    /**
97
     * Find a permission by its id (and optionally guardName).
98
     *
99
     * @param int $id
100
     * @param string|null $guardName
101
     *
102
     * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
103
     *
104
     * @return \Spatie\Permission\Contracts\Permission
105
     */
106 View Code Duplication
    public static function findById(int $id, $guardName = null): PermissionContract
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
109
110
        $permission = static::getPermissions()->where('id', $id)->where('guard_name', $guardName)->first();
111
112
        if (! $permission) {
113
            throw PermissionDoesNotExist::create($id, $guardName);
114
        }
115
116
        return $permission;
117
    }
118
119
    /**
120
     * Find or create permission by its name (and optionally guardName).
121
     *
122
     * @param string $name
123
     * @param string|null $guardName
124
     *
125
     * @return \Spatie\Permission\Contracts\Permission
126
     */
127
    public static function findOrCreate(string $name, $guardName = null): PermissionContract
128
    {
129
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
130
131
        $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();
132
133
        if (! $permission) {
134
            return static::create(['name' => $name, 'guard_name' => $guardName]);
135
        }
136
137
        return $permission;
138
    }
139
140
    /**
141
     * Get the current cached permissions.
142
     */
143
    protected static function getPermissions(): Collection
144
    {
145
        return app(PermissionRegistrar::class)->getPermissions();
146
    }
147
}
148