|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Integrations\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Auth; |
|
6
|
|
|
use Socialite; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use App\Models\User; |
|
9
|
|
|
use Flash; |
|
10
|
|
|
|
|
11
|
|
|
trait SocialiteHelper |
|
12
|
|
|
{ |
|
13
|
|
|
protected $oauthDrivers = [ |
|
14
|
|
|
'github' => 'github', |
|
15
|
|
|
'facebook' => 'facebook', |
|
16
|
|
|
'linkedin' => 'linkedin', |
|
17
|
|
|
'twitter' => 'twitter', |
|
18
|
|
|
'google' => 'google', |
|
19
|
|
|
'wechat' => 'weixin' |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
public function oauth(Request $request) |
|
23
|
|
|
{ |
|
24
|
|
|
$driver = $request->input('driver'); |
|
25
|
|
|
$driver = !isset($this->oauthDrivers[$driver]) ? 'github' : $this->oauthDrivers[$driver]; |
|
26
|
|
|
|
|
27
|
|
|
if (Auth::check() && Auth::user()->register_source == $driver) { |
|
28
|
|
|
return redirect('/'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return Socialite::driver($driver)->redirect(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getUserFromSocialite($driver) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($driver!=='twitter') { |
|
37
|
|
|
return Socialite::with($this->oauthDrivers[$driver])->stateless()->user(); |
|
38
|
|
|
} |
|
39
|
|
|
return Socialite::with($this->oauthDrivers[$driver]) |
|
40
|
|
|
->userFromTokenAndSecret( |
|
41
|
|
|
getenv('TWITTER_CLIENT_ID'), |
|
42
|
|
|
getenv('TWITTER_CLIENT_SECRET') |
|
43
|
|
|
)->user(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function callback(Request $request) |
|
47
|
|
|
{ |
|
48
|
|
|
$driver = $request->input('driver'); |
|
49
|
|
|
|
|
50
|
|
|
if (!isset($this->oauthDrivers[$driver]) |
|
51
|
|
|
|| (Auth::check() && Auth::user()->register_source == $driver) |
|
52
|
|
|
) { |
|
53
|
|
|
return redirect()->intended('/'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$oauthUser = $this->getUserFromSocialite($driver); |
|
57
|
|
|
$user = User::getByDriver($driver, $oauthUser->id); |
|
58
|
|
|
|
|
59
|
|
|
if (Auth::check()) { |
|
60
|
|
|
if ($user && $user->id != Auth::id()) { |
|
61
|
|
|
Flash::error(lang('Sorry, this socialite account has been registed.', ['driver' => lang($driver)])); |
|
62
|
|
|
} else { |
|
63
|
|
|
$this->bindSocialiteUser($oauthUser, $driver); |
|
64
|
|
|
Flash::success(lang('Bind Successfully!', ['driver' => lang($driver)])); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return redirect(route('users.edit_social_binding', Auth::id())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($user) { |
|
71
|
|
|
return $this->loginUser($user); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->userNotFound($driver, $oauthUser); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function bindSocialiteUser($oauthUser, $driver) |
|
78
|
|
|
{ |
|
79
|
|
|
$currentUser = Auth::user(); |
|
80
|
|
|
|
|
81
|
|
|
if ($driver == 'github') { |
|
82
|
|
|
$currentUser->github_id = $oauthUser->id; |
|
83
|
|
|
$currentUser->github_url = $oauthUser->user['url']; |
|
84
|
|
|
} elseif ($driver == 'linkedin') { |
|
85
|
|
|
$currentUser->linkedin_id = $oauthUser->id; |
|
86
|
|
|
} elseif ($driver == 'twitter') { |
|
87
|
|
|
$currentUser->twitter_id = $oauthUser->id; |
|
88
|
|
|
} elseif ($driver == 'google') { |
|
89
|
|
|
$currentUser->facebook_id = $oauthUser->id; |
|
90
|
|
|
} elseif ($driver == 'facebook') { |
|
91
|
|
|
$currentUser->facebook_id = $oauthUser->id; |
|
92
|
|
|
} elseif ($driver == 'wechat') { |
|
93
|
|
|
$currentUser->wechat_openid = $oauthUser->id; |
|
94
|
|
|
$currentUser->wechat_unionid = $oauthUser->user['unionid']; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$currentUser->save(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.