Passed
Branch beta (1b8e35)
by Jon
07:16
created

Access::hasPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Services\Access;
4
5
use App\Events\Frontend\Auth\UserLoggedIn;
6
use App\Events\Frontend\Auth\UserLoggedOut;
7
use Illuminate\Contracts\Auth\Authenticatable;
8
9
/**
10
 * Class Access.
11
 */
12
class Access
13
{
14
    /**
15
     * Get the currently authenticated user or null.
16
     */
17
    public function user()
18
    {
19
        return auth()->user();
20
    }
21
22
    /**
23
     * Return if the current session user is a guest or not.
24
     *
25
     * @return mixed
26
     */
27
    public function guest()
28
    {
29
        return auth()->guest();
30
    }
31
32
    /**
33
     * @return mixed
34
     */
35
    public function logout()
36
    {
37
        event(new UserLoggedOut($this->user()));
38
39
        return auth()->logout();
40
    }
41
42
    /**
43
     * Get the currently authenticated user's id.
44
     *
45
     * @return mixed
46
     */
47
    public function id()
48
    {
49
        return auth()->id();
50
    }
51
52
    /**
53
     * @param Authenticatable $user
54
     * @param bool            $remember
55
     */
56
    public function login(Authenticatable $user, $remember = false)
57
    {
58
        $logged_in = auth()->login($user, $remember);
59
        event(new UserLoggedIn($this->user()));
60
61
        return $logged_in;
62
    }
63
64
    /**
65
     * @param $id
66
     *
67
     * @return mixed
68
     */
69
    public function loginUsingId($id)
70
    {
71
        $logged_in = auth()->loginUsingId($id);
72
        event(new UserLoggedIn($this->user()));
73
74
        return $logged_in;
75
    }
76
77
    /**
78
     * Checks if the current user has a Role by its name or id.
79
     *
80
     * @param string $role Role name.
81
     *
82
     * @return bool
83
     */
84
    public function hasRole($role)
85
    {
86
        if ($user = $this->user()) {
87
            return $user->hasRole($role);
88
        }
89
90
        return false;
91
    }
92
93
    /**
94
     * Checks if the user has either one or more, or all of an array of roles.
95
     *
96
     * @param  $roles
97
     * @param bool $needsAll
98
     *
99
     * @return bool
100
     */
101
    public function hasRoles($roles, $needsAll = false)
102
    {
103
        if ($user = $this->user()) {
104
            return $user->hasRoles($roles, $needsAll);
105
        }
106
107
        return false;
108
    }
109
110
    /**
111
     * Check if the current user has a permission by its name or id.
112
     *
113
     * @param string $permission Permission name or id.
114
     *
115
     * @return bool
116
     */
117
    public function allow($permission)
118
    {
119
        if ($user = $this->user()) {
120
            return $user->allow($permission);
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * Check an array of permissions and whether or not all are required to continue.
128
     *
129
     * @param  $permissions
130
     * @param  $needsAll
131
     *
132
     * @return bool
133
     */
134
    public function allowMultiple($permissions, $needsAll = false)
135
    {
136
        if ($user = $this->user()) {
137
            return $user->allowMultiple($permissions, $needsAll);
138
        }
139
140
        return false;
141
    }
142
143
    /**
144
     * @param  $permission
145
     *
146
     * @return bool
147
     */
148
    public function hasPermission($permission)
149
    {
150
        return $this->allow($permission);
151
    }
152
153
    /**
154
     * @param  $permissions
155
     * @param  $needsAll
156
     *
157
     * @return bool
158
     */
159
    public function hasPermissions($permissions, $needsAll = false)
160
    {
161
        return $this->allowMultiple($permissions, $needsAll);
162
    }
163
}
164