Passed
Pull Request — master (#610)
by John
22:48
created

GithubController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\OAuth;
4
5
use App\Models\Eloquent\User;
6
use App\Models\Eloquent\UserExtra;
7
use Laravel\Socialite\Facades\Socialite;
8
use Illuminate\Database\QueryException;
9
use Auth;
10
use Str;
11
12
class GithubController extends OAuthController
13
{
14
    public function __construct()
15
    {
16
        $this->platform="Github";
0 ignored issues
show
Bug Best Practice introduced by
The property platform does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->platformID="github";
18
    }
19
20
    public function redirectTo()
21
    {
22
        // 2 ways to access this page, you want to bind(logined), you want to login(not logined)
23
        if (Auth::check()) {
24
            // logined user, only users with non-temp email & pass access and binded can unbind
25
            if (!Auth::user()->isIndependent()) {
0 ignored issues
show
Bug introduced by
The method isIndependent() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

25
            if (!Auth::user()->/** @scrutinizer ignore-call */ isIndependent()) {
Loading history...
26
                return redirect()->route('account.settings');
27
            }
28
            if (Auth::user()->getExtra('github_id')) {
0 ignored issues
show
Bug introduced by
The method getExtra() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

28
            if (Auth::user()->/** @scrutinizer ignore-call */ getExtra('github_id')) {
Loading history...
29
                return $this->generateOperationView(Auth::user()->getExtra('github_email'));
30
            }
31
        }
32
        return Socialite::driver('github')->redirect();
33
    }
34
35
    public function handleCallback()
36
    {
37
        try {
38
            $github_user=Socialite::driver('github')->user();
39
        } catch (\Laravel\Socialite\Two\InvalidStateException $e) {
40
            return redirect()->route('home');
41
        }
42
43
        if (Auth::check()) {
44
            $user_id=Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
45
            $ret=UserExtra::search('github_id', $github_user->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
            if (!empty($ret) && $ret[0]['uid']!=$user_id) {
47
                $user=User::find($ret[0]['uid']);
48
                return $this->generateDuplicateView($user->email);
49
            }
50
            Auth::user()->setExtra('github_id', $github_user->id);
0 ignored issues
show
Bug introduced by
The method setExtra() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

50
            Auth::user()->/** @scrutinizer ignore-call */ setExtra('github_id', $github_user->id);
Loading history...
51
            Auth::user()->setExtra('github_email', $github_user->email);
0 ignored issues
show
Bug introduced by
Accessing email on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52
            Auth::user()->setExtra('github_nickname', $github_user->nickname);
0 ignored issues
show
Bug introduced by
Accessing nickname on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
            Auth::user()->setExtra('github_homepage', ($github_user->user)['html_url']);
0 ignored issues
show
Bug introduced by
Accessing user on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
54
            Auth::user()->setExtra('github_token', $github_user->token, 101);
0 ignored issues
show
Bug introduced by
Accessing token on the interface Laravel\Socialite\Contracts\User suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
            return $this->generateSuccessView(Auth::user()->getExtra('github_email'));
56
        } else {
57
            $ret=UserExtra::search('github_id', $github_user->id);
58
            if (!empty($ret)) {
59
                Auth::loginUsingId($ret[0]['uid'], true);
60
                Auth::user()->setExtra('github_email', $github_user->email);
61
                Auth::user()->setExtra('github_nickname', $github_user->nickname);
62
                Auth::user()->setExtra('github_homepage', ($github_user->user)['html_url']);
63
                Auth::user()->setExtra('github_token', $github_user->token, 101);
64
                return redirect()->route('account.dashboard');
65
            } else {
66
                if (config('app.allow_oauth_temp_account')) {
67
                    try {
68
                        $createdUser=User::create([
69
                            'name' => Str::random(12),
70
                            'email' => Str::random(16)."@temporary.email",
71
                            'password' => '',
72
                            'avatar' => '/static/img/avatar/default.png',
73
                        ]);
74
                    } catch (QueryException $exception) {
75
                        return $this->generateUnknownErrorView();
76
                    }
77
                    Auth::loginUsingId($createdUser->id, true);
78
                    Auth::user()->setExtra('github_id', $github_user->id);
79
                    Auth::user()->setExtra('github_email', $github_user->email);
80
                    Auth::user()->setExtra('github_nickname', $github_user->nickname);
81
                    Auth::user()->setExtra('github_homepage', ($github_user->user)['html_url']);
82
                    Auth::user()->setExtra('github_token', $github_user->token, 101);
83
                    return redirect()->route('account.dashboard');
84
                }
85
                $buttons=[[
86
                    'text' => 'login',
87
                    'href' => route('login'),
88
                ]];
89
                if (config('function.register')) {
90
                    $buttons[]=[
91
                        'text' => 'register',
92
                        'href' => route('register'),
93
                    ];
94
                }
95
                return $this->generateAccountNotFoundView();
96
            }
97
        }
98
    }
99
100
    public function unbind()
101
    {
102
        if (!Auth::check()) {
103
            return redirect()->route('home');
104
        }
105
        if (Auth::user()->getExtra('github_id')) {
106
            return $this->generateUnbindConfirmView(Auth::user()->email, Auth::user()->getExtra('github_email'));
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
107
        } else {
108
            return $this->generateAlreadyUnbindView();
109
        }
110
    }
111
112
    public function confirmUnbind()
113
    {
114
        if (!Auth::check()) {
115
            return redirect()->route('home');
116
        }
117
        if (Auth::user()->getExtra('github_id')) {
118
            Auth::user()->setExtra('github_id', null);
119
            Auth::user()->setExtra('github_email', null);
120
            Auth::user()->setExtra('github_nickname', null);
121
            Auth::user()->setExtra('github_homepage', null);
122
            Auth::user()->setExtra('github_token', null);
123
            return $this->generateUnbindSuccessView();
124
        } else {
125
            return $this->generateAlreadyUnbindView();
126
        }
127
    }
128
}
129