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\Support\Facades\Config; |
12
|
|
|
use Illuminate\Support\Facades\Input; |
13
|
|
|
use jeremykenedy\LaravelRoles\Models\Role; |
14
|
|
|
use Laravel\Socialite\Facades\Socialite; |
15
|
|
|
|
16
|
|
|
use Illuminate\Support\Facades\Log; |
17
|
|
|
|
18
|
|
|
class SocialController extends Controller |
19
|
|
|
{ |
20
|
|
|
use ActivationTrait; |
|
|
|
|
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) |
49
|
|
|
{ |
50
|
|
|
if (Input::get('denied') != '') { |
51
|
|
|
return redirect()->to('login') |
|
|
|
|
52
|
|
|
->with('status', 'danger') |
53
|
|
|
->with('message', trans('socials.denied')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$socialUserObject = Socialite::driver($provider)->user(); |
57
|
|
|
|
58
|
|
|
$socialUser = null; |
59
|
|
|
|
60
|
|
|
// Check if email is already registered |
61
|
|
|
$userCheck = User::where('email', '=', $socialUserObject->email)->first(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$email = $socialUserObject->email; |
64
|
|
|
|
65
|
|
|
if (!$socialUserObject->email) { |
66
|
|
|
$email = 'missing'.str_random(10).'@'.str_random(10).'.example.org'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (empty($userCheck)) { |
70
|
|
|
$sameSocialId = Social::where('social_id', '=', $socialUserObject->id) |
|
|
|
|
71
|
|
|
->where('provider', '=', $provider) |
72
|
|
|
->first(); |
73
|
|
|
|
74
|
|
|
if (empty($sameSocialId)) { |
75
|
|
|
$ipAddress = new CaptureIpTrait(); |
76
|
|
|
$socialData = new Social(); |
77
|
|
|
$profile = new Profile(); |
78
|
|
|
$role = Role::where('slug', '=', 'user')->first(); |
79
|
|
|
$fullname = explode(' ', $socialUserObject->name); |
|
|
|
|
80
|
|
|
if (count($fullname) == 1) { |
81
|
|
|
$fullname[1] = ''; |
82
|
|
|
} |
83
|
|
|
$username = $socialUserObject->nickname; |
|
|
|
|
84
|
|
|
|
85
|
|
|
if ($username == null) { |
86
|
|
|
foreach ($fullname as $name) { |
87
|
|
|
$username .= $name; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Check to make sure username does not already exist in DB before recording |
92
|
|
|
$username = $this->checkUserName($username, $email); |
93
|
|
|
|
94
|
|
|
$user = User::create([ |
95
|
|
|
'name' => $username, |
96
|
|
|
'first_name' => $fullname[0], |
97
|
|
|
'last_name' => $fullname[1], |
98
|
|
|
'email' => $email, |
99
|
|
|
'password' => bcrypt(str_random(40)), |
100
|
|
|
'token' => str_random(64), |
101
|
|
|
'activated' => true, |
102
|
|
|
'signup_sm_ip_address' => $ipAddress->getClientIp(), |
103
|
|
|
|
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
$socialData->social_id = $socialUserObject->id; |
107
|
|
|
$socialData->provider = $provider; |
108
|
|
|
$user->social()->save($socialData); |
109
|
|
|
$user->attachRole($role); |
110
|
|
|
$user->activated = true; |
111
|
|
|
|
112
|
|
|
$user->profile()->save($profile); |
113
|
|
|
$user->save(); |
114
|
|
|
|
115
|
|
|
if ($socialData->provider == 'github') { |
116
|
|
|
$user->profile->github_username = $socialUserObject->nickname; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Twitter User Object details: https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object |
120
|
|
|
if ($socialData->provider == 'twitter') { |
121
|
|
|
$user->profile()->twitter_username = $socialUserObject->screen_name; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
$user->profile->save(); |
124
|
|
|
|
125
|
|
|
$socialUser = $user; |
126
|
|
|
} else { |
127
|
|
|
$socialUser = $sameSocialId->user; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
auth()->login($socialUser, true); |
131
|
|
|
|
132
|
|
|
return redirect('home')->with('success', trans('socials.registerSuccess')); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$socialUser = $userCheck; |
136
|
|
|
|
137
|
|
|
auth()->login($socialUser, true); |
138
|
|
|
|
139
|
|
|
return redirect('home'); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check if username against database and return valid username. |
144
|
|
|
* If username is not in the DB return the username |
145
|
|
|
* else generate, check, and return the username. |
146
|
|
|
* |
147
|
|
|
* @param string $username |
148
|
|
|
* @param string $email |
149
|
|
|
* |
150
|
|
|
* @return string || re-run method |
|
|
|
|
151
|
|
|
*/ |
152
|
|
|
public function checkUserName($username, $email) |
153
|
|
|
{ |
154
|
|
|
$userNameCheck = User::where('name', '=', $username)->first(); |
155
|
|
|
|
156
|
|
|
if ($userNameCheck) { |
157
|
|
|
$i = 1; |
|
|
|
|
158
|
|
|
do { |
159
|
|
|
$username = $this->generateUserName($username, $email); |
160
|
|
|
$newCheck = User::where('name', '=', $username)->first(); |
161
|
|
|
|
162
|
|
|
if ($newCheck == null) { |
163
|
|
|
$newCheck = 0; |
164
|
|
|
} else { |
165
|
|
|
$newCheck = count($newCheck); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} while ($newCheck != 0); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $username; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Generate Username |
176
|
|
|
* |
177
|
|
|
* @param string $username |
178
|
|
|
* @param string $email |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function generateUserName($username, $email) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
return $username . '_' . str_random(10); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
} |
188
|
|
|
|