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