Completed
Push — master ( 5afcda...69d749 )
by Arjay
12:59
created

HasRole::isRole()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 4
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Yajra\Acl\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
use Yajra\Acl\Models\Role;
8
9
trait HasRole
10
{
11
    use InteractsWithRole;
12
13
    /**
14
     * Check if user have access using any of the acl (permissions or roles slug).
15
     *
16
     * @param  string|array  $acl
17
     * @return boolean
18
     */
19
    public function canAccess($acl): bool
20
    {
21
        return $this->canAtLeast($acl) || $this->hasRole($acl);
22
    }
23
24
    /**
25
     * Check if user has at least one of the given permissions
26
     *
27
     * @param  string|array  $permissions
28
     * @return bool
29
     */
30
    public function canAtLeast($permissions): bool
31
    {
32
        $can = false;
33
34
        if (auth()->check()) {
0 ignored issues
show
Bug introduced by
The method check does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
35
            /** @var Role $role */
36
            foreach ($this->roles as $role) {
37
                if ($role->canAtLeast($permissions)) {
38
                    $can = true;
39
                }
40
            }
41
        } else {
42
            try {
43
                $guest = $this->findRoleBySlug('guest');
44
45
                return $guest->canAtLeast($permissions);
46
            } catch (ModelNotFoundException $exception) {
47
                //
48
            }
49
        }
50
51
        return $can;
52
    }
53
54
55
    /**
56
     * Get all user role permissions.
57
     *
58
     * @return array
59
     */
60
    public function getPermissions(): array
61
    {
62
        $permissions = [[], []];
63
64
        foreach ($this->roles as $role) {
65
            $permissions[] = $role->getPermissions();
66
        }
67
68
        return call_user_func_array('array_merge', $permissions);
69
    }
70
71
    /**
72
     * Check if the given entity/model is owned by the user.
73
     *
74
     * @param  \Illuminate\Database\Eloquent\Model  $entity
75
     * @param  string  $relation
76
     * @return bool
77
     */
78
    public function owns(Model $entity, $relation = 'user_id'): bool
79
    {
80
        return $this->getKeyName() === $entity->{$relation};
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
    }
82
}
83