Completed
Push — master ( 21b5e5...d039a4 )
by Mahmoud
03:43
created

AuthorizationTrait::hasAccess()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
1
<?php
2
3
namespace App\Containers\Authorization\Traits;
4
5
use Illuminate\Support\Facades\Auth;
6
7
/**
8
 * Class AuthorizationTrait
9
 *
10
 * @author  Mahmoud Zalt  <[email protected]>
11
 */
12
trait AuthorizationTrait
13
{
14
15
    /**
16
     * @return  \App\Containers\User\Models\User|null
17
     */
18
    public function getUser()
19
    {
20
        return Auth::user();
21
    }
22
23
    /**
24
     * @return  mixed
25
     */
26
    public function hasAdminRole()
27
    {
28
        return $this->hasRole('admin');
0 ignored issues
show
Bug introduced by
It seems like hasRole() 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...
29
    }
30
31
    /**
32
     * @return  mixed
33
     */
34
    public function hasClientRole()
35
    {
36
        return $this->hasRole('client');
0 ignored issues
show
Bug introduced by
It seems like hasRole() 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...
37
    }
38
39
    /**
40
     * This function will be called from the Requests (authorize) to check if a user
41
     * has permission to perform an action.
42
     * User can set multiple permissions (separated with "|") and if the user has
43
     * any of the permissions, he will be authorize to proceed with this action.
44
     *
45
     * @return  bool
46
     */
47
    public function hasAccess(User $user = null)
48
    {
49
        // if not in parameters, take from the request object {$this}
50
        $user = $user ? : $this->user();
0 ignored issues
show
Bug introduced by
It seems like user() 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...
51
52
        $hasAccess = array_merge(
53
            $this->hasAnyPermissionAccess($user),
54
            $this->hasAnyRoleAccess($user)
55
        );
56
57
        // allow access if user has access to any of the defined roles or permissions.
58
        return empty($hasAccess) ? true : in_array(true, $hasAccess);
59
    }
60
61
    /**
62
     * @param $user
63
     *
64
     * @return  array
65
     */
66 View Code Duplication
    private function hasAnyPermissionAccess($user)
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...
67
    {
68
        if (!array_key_exists('permissions', $this->access) || !$this->access['permissions']) {
0 ignored issues
show
Bug introduced by
The property access 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...
69
            return [];
70
        }
71
72
        $permissions = explode('|', $this->access['permissions']);
73
74
        $hasAccess = array_map(function ($permission) use ($user) {
75
            // Note: internal return
76
            return $user->hasPermissionTo($permission);
77
        }, $permissions);
78
79
        return $hasAccess;
80
    }
81
82
    /**
83
     * @param $user
84
     *
85
     * @return  array
86
     */
87 View Code Duplication
    private function hasAnyRoleAccess($user)
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...
88
    {
89
        if (!array_key_exists('roles', $this->access) || !$this->access['roles']) {
90
            return [];
91
        }
92
93
        $roles = explode('|', $this->access['roles']);
94
95
        $hasAccess = array_map(function ($role) use ($user) {
96
            // Note: internal return
97
            return $user->hasRole($role);
98
        }, $roles);
99
100
        return $hasAccess;
101
    }
102
}
103