Passed
Push — master ( 54115d...67a0b3 )
by meta
02:30
created

getAuthorizedUserRolesAbilities()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth\Controller;
4
5
use Illuminate\Routing\Controller;
6
use Laravel\Socialite\Facades\Socialite;
7
8
class ApiAuthController extends AuthController
9
{
10
    // Helper to find a token wherever it is hidden and attempt to auth it
11
    public function extractOauthAccessTokenFromRequest(\Illuminate\Http\Request $request)
12
    {
13
        $oauthAccessToken = '';
14
15
        // IF we get an explicit TOKEN=abc123 in the $request
16
        if ($request->query('token')) {
17
            $oauthAccessToken = $request->query('token');
18
        }
19
20
        // IF posted as access_token=abc123 in the $request
21
        if ($request->input('access_token')) {
22
            $oauthAccessToken = $request->input('access_token');
23
        }
24
25
        // IF the request has an Authorization: Bearer abc123 header
26
        $header = $request->headers->get('authorization');
27
        $regex = '/bearer\s+(\S+)/i';
28
        if ($header && preg_match($regex, $header, $matches)) {
29
            $oauthAccessToken = $matches[1];
30
        }
31
32
        return $oauthAccessToken;
33
    }
34
35
    // Route to dump out the authenticated API user
36
    public function getAuthorizedUserInfo(\Illuminate\Http\Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    public function getAuthorizedUserInfo(/** @scrutinizer ignore-unused */ \Illuminate\Http\Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        $user = auth()->user();
39
40
        return response()->json($user);
41
    }
42
43
    // Route to dump out the authenticated users groups/roles
44
    public function getAuthorizedUserRoles(\Illuminate\Http\Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

44
    public function getAuthorizedUserRoles(/** @scrutinizer ignore-unused */ \Illuminate\Http\Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        $user = auth()->user();
47
        $roles = $user->roles()->get();
48
49
        return response()->json($roles);
50
    }
51
52
    // Route to dump out the authenticated users group/roles abilities/permissions
53
    public function getAuthorizedUserRolesAbilities(\Illuminate\Http\Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function getAuthorizedUserRolesAbilities(/** @scrutinizer ignore-unused */ \Illuminate\Http\Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        $user = auth()->user();
56
        $roles = $user->roles()->get()->all();
57
        foreach ($roles as $key => $role) {
58
            $role->permissions = $role->abilities()->get()->all();
59
            if (! count($role->permissions)) {
60
                unset($roles[$key]);
61
            }
62
        }
63
        $roles = array_values($roles);
64
65
        return response()->json($roles);
66
    }
67
}
68