Completed
Push — master ( dcb410...e7354d )
by Stephen
07:30
created

UserOnly::negatePermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace z1haze\Acl\Traits;
4
5
use z1haze\Acl\Models\Permission;
6
7
/**
8
 * Class UserOnly
9
 * @package z1haze\Acl\Traits
10
 *
11
 * @property mixed id
12
 *
13
 * @method belongsToMany($relationship)
14
 * @method modifyPermissions($permissions, $action)
15
 */
16
trait UserOnly
17
{
18
    /** USER
19
     * A User can have permissions explicitly assigned to it
20
     *
21
     * @return mixed
22
     */
23
    public function permissions()
24
    {
25
        return $this->belongsToMany(config('laravel-acl.permission', Permission::class))->withPivot('negated')->withTimestamps();
26
    }
27
    
28
    /**
29
     * Negate a permission from a User
30
     *
31
     * @param $permission
32
     */
33
    public function negatePermission($permission)
34
    {
35
        $this->modifyPermissions([$permission], 'negate');
36
    }
37
38
    /**
39
     * Negate an array of permissions from a User
40
     *
41
     * @param $permissions
42
     */
43
    public function negatePermissions(array $permissions)
44
    {
45
        $this->modifyPermissions($permissions, 'negate');
46
    }
47
48
    /**
49
     * First try the cache to return the collection,
50
     * then fetch it from the database.
51
     *
52
     * See @cacheGetNegatedPermissions()
53
     * @return mixed
54
     */
55
    public function getNegatedPermissions()
56
    {
57
        return \Cache::remember(
58
            'laravel-acl.getNegatedPermissionsForUser_' . $this->id,
59
            config('laravel-acl.cacheMinutes'),
60
            function () {
61
                return $this->cacheGetNegatedPermissions();
62
            }
63
        );
64
    }
65
66
    /**
67
     * Return a collection of permissions that
68
     * are negated for a User
69
     *
70
     * @return mixed
71
     */
72
    protected function cacheGetNegatedPermissions()
73
    {
74
        return $this->permissions()->wherePivot('negated', true)->get();
75
    }
76
}