|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Oauth; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use App\Models\AccountModel; |
|
8
|
|
|
use App\Models\UserModel; |
|
9
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
10
|
|
|
use Auth; |
|
11
|
|
|
|
|
12
|
|
|
class GithubController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
public function redirectTo() |
|
15
|
|
|
{ |
|
16
|
|
|
$accountModel = new AccountModel(); |
|
17
|
|
|
if(Auth::check() && $accountModel->getExtra(Auth::user()->id ,'github_id')){ |
|
18
|
|
|
return view('oauth.index',[ |
|
19
|
|
|
'page_title'=>"OAuth", |
|
20
|
|
|
'site_title'=>"NOJ", |
|
21
|
|
|
'navigation'=>"OAuth", |
|
22
|
|
|
'platform' => 'Github', |
|
23
|
|
|
'display_html' => 'You\'re already tied to the github account : <span class="text-info">'.$accountModel->getExtra(Auth::user()->id ,'github_email').'</span><br /> |
|
|
|
|
|
|
24
|
|
|
You can choose to unbind or go back to the homepage', |
|
25
|
|
|
'buttons' => [ |
|
26
|
|
|
[ |
|
27
|
|
|
'text' => 'unbind', |
|
28
|
|
|
'href' => route('oauth_github_unbind'), |
|
29
|
|
|
'style' => 'btn-danger' |
|
30
|
|
|
], |
|
31
|
|
|
[ |
|
32
|
|
|
'text' => 'home', |
|
33
|
|
|
'href' => route('home'), |
|
34
|
|
|
], |
|
35
|
|
|
] |
|
36
|
|
|
]); |
|
37
|
|
|
} |
|
38
|
|
|
return Socialite::driver('github')->redirect(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function handleCallback() |
|
42
|
|
|
{ |
|
43
|
|
|
$github_user = Socialite::driver('github')->user(); |
|
44
|
|
|
$accountModel = new AccountModel(); |
|
45
|
|
|
|
|
46
|
|
|
if(Auth::check()){ |
|
47
|
|
|
$user_id = Auth::user()->id; |
|
48
|
|
|
$ret = $accountModel->findExtra('github_id',$github_user->id); |
|
|
|
|
|
|
49
|
|
|
if(!empty($ret) && $ret['uid'] != $user_id){ |
|
50
|
|
|
$user = UserModel::find($ret['uid']); |
|
51
|
|
|
return view('oauth.index',[ |
|
52
|
|
|
'page_title'=>"OAuth", |
|
53
|
|
|
'site_title'=>"NOJ", |
|
54
|
|
|
'navigation'=>"OAuth", |
|
55
|
|
|
'platform' => 'Github', |
|
56
|
|
|
'display_html' => 'The github account is now tied to another NOJ account : <span class="text-danger">'.$user->email.'</span><br /> |
|
57
|
|
|
You can try logging in using github', |
|
58
|
|
|
'buttons' => [ |
|
59
|
|
|
[ |
|
60
|
|
|
'text' => 'home', |
|
61
|
|
|
'href' => route('home'), |
|
62
|
|
|
], |
|
63
|
|
|
] |
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
$accountModel->setExtra($user_id,'github_id',$github_user->id); |
|
67
|
|
|
$accountModel->setExtra($user_id,'github_email',$github_user->email); |
|
|
|
|
|
|
68
|
|
|
$accountModel->setExtra($user_id,'github_nickname',$github_user->nickname); |
|
|
|
|
|
|
69
|
|
|
$accountModel->setExtra($user_id,'github_homepage',($github_user->user)['html_url']); |
|
|
|
|
|
|
70
|
|
|
$accountModel->setExtra($user_id,'github_token',$github_user->token,101); |
|
|
|
|
|
|
71
|
|
|
return view('oauth.index',[ |
|
72
|
|
|
'page_title'=>"OAuth", |
|
73
|
|
|
'site_title'=>"NOJ", |
|
74
|
|
|
'navigation'=>"OAuth", |
|
75
|
|
|
'platform' => 'Github', |
|
76
|
|
|
'display_html' => 'You have successfully tied up the github account : <span class="text-info">'.$accountModel->getExtra(Auth::user()->id ,'github_email').'</span><br /> |
|
|
|
|
|
|
77
|
|
|
You can log in to NOJ later using this account', |
|
78
|
|
|
'buttons' => [ |
|
79
|
|
|
[ |
|
80
|
|
|
'text' => 'home', |
|
81
|
|
|
'href' => route('home'), |
|
82
|
|
|
], |
|
83
|
|
|
] |
|
84
|
|
|
]); |
|
85
|
|
|
}else{ |
|
86
|
|
|
$ret = $accountModel->findExtra('github_id',$github_user->id); |
|
87
|
|
|
if(!empty($ret)){ |
|
88
|
|
|
$github_user = Socialite::driver('github')->user(); |
|
89
|
|
|
Auth::loginUsingId($ret['uid']); |
|
90
|
|
|
$accountModel->setExtra($user_id,'github_email',$github_user->email); |
|
|
|
|
|
|
91
|
|
|
$accountModel->setExtra($user_id,'github_nickname',$github_user->nickname); |
|
92
|
|
|
$accountModel->setExtra($user_id,'github_homepage',($github_user->user)['html_url']); |
|
93
|
|
|
$accountModel->setExtra($user_id,'github_token',$github_user->token,101); |
|
94
|
|
|
return redirect('/'); |
|
95
|
|
|
}else{ |
|
96
|
|
|
return view('oauth.index',[ |
|
97
|
|
|
'page_title'=>"OAuth", |
|
98
|
|
|
'site_title'=>"NOJ", |
|
99
|
|
|
'navigation'=>"OAuth", |
|
100
|
|
|
'platform' => 'Github', |
|
101
|
|
|
'display_text' => 'This github account doesn\'t seem to have a NOJ account, please register or log in first', |
|
102
|
|
|
'buttons' => [ |
|
103
|
|
|
[ |
|
104
|
|
|
'text' => 'login', |
|
105
|
|
|
'href' => route('login'), |
|
106
|
|
|
], |
|
107
|
|
|
[ |
|
108
|
|
|
'text' => 'register', |
|
109
|
|
|
'href' => route('register'), |
|
110
|
|
|
], |
|
111
|
|
|
] |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function unbind() |
|
118
|
|
|
{ |
|
119
|
|
|
$accountModel = new AccountModel(); |
|
120
|
|
|
if($accountModel->getExtra(Auth::user()->id ,'github_id')){ |
|
121
|
|
|
return view('oauth.index',[ |
|
122
|
|
|
'page_title'=>"OAuth", |
|
123
|
|
|
'site_title'=>"NOJ", |
|
124
|
|
|
'navigation'=>"OAuth", |
|
125
|
|
|
'platform' => 'Github', |
|
126
|
|
|
'display_html' => 'You are trying to unbind the following two : <br /> |
|
127
|
|
|
Your NOJ account : <span class="text-info">'.Auth::user()->email.'</span><br /> |
|
|
|
|
|
|
128
|
|
|
This Github account : <span class="text-info">'.$accountModel->getExtra(Auth::user()->id ,'github_email').'</span><br /> |
|
|
|
|
|
|
129
|
|
|
Make your decision carefully, although you can later establish the binding again', |
|
130
|
|
|
'buttons' => [ |
|
131
|
|
|
[ |
|
132
|
|
|
'text' => 'confirm', |
|
133
|
|
|
'href' => route('oauth_github_unbind_confirm'), |
|
134
|
|
|
'style' => 'btn-danger' |
|
135
|
|
|
], |
|
136
|
|
|
[ |
|
137
|
|
|
'text' => 'home', |
|
138
|
|
|
'href' => route('home'), |
|
139
|
|
|
], |
|
140
|
|
|
] |
|
141
|
|
|
]); |
|
142
|
|
|
}else{ |
|
143
|
|
|
return view('oauth.index',[ |
|
144
|
|
|
'page_title'=>"OAuth", |
|
145
|
|
|
'site_title'=>"NOJ", |
|
146
|
|
|
'navigation'=>"OAuth", |
|
147
|
|
|
'platform' => 'Github', |
|
148
|
|
|
'display_html' => 'You\'re not tied to github', |
|
149
|
|
|
'buttons' => [ |
|
150
|
|
|
[ |
|
151
|
|
|
'text' => 'home', |
|
152
|
|
|
'href' => route('home'), |
|
153
|
|
|
], |
|
154
|
|
|
] |
|
155
|
|
|
]); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function confirmUnbind() |
|
160
|
|
|
{ |
|
161
|
|
|
$accountModel = new AccountModel(); |
|
162
|
|
|
$user_id = Auth::user()->id; |
|
163
|
|
|
if($accountModel->getExtra($user_id ,'github_id')){ |
|
164
|
|
|
$accountModel->setExtra($user_id,'github_id',null); |
|
165
|
|
|
$accountModel->setExtra($user_id,'github_email',null); |
|
166
|
|
|
$accountModel->setExtra($user_id,'github_nickname',null); |
|
167
|
|
|
$accountModel->setExtra($user_id,'github_homepage',null); |
|
168
|
|
|
$accountModel->setExtra($user_id,'github_token',null); |
|
169
|
|
|
return view('oauth.index',[ |
|
170
|
|
|
'page_title'=>"OAuth", |
|
171
|
|
|
'site_title'=>"NOJ", |
|
172
|
|
|
'navigation'=>"OAuth", |
|
173
|
|
|
'platform' => 'Github', |
|
174
|
|
|
'display_html' => 'You have successfully unbound your Github account from your NOJ account', |
|
175
|
|
|
'buttons' => [ |
|
176
|
|
|
[ |
|
177
|
|
|
'text' => 'home', |
|
178
|
|
|
'href' => route('home'), |
|
179
|
|
|
], |
|
180
|
|
|
] |
|
181
|
|
|
]); |
|
182
|
|
|
}else{ |
|
183
|
|
|
return view('oauth.index',[ |
|
184
|
|
|
'page_title'=>"OAuth", |
|
185
|
|
|
'site_title'=>"NOJ", |
|
186
|
|
|
'navigation'=>"OAuth", |
|
187
|
|
|
'platform' => 'Github', |
|
188
|
|
|
'display_html' => 'You\'re not tied to github', |
|
189
|
|
|
'buttons' => [ |
|
190
|
|
|
[ |
|
191
|
|
|
'text' => 'home', |
|
192
|
|
|
'href' => route('home'), |
|
193
|
|
|
], |
|
194
|
|
|
] |
|
195
|
|
|
]); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|