| 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"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                 return redirect()->route('account.settings'); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             if (Auth::user()->getExtra('github_id')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $ret=UserExtra::search('github_id', $github_user->id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             Auth::user()->setExtra('github_email', $github_user->email); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             Auth::user()->setExtra('github_nickname', $github_user->nickname); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             Auth::user()->setExtra('github_homepage', ($github_user->user)['html_url']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             Auth::user()->setExtra('github_token', $github_user->token, 101); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  |