LoginController::showLoginForm()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
rs 10
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
                return redirect()->route('verification.notice');
87
            }
88
            $sso = $request->session()->get('sso');
89
            $sig = $request->session()->get('sig');
90
            return redirect('discourse/sso?sso='.$sso.'&sig='.$sig);
91
        }
92
93
        if($request->session()->has('wiki_callback')){
94
            $user->wiki_token = $request->session()->get('wiki_token');
95
            $user->save();
96
            $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

96
            $callback = urldecode(/** @scrutinizer ignore-type */ $request->session()->get('wiki_callback'));
Loading history...
97
            if(!$user->hasVerifiedEmail()){
98
                return redirect()->route('verification.notice');
99
            }
100
            return redirect($callback);
101
        }
102
103
        if($request->session()->has('should_attach_to_organization') && $request->session()->get('should_attach_to_organization') !== null){
104
            $token = $request->session()->get('should_attach_to_organization_token');
105
            $link = route('organization.invite.show').'?&token='.$token;
106
            return $request->wantsJson()
107
                ? new Response('', 204)
108
                : redirect($link);
109
        }
110
        return redirect()->route('show.profile');
111
    }
112
113
    public function redirectToProvider(string $provider)
114
    {
115
        if($provider === 'twitter'){
116
            config(['services.'.$provider.'.redirect' => env(strtoupper($provider).'_CALLBACK_LOGIN')]);
117
            return Socialite::driver($provider)->redirect();
118
        }
119
120
        config(['services.'.$provider.'.redirect' => env(strtoupper($provider).'_CALLBACK_LOGIN')]);
121
        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

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