Completed
Pull Request — master (#38)
by Arjay
12:59
created

HasRoleAndPermission::grantPermissionBySlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Yajra\Acl\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Yajra\Acl\Models\Permission;
7
8
/**
9
 * @mixin \Illuminate\Database\Eloquent\Model
10
 * @property Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
11
 */
12
trait HasRoleAndPermission
13
{
14
    use HasRole {
15
        HasRole::getPermissions as getRolePermissions;
16
    }
17
18
    /**
19
     * Grants the given permission to the user.
20
     *
21
     * @param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array  $id
22
     * @param  array  $attributes
23
     * @param  bool  $touch
24
     * @return void
25
     */
26
    public function grantPermission($id, array $attributes = [], $touch = true)
27
    {
28
        $this->permissions()->attach($id, $attributes, $touch);
29
    }
30
31
    /**
32
     * Get related permissions.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
35
     */
36
    public function permissions(): BelongsToMany
37
    {
38
        return $this->belongsToMany(config('acl.permission', Permission::class))->withTimestamps();
0 ignored issues
show
Documentation Bug introduced by
The method belongsToMany does not exist on object<Yajra\Acl\Traits\HasRoleAndPermission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
39
    }
40
41
    /**
42
     * Grant user permission by slug.
43
     *
44
     * @param  string|array  $slug
45
     */
46
    public function grantPermissionBySlug($slug)
47
    {
48
        $this->permissions()
49
            ->newQuery()
50
            ->whereIn('slug', (array) $slug)
51
            ->pluck('id')
52
            ->each(function ($id) {
53
                $this->permissions()->attach($id);
54
            });
55
    }
56
57
    /**
58
     * Grant user permissions by resource.
59
     *
60
     * @param  string|array  $resource
61
     */
62
    public function grantPermissionByResource($resource)
63
    {
64
        $this->permissions()
65
            ->newQuery()
66
            ->whereIn('resource', (array) $resource)
67
            ->pluck('id')
68
            ->each(function ($id) {
69
                $this->permissions()->attach($id);
70
            });
71
    }
72
73
    /**
74
     * Revokes the given permission from the user.
75
     *
76
     * @param  mixed  $ids
77
     * @param  bool  $touch
78
     * @return int
79
     */
80
    public function revokePermission($ids = null, $touch = true): int
81
    {
82
        return $this->permissions()->detach($ids, $touch);
83
    }
84
85
    /**
86
     * Syncs the given permission(s) with the user.
87
     *
88
     * @param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array  $ids
89
     * @param  bool  $detaching
90
     * @return array
91
     */
92
    public function syncPermissions($ids, $detaching = true): array
93
    {
94
        return $this->permissions()->sync($ids, $detaching);
95
    }
96
97
    /**
98
     * Revokes all permissions from the user.
99
     *
100
     * @return int
101
     */
102
    public function revokeAllPermissions(): int
103
    {
104
        return $this->permissions()->detach();
105
    }
106
107
    /**
108
     * Get all user permissions slug.
109
     *
110
     * @return array|null
111
     */
112
    public function getPermissions(): array
113
    {
114
        $rolePermissions = $this->getRolePermissions();
0 ignored issues
show
Bug introduced by
The method getRolePermissions() does not exist on Yajra\Acl\Traits\HasRoleAndPermission. Did you maybe mean permissions()?

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...
115
        $userPermissions = $this->permissions->pluck('slug')->toArray();
116
117
        return collect($userPermissions)->merge($rolePermissions)->unique()->toArray();
118
    }
119
}
120