Completed
Pull Request — master (#100)
by
unknown
01:13
created

ImpersonateController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B take() 0 31 8
A leave() 0 14 3
A info() 0 14 2
1
<?php
2
3
namespace Lab404\Impersonate\Controllers;
4
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Lab404\Impersonate\Services\ImpersonateManager;
9
10
class ImpersonateController extends Controller
11
{
12
    /** @var ImpersonateManager */
13
    protected $manager;
14
15
    /**
16
     * ImpersonateController constructor.
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
22
        $this->manager = app()->make(ImpersonateManager::class);
23
    }
24
25
    /**
26
     * @param int         $id
27
     * @param string|null $guardName
28
     * @return  RedirectResponse
29
     * @throws  \Exception
30
     */
31
    public function take(Request $request, $id, $guardName = null)
32
    {
33
        $guardName = $guardName ?? $this->manager->getDefaultSessionGuard();
34
35
        // Cannot impersonate yourself
36
        if ($id == $request->user()->getAuthIdentifier() && ($this->manager->getCurrentAuthGuardName() == $guardName)) {
37
            abort(403);
38
        }
39
40
        // Cannot impersonate again if you're already impersonate a user
41
        if ($this->manager->isImpersonating()) {
42
            abort(403);
43
        }
44
45
        if (!$request->user()->canImpersonate()) {
46
            abort(403);
47
        }
48
49
        $userToImpersonate = $this->manager->findUserById($id, $guardName);
50
51
        if ($userToImpersonate->canBeImpersonated()) {
0 ignored issues
show
Bug introduced by
The method canBeImpersonated() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
            if ($this->manager->take($request->user(), $userToImpersonate, $guardName)) {
53
                $takeRedirect = $this->manager->getTakeRedirectTo();
54
                if ($takeRedirect !== 'back') {
55
                    return redirect()->to($takeRedirect);
56
                }
57
            }
58
        }
59
60
        return redirect()->back();
61
    }
62
63
    /**
64
     * @return RedirectResponse
65
     */
66
    public function leave()
67
    {
68
        if (!$this->manager->isImpersonating()) {
69
            abort(403);
70
        }
71
72
        $this->manager->leave();
73
74
        $leaveRedirect = $this->manager->getLeaveRedirectTo();
75
        if ($leaveRedirect !== 'back') {
76
            return redirect()->to($leaveRedirect);
77
        }
78
        return redirect()->back();
79
    }
80
81
    public function info(Request $request)
82
    {
83
        $token = $request->bearerToken();
84
        $response = [
85
            'impersonating' => $this->manager->isImpersonating(),
86
            'data' => [
87
                'user_to_impersonate' => $request->user(),
88
                'impersonator' => $this->manager->isImpersonating() ?
89
                    $this->manager->findUserById($this->manager->getImpersonatorId()) : null,
90
                'token' => $token,
91
            ],
92
        ];
93
        return response()->json($response);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
94
    }
95
}
96