Passed
Pull Request — master (#17)
by Stephen
08:18 queued 04:17
created

isActiveUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Contracts\Auth\Authenticatable;
4
5
/**
6
 * Retrieve the active User.
7
 *
8
 * @return Authenticatable|null
9
 */
10
function activeUser()
11
{
12
    return auth()->user();
13
}
14
15
/**
16
 * Retrieve the active user's user_id.
17
 *
18
 * @return int|string|null
19
 */
20
function activeUserID()
21
{
22
    return (auth()->user()) ? auth()->id() : null;
23
}
24
25
/**
26
 * Retrieve the active user's name.
27
 *
28
 * @return string|null
29
 */
30
function activeUserName()
31
{
32
    return (auth()->user()) ? auth()->user()->name : null;
33
}
34
35
/**
36
 * Check if the active user has a particular role or retrieve the active user's role name.
37
 *
38
 * @param string|int|null $role
39
 *
40
 * @return string|bool|null
41
 */
42
function activeUserRole($role = null)
43
{
44
    // Determine if the active user has a particular 'role name'
45
    if (isset($role) && is_int($role)) {
46
        return activeUser()->isRoleId($role);
47
    }
48
49
    // Determine if the active user has a particular 'role_id'
50
    elseif (isset($role) && is_string($role)) {
51
        return activeUser()->isRole($role);
52
    }
53
54
    // Return Active User's role name
55
    else {
56
        return (activeUser()) ? activeUser()->role->name : null;
0 ignored issues
show
Bug introduced by
Accessing role on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
57
    }
58
}
59
60
/**
61
 * Determine if a User is an admin or is the active user.
62
 *
63
 * @param int $user_id
64
 *
65
 * @return bool
66
 */
67
function isAdminOrActiveUser(int $user_id): bool
68
{
69
    return activeUser()->isAdmin() || isActiveUser($user_id);
70
}
71
72
/**
73
 * Determine if a $user_id is the active user.
74
 *
75
 * @param int $user_id
76
 * @return bool
77
 */
78
function isActiveUser(int $user_id): bool
79
{
80
    return activeUserID() == $user_id;
81
}
82