Issues (18)

src/Helpers/auth.php (1 issue)

Labels
Severity
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
 * @return string|bool|null
40
 */
41
function activeUserRole($role = null)
42
{
43
    // Determine if the active user has a particular 'role name'
44
    if (isset($role) && is_int($role)) {
45
        return activeUser()->isRoleId($role);
46
    }
47
48
    // Determine if the active user has a particular 'role_id'
49
    elseif (isset($role) && is_string($role)) {
50
        return activeUser()->isRole($role);
51
    }
52
53
    // Return Active User's role name
54
    else {
55
        return (activeUser()) ? activeUser()->role->name : null;
0 ignored issues
show
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...
56
    }
57
}
58
59
/**
60
 * Determine if a User is an admin or is the active user.
61
 *
62
 * @param  int  $user_id
63
 * @return bool
64
 */
65
function isAdminOrActiveUser(int $user_id): bool
66
{
67
    return activeUser()->isAdmin() || isActiveUser($user_id);
68
}
69
70
/**
71
 * Determine if a $user_id is the active user.
72
 *
73
 * @param  int  $user_id
74
 * @return bool
75
 */
76
function isActiveUser(int $user_id): bool
77
{
78
    return activeUserID() == $user_id;
79
}
80