Completed
Push — master ( 3c03ca...d45e9e )
by Freek
02:07
created

Permission::users()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Permission\PermissionRegistrar;
8
use Spatie\Permission\Traits\RefreshesPermissionCache;
9
use Illuminate\Database\Eloquent\Relations\MorphToMany;
10
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
11
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12
use Spatie\Permission\Exceptions\PermissionAlreadyExists;
13
use Spatie\Permission\Contracts\Permission as PermissionContract;
14
15
class Permission extends Model implements PermissionContract
16
{
17
    use RefreshesPermissionCache;
18
19
    public $guarded = ['id'];
20
21
    public function __construct(array $attributes = [])
22
    {
23
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
24
25
        parent::__construct($attributes);
26
27
        $this->setTable(config('permission.table_names.permissions'));
28
    }
29
30 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...
31
    {
32
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
33
34
        if (static::getPermissions()->where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
35
            throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
36
        }
37
38
        return static::query()->create($attributes);
39
    }
40
41
    /**
42
     * A permission can be applied to roles.
43
     */
44
    public function roles(): BelongsToMany
45
    {
46
        return $this->belongsToMany(
47
            config('permission.models.role'),
48
            config('permission.table_names.role_has_permissions')
49
        );
50
    }
51
52
    /**
53
     * A permission belongs to some users of the model associated with its guard.
54
     */
55 View Code Duplication
    public function users(): MorphToMany
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...
56
    {
57
        return $this->morphedByMany(
58
            getModelForGuard($this->attributes['guard_name']),
59
            'model',
60
            config('permission.table_names.model_has_permissions'),
61
            'permission_id',
62
            'model_id'
63
        );
64
    }
65
66
    /**
67
     * Find a permission by its name (and optionally guardName).
68
     *
69
     * @param string $name
70
     * @param string|null $guardName
71
     *
72
     * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
73
     *
74
     * @return \Spatie\Permission\Contracts\Permission
75
     */
76
    public static function findByName(string $name, $guardName = null): PermissionContract
77
    {
78
        $guardName = $guardName ?? config('auth.defaults.guard');
79
80
        $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();
81
82
        if (! $permission) {
83
            throw PermissionDoesNotExist::create($name);
84
        }
85
86
        return $permission;
87
    }
88
89
    /**
90
     * Get the current cached permissions.
91
     */
92
    protected static function getPermissions(): Collection
93
    {
94
        return app(PermissionRegistrar::class)->getPermissions();
95
    }
96
}
97