1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use App\Models\Profile; |
7
|
|
|
use App\Models\Social; |
8
|
|
|
use App\Models\User; |
9
|
|
|
use App\Traits\ActivationTrait; |
10
|
|
|
use App\Traits\CaptureIpTrait; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Illuminate\Support\Facades\Config; |
13
|
|
|
use jeremykenedy\LaravelRoles\Models\Role; |
14
|
|
|
use Laravel\Socialite\Facades\Socialite; |
15
|
|
|
|
16
|
|
|
class SocialController extends Controller |
17
|
|
|
{ |
18
|
|
|
use ActivationTrait; |
|
|
|
|
19
|
|
|
|
20
|
|
|
private $redirectSuccessLogin = 'home'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Gets the social redirect. |
24
|
|
|
* |
25
|
|
|
* @param string $provider The provider |
26
|
|
|
* |
27
|
|
|
* @return Redirect |
28
|
|
|
*/ |
29
|
|
|
public function getSocialRedirect($provider) |
30
|
|
|
{ |
31
|
|
|
$providerKey = Config::get('services.'.$provider); |
32
|
|
|
|
33
|
|
|
if (empty($providerKey)) { |
34
|
|
|
return view('pages.status') |
|
|
|
|
35
|
|
|
->with('error', trans('socials.noProvider')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return Socialite::driver($provider)->redirect(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets the social handle. |
43
|
|
|
* |
44
|
|
|
* @param string $provider The provider |
45
|
|
|
* |
46
|
|
|
* @return Redirect |
47
|
|
|
*/ |
48
|
|
|
public function getSocialHandle($provider, Request $request) |
49
|
|
|
{ |
50
|
|
|
$denied = $request->denied ? $request->denied : null; |
51
|
|
|
$socialUser = null; |
52
|
|
|
|
53
|
|
|
if ($denied != null || $denied != '') { |
54
|
|
|
return redirect()->to('login') |
|
|
|
|
55
|
|
|
->with('status', 'danger') |
56
|
|
|
->with('message', trans('socials.denied')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$socialUserObject = Socialite::driver($provider)->user(); |
60
|
|
|
|
61
|
|
|
// Check if email is already registered |
62
|
|
|
$userCheck = User::where('email', '=', $socialUserObject->email)->first(); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$email = $socialUserObject->email; |
65
|
|
|
|
66
|
|
|
if (! $socialUserObject->email) { |
67
|
|
|
$email = 'missing'.str_random(10).'@'.str_random(10).'.example.org'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// If user is not registered |
71
|
|
|
if (empty($userCheck)) { |
72
|
|
|
$sameSocialId = Social::where('social_id', '=', $socialUserObject->id) |
|
|
|
|
73
|
|
|
->where('provider', '=', $provider) |
74
|
|
|
->first(); |
75
|
|
|
|
76
|
|
|
if (empty($sameSocialId)) { |
77
|
|
|
$ipAddress = new CaptureIpTrait(); |
78
|
|
|
$socialData = new Social(); |
79
|
|
|
$profile = new Profile(); |
80
|
|
|
$role = Role::where('slug', '=', 'user')->first(); |
81
|
|
|
$fullname = explode(' ', $socialUserObject->name); |
|
|
|
|
82
|
|
|
if (count($fullname) == 1) { |
83
|
|
|
$fullname[1] = ''; |
84
|
|
|
} |
85
|
|
|
$username = $socialUserObject->nickname; |
|
|
|
|
86
|
|
|
|
87
|
|
|
if ($username == null) { |
88
|
|
|
foreach ($fullname as $name) { |
89
|
|
|
$username .= $name; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Check to make sure username does not already exist in DB before recording |
94
|
|
|
$username = $this->checkUserName($username, $email); |
95
|
|
|
|
96
|
|
|
$user = User::create([ |
97
|
|
|
'name' => $username, |
98
|
|
|
'first_name' => $fullname[0], |
99
|
|
|
'last_name' => $fullname[1], |
100
|
|
|
'email' => $email, |
101
|
|
|
'password' => bcrypt(str_random(40)), |
102
|
|
|
'token' => str_random(64), |
103
|
|
|
'activated' => true, |
104
|
|
|
'signup_sm_ip_address' => $ipAddress->getClientIp(), |
105
|
|
|
|
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$socialData->social_id = $socialUserObject->id; |
109
|
|
|
$socialData->provider = $provider; |
110
|
|
|
$user->social()->save($socialData); |
111
|
|
|
$user->attachRole($role); |
112
|
|
|
$user->activated = true; |
113
|
|
|
|
114
|
|
|
$user->profile()->save($profile); |
115
|
|
|
$user->save(); |
116
|
|
|
|
117
|
|
|
if ($socialData->provider == 'github') { |
118
|
|
|
$user->profile->github_username = $socialUserObject->nickname; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Twitter User Object details: https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object |
122
|
|
|
if ($socialData->provider == 'twitter') { |
123
|
|
|
//$user->profile()->twitter_username = $socialUserObject->screen_name; |
124
|
|
|
//If the above fails try (The documentation shows screen_name however so Twitters docs may be out of date.): |
125
|
|
|
$user->profile()->twitter_username = $socialUserObject->nickname; |
126
|
|
|
} |
127
|
|
|
$user->profile->save(); |
128
|
|
|
|
129
|
|
|
$socialUser = $user; |
130
|
|
|
} else { |
131
|
|
|
$socialUser = $sameSocialId->user; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
auth()->login($socialUser, true); |
135
|
|
|
|
136
|
|
|
return redirect($this->redirectSuccessLogin)->with('success', trans('socials.registerSuccess')); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$socialUser = $userCheck; |
140
|
|
|
|
141
|
|
|
auth()->login($socialUser, true); |
142
|
|
|
|
143
|
|
|
return redirect($this->redirectSuccessLogin); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Check if username against database and return valid username. |
148
|
|
|
* If username is not in the DB return the username |
149
|
|
|
* else generate, check, and return the username. |
150
|
|
|
* |
151
|
|
|
* @param string $username |
152
|
|
|
* @param string $email |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function checkUserName($username, $email) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$userNameCheck = User::where('name', '=', $username)->first(); |
159
|
|
|
|
160
|
|
|
if ($userNameCheck) { |
161
|
|
|
$i = 1; |
|
|
|
|
162
|
|
|
do { |
163
|
|
|
$username = $this->generateUserName($username); |
164
|
|
|
$newCheck = User::where('name', '=', $username)->first(); |
165
|
|
|
|
166
|
|
|
if ($newCheck == null) { |
167
|
|
|
$newCheck = 0; |
168
|
|
|
} else { |
169
|
|
|
$newCheck = count($newCheck); |
170
|
|
|
} |
171
|
|
|
} while ($newCheck != 0); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $username; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Generate Username. |
179
|
|
|
* |
180
|
|
|
* @param string $username |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function generateUserName($username) |
185
|
|
|
{ |
186
|
|
|
return $username.'_'.str_random(10); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|