Passed
Push — master ( 2b0762...9c76a8 )
by Bertrand
26:13 queued 17:43
created

LoginController::authenticated()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 21
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 31
rs 8.4444
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Providers\RouteServiceProvider;
7
use App\Src\UseCases\Domain\Auth\LogUserFromSocialNetwork;
8
use Illuminate\Foundation\Auth\AuthenticatesUsers;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
use Illuminate\Support\Facades\Auth;
12
use Laravel\Socialite\Facades\Socialite;
13
14
class LoginController extends Controller
15
{
16
    use AuthenticatesUsers;
0 ignored issues
show
introduced by
The trait Illuminate\Foundation\Auth\AuthenticatesUsers requires some properties which are not provided by App\Http\Controllers\Auth\LoginController: $maxAttempts, $decayMinutes
Loading history...
17
18
    protected $redirectTo = 'profile';
19
20
    public function __construct()
21
    {
22
        $this->middleware('guest')->except('logout');
23
    }
24
25
    public function showLoginForm(Request $request)
26
    {
27
        if($request->session()->has('should_attach_to_organization')) {
28
            session()->reflash();
29
        }
30
31
        if($request->has('wiki_callback')){
32
            session()->flash('wiki_callback', $request->input('wiki_callback'));
33
            session()->flash('wiki_token', $request->input('wiki_token'));
34
        }
35
        return view('public.auth.login');
36
    }
37
38
    public function logout(Request $request)
39
    {
40
        if($request->session()->has('should_attach_to_organization')) {
41
            $shouldAttach = $request->session()->get('should_attach_to_organization');
42
            $shouldAttachToken = $request->session()->get('should_attach_to_organization_token');
43
            $linkToRedirect = $request->session()->get('should_attach_to_organization_redirect');
44
            $userToRegister = $request->session()->get('user_to_register');
45
        }
46
        $this->guard()->logout();
47
48
        $request->session()->invalidate();
49
50
        $request->session()->regenerateToken();
51
52
        if(isset($shouldAttach)){
53
            $request->session()->flash('should_attach_to_organization', $shouldAttach);
54
            $request->session()->flash('should_attach_to_organization_token', $shouldAttachToken);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $shouldAttachToken does not seem to be defined for all execution paths leading up to this point.
Loading history...
55
            $request->session()->flash('should_attach_to_organization_redirect', $linkToRedirect);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $linkToRedirect does not seem to be defined for all execution paths leading up to this point.
Loading history...
56
            $request->session()->flash('user_to_register', $userToRegister);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userToRegister does not seem to be defined for all execution paths leading up to this point.
Loading history...
57
        }
58
        if ($response = $this->loggedOut($request)) {
59
            return $response;
60
        }
61
62
        return $request->wantsJson()
63
            ? new Response('', 204)
64
            : redirect('/');
65
    }
66
67
    protected function loggedOut(Request $request)
68
    {
69
        $request->session()->reflash();
70
        if($request->session()->has('should_attach_to_organization')){
71
            $linkToRedirect = $request->session()->get('should_attach_to_organization_redirect');
72
            return $request->wantsJson()
73
                ? new Response('', 204)
74
                : redirect($linkToRedirect);
75
        }
76
    }
77
78
    protected function authenticated(Request $request, $user)
79
    {
80
        if($user->context_id === null){
81
            return redirect()->route('wizard.profile');
82
        }
83
84
        if($request->session()->has('sso')){
85
            if(!$user->hasVerifiedEmail()){
86
                $request->session()->flash('from_forum', true);
87
                return redirect()->route('email.verify');
88
            }
89
            $sso = $request->session()->get('sso');
90
            $sig = $request->session()->get('sig');
91
            return redirect('discourse/sso?sso='.$sso.'&sig='.$sig);
92
        }
93
94
        if($request->session()->has('wiki_callback')){
95
            $user->wiki_token = $request->session()->get('wiki_token');
96
            $user->save();
97
            $callback = urldecode($request->session()->get('wiki_callback'));
0 ignored issues
show
Bug introduced by
It seems like $request->session()->get('wiki_callback') can also be of type null; however, parameter $string of urldecode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

97
            $callback = urldecode(/** @scrutinizer ignore-type */ $request->session()->get('wiki_callback'));
Loading history...
98
            return redirect($callback);
99
        }
100
101
        if($request->session()->has('should_attach_to_organization') && $request->session()->get('should_attach_to_organization') !== null){
102
            $token = $request->session()->get('should_attach_to_organization_token');
103
            $link = route('organization.invite.show').'?&token='.$token;
104
            return $request->wantsJson()
105
                ? new Response('', 204)
106
                : redirect($link);
107
        }
108
        return redirect()->route('show.profile');
109
    }
110
111
    public function redirectToProvider(string $provider)
112
    {
113
        if($provider === 'twitter'){
114
            config(['services.'.$provider.'.redirect' => env(strtoupper($provider).'_CALLBACK_LOGIN')]);
115
            return Socialite::driver($provider)->redirect();
116
        }
117
118
        config(['services.'.$provider.'.redirect' => env(strtoupper($provider).'_CALLBACK_LOGIN')]);
119
        return Socialite::driver($provider)->redirectUrl(config('services.'.$provider.'.redirect'))->redirect();
0 ignored issues
show
Bug introduced by
The method redirectUrl() does not exist on Laravel\Socialite\Contracts\Provider. Did you maybe mean redirect()? ( Ignorable by Annotation )

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

119
        return Socialite::driver($provider)->/** @scrutinizer ignore-call */ redirectUrl(config('services.'.$provider.'.redirect'))->redirect();

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...
120
    }
121
122
    public function handleProviderCallback(string $provider, LogUserFromSocialNetwork $logUserFromSocialNetwork)
123
    {
124
        config(['services.'.$provider.'.redirect' => env(strtoupper($provider).'_CALLBACK_LOGIN')]);
125
        $logUserFromSocialNetwork->log($provider);
126
        return $this->authenticated(request(), Auth::user());
127
    }
128
}
129