/** * Return user matching given social profile from database. * * @param $profile * @return User|null */ public function findUserByProfile($profile) { if (!$profile) { return; } //if there's no email returned from provider (twitter) we //will try to find the user using their unique profile id if (!$profile->email) { $oauth = Social::where('token', $profile->id)->first(); if ($oauth) { return $oauth->user; } } return User::where('email', $profile->email)->first(); }
public function getSocialHandle($provider) { $user = Socialite::driver($provider)->user(); $socialUser = null; //Check is this email belongs to OZU if (!preg_match('/((ozu|ozyegin)\\.edu(\\.[a-zA-Z]+)?|\\.ac\\.[a-zA-Z]+)$/i', $user->email)) { return redirect()->route('auth.login')->with('message', 'You can only login with your.name@(ozu|ozyegin).edu.tr')->with('status', 'danger')->withInput(); } //Check is this email present $userCheck = User::where('email', '=', $user->email)->first(); if (!empty($userCheck)) { $socialUser = $userCheck; } else { $sameSocialId = Social::where('social_id', '=', $user->id)->where('provider', '=', $provider)->first(); if (empty($sameSocialId)) { //There is no combination of this social id and provider, so create new one $newSocialUser = new User(); $newSocialUser->email = $user->email; $parts = explode("@", $user->email); $username = explode(".", $parts[0]); $alt_name = ucfirst($username[0]); $alt_last = ucfirst($username[1]); $name = explode(' ', $user->name); $newSocialUser->first_name = isset($name[0]) && !empty($name[0]) ? $name[0] : $alt_name; $newSocialUser->last_name = isset($name[2]) ? $name[2] : (isset($name[1]) ? $name[1] : $alt_last); $newSocialUser->save(); $socialData = new Social(); $socialData->social_id = $user->id; $socialData->provider = $provider; $newSocialUser->social()->save($socialData); // Add role if (in_array($user->email, $this->privilegedEmails)) { $role = Role::whereName('administrator')->first(); } else { $role = Role::whereName('user')->first(); } $newSocialUser->assignRole($role); $socialUser = $newSocialUser; } else { //Load this existing social user $socialUser = $sameSocialId->user; } } $this->auth->login($socialUser, true); if ($this->auth->user()->hasRole('user')) { return redirect('/'); } if ($this->auth->user()->hasRole('administrator')) { return redirect('/'); } if ($this->auth->user()->hasRole('super')) { return redirect('/'); } return App::abort(500); }
public static function social($type, $data) { if ($type == 'id') { return Social::findOrFail($data); } return Social::where($type, $data)->first(); }