Completed
Push — master ( c3e39f...37ed4c )
by Freek
03:52
created

HasPermissions::getGuardName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Permission\Traits;
4
5
use Spatie\Permission\PermissionRegistrar;
6
use Spatie\Permission\Contracts\Permission;
7
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
8
9
trait HasPermissions
10
{
11
    /**
12
     * Grant the given permission(s) to a role.
13
     *
14
     * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
15
     *
16
     * @return $this
17
     */
18 View Code Duplication
    public function givePermissionTo(...$permissions)
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...
19
    {
20
        $permissions = collect($permissions)
21
            ->flatten()
22
            ->map(function ($permission) {
23
                return $this->getStoredPermission($permission);
24
            })
25
            ->each(function ($permission) {
26
                $this->ensureGuardIsEqual($permission);
27
            })
28
            ->all();
29
30
        $this->permissions()->saveMany($permissions);
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
31
32
        $this->forgetCachedPermissions();
33
34
        return $this;
35
    }
36
37
    /**
38
     * Remove all current permissions and set the given ones.
39
     *
40
     * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
41
     *
42
     * @return $this
43
     */
44
    public function syncPermissions(...$permissions)
45
    {
46
        $this->permissions()->detach();
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
48
        return $this->givePermissionTo($permissions);
49
    }
50
51
    /**
52
     * Revoke the given permission.
53
     *
54
     * @param \Spatie\Permission\Contracts\Permission|string $permission
55
     *
56
     * @return $this
57
     */
58
    public function revokePermissionTo($permission)
59
    {
60
        $this->permissions()->detach($this->getStoredPermission($permission));
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
62
        $this->forgetCachedPermissions();
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
69
     *
70
     * @return \Spatie\Permission\Contracts\Permission
71
     */
72
    protected function getStoredPermission($permissions): Permission
73
    {
74
        if (is_string($permissions)) {
75
            return app(Permission::class)->findByName($permissions, $this->getGuardName());
76
        }
77
78
        if (is_array($permissions)) {
79
            return app(Permission::class)
80
                ->whereIn('name', $permissions)
81
                ->where('guard_name', $this->getGuardName())
82
                ->get();
83
        }
84
85
        return $permissions;
86
    }
87
88
    /**
89
     * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission
90
     *
91
     * @throws \Spatie\Permission\Exceptions\GuardMismatch
92
     */
93
    protected function ensureGuardIsEqual($roleOrPermission)
94
    {
95
        if ($roleOrPermission->guard_name !== $this->getGuardName()) {
96
            throw GuardDoesNotMatch::create($roleOrPermission->guard_name, $this->getGuardName());
97
        }
98
    }
99
100
    /**
101
     * Check if the class is assigned to a guard in `config/auth.php`.
102
     */
103
    protected function isAssignedToGuard(): bool
104
    {
105
        return (bool) $this->getGuardName();
106
    }
107
108
    /**
109
     * Get the name of the guard that this class is assigned to based on it's `guard_name` property or the auth config
110
     * file.
111
     */
112
    protected function getGuardName(): string
113
    {
114
        return $this->guard_name ?? array_search(get_class($this), $this->getAllAuthGuardProviderModels());
0 ignored issues
show
Bug introduced by
The property guard_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
115
    }
116
117
    /**
118
     * Get an array of all models assigned to guards with the guard names as the keys.
119
     */
120
    protected function getAllAuthGuardProviderModels(): array
121
    {
122
        return collect(config('auth.guards'))
123
            ->map(function ($guard) {
124
                return config("auth.providers.{$guard['provider']}.model");
125
            })->toArray();
126
    }
127
128
    /**
129
     * Forget the cached permissions.
130
     */
131
    public function forgetCachedPermissions()
132
    {
133
        app(PermissionRegistrar::class)->forgetCachedPermissions();
134
    }
135
}
136