Completed
Pull Request — master (#1480)
by Richard
01:24
created

Role::findOrCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Spatie\Permission\Guard;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Permission\Traits\HasPermissions;
8
use Spatie\Permission\Exceptions\RoleDoesNotExist;
9
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
10
use Spatie\Permission\Contracts\Role as RoleContract;
11
use Spatie\Permission\Traits\RefreshesPermissionCache;
12
use Illuminate\Database\Eloquent\Relations\MorphToMany;
13
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
14
use Symfony\Component\Console\Output\ConsoleOutput;
15
16
class Role extends Model implements RoleContract
17
{
18
    use HasPermissions;
19
    use RefreshesPermissionCache;
20
21
    protected $guarded = ['id'];
22
23
    public function __construct(array $attributes = [])
24
    {
25
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
26
27
        parent::__construct($attributes);
28
29
        $this->setTable(config('permission.table_names.roles'));
30
    }
31
32
    public static function create(array $attributes = [])
33
    {
34
        $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
35
36
        if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
37
            return static::roleAlreadyExists($attributes['name'], $attributes['guard_name']);
38
        }
39
40
        return static::query()->create($attributes);
41
    }
42
43
    /**
44
     * A role may be given various permissions.
45
     */
46
    public function permissions(): BelongsToMany
47
    {
48
        return $this->belongsToMany(
49
            config('permission.models.permission'),
50
            config('permission.table_names.role_has_permissions'),
51
            'role_id',
52
            'permission_id'
53
        );
54
    }
55
56
    /**
57
     * A role belongs to some users of the model associated with its guard.
58
     */
59 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...
60
    {
61
        return $this->morphedByMany(
62
            getModelForGuard($this->attributes['guard_name']),
63
            'model',
64
            config('permission.table_names.model_has_roles'),
65
            'role_id',
66
            config('permission.column_names.model_morph_key')
67
        );
68
    }
69
70
    /**
71
     * Find a role by its name and guard name.
72
     *
73
     * @param string $name
74
     * @param string|null $guardName
75
     *
76
     * @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
77
     *
78
     * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
79
     */
80 View Code Duplication
    public static function findByName(string $name, $guardName = null): RoleContract
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...
81
    {
82
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
83
84
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
85
86
        if (! $role) {
87
            throw RoleDoesNotExist::named($name);
88
        }
89
90
        return $role;
91
    }
92
93 View Code Duplication
    public static function findById(int $id, $guardName = null): RoleContract
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...
94
    {
95
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
96
97
        $role = static::where('id', $id)->where('guard_name', $guardName)->first();
98
99
        if (! $role) {
100
            throw RoleDoesNotExist::withId($id);
101
        }
102
103
        return $role;
104
    }
105
106
    /**
107
     * Find or create role by its name (and optionally guardName).
108
     *
109
     * @param string $name
110
     * @param string|null $guardName
111
     *
112
     * @return \Spatie\Permission\Contracts\Role
113
     */
114 View Code Duplication
    public static function findOrCreate(string $name, $guardName = null): RoleContract
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...
115
    {
116
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
117
118
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
119
120
        if (! $role) {
121
            return static::query()->create(['name' => $name, 'guard_name' => $guardName]);
122
        }
123
124
        return $role;
125
    }
126
127
    /**
128
     * Determine if the user may perform the given permission.
129
     *
130
     * @param string|Permission $permission
131
     *
132
     * @return bool
133
     *
134
     * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
135
     */
136
    public function hasPermissionTo($permission): bool
137
    {
138
        if (config('permission.enable_wildcard_permission', false)) {
139
            return $this->hasWildcardPermission($permission, $this->getDefaultGuardName());
140
        }
141
142
        $permissionClass = $this->getPermissionClass();
143
144
        if (is_string($permission)) {
145
            $permission = $permissionClass->findByName($permission, $this->getDefaultGuardName());
146
        }
147
148
        if (is_int($permission)) {
149
            $permission = $permissionClass->findById($permission, $this->getDefaultGuardName());
150
        }
151
152
        if (! $this->getGuardNames()->contains($permission->guard_name)) {
153
            throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames());
154
        }
155
156
        return $this->permissions->contains('id', $permission->id);
157
    }
158
159
    protected static function roleAlreadyExists(string $roleName, string $guardName)
160
    {
161
        $message = "A role $roleName already exists for guard $guardName. Seed ignored.";
162
        $output = new ConsoleOutput();
163
        $output->writeln("<error>$message.</error>");
164
165
        return $message;
166
    }
167
168
}
169