示例#1
0
 public function saveSocial($data)
 {
     $social = new Social();
     $social->user_id = $data['social_id'];
     $social->provider = $data['provider'];
     $social->save();
 }
 public function getSocialHandle($provider)
 {
     $user = Socialite::driver($provider)->user();
     $socialUser = null;
     //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;
             $name = explode(' ', $user->name);
             $newSocialUser->first_name = $name[0];
             $newSocialUser->last_name = $name[1];
             $newSocialUser->save();
             $socialData = new Social();
             $socialData->social_id = $user->id;
             $socialData->provider = $provider;
             $newSocialUser->social()->save($socialData);
             // Add role
             $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()->route('user.home');
     }
     if ($this->auth->user()->hasRole('administrator')) {
         return redirect()->route('admin.home');
     }
     return \App::abort(500);
 }
 public function getSocialHandle($provider)
 {
     $user = Socialite::driver($provider)->user();
     $social_user = null;
     //CHECK IF USERS EMAIL ADDRESS IS ALREADY IN DATABASE
     $user_check = User::where('email', '=', $user->email)->first();
     if (!empty($user_check)) {
         $social_user = $user_check;
     } else {
         $same_social_id = Social::where('social_id', '=', $user->id)->where('provider', '=', $provider)->first();
         // CHECK IF NEW SOCIAL MEDIA USER
         if (empty($same_social_id)) {
             $new_social_user = new User();
             $new_social_user->email = $user->email;
             $name = explode(' ', $user->name);
             if ($user->email) {
                 $new_social_user->name = $user->email;
             } else {
                 $new_social_user->name = $name[0];
             }
             $new_social_user->first_name = $name[0];
             // CHECK FOR LAST NAME
             if (isset($name[1])) {
                 $new_social_user->last_name = $name[1];
             }
             $new_social_user->active = '1';
             $the_activation_code = str_random(60) . $user->email;
             $new_social_user->activation_code = $the_activation_code;
             // GET IP ADDRESS
             $userIpAddress = new CaptureIp();
             $new_social_user->signup_sm_ip_address = $userIpAddress->getClientIp();
             // SAVE THE USER
             $new_social_user->save();
             // GET SOCIAL MEDIA LOGIN DATA
             $social_data = new Social();
             $social_data->social_id = $user->id;
             $social_data->provider = $provider;
             $new_social_user->social()->save($social_data);
             // GET GRAVATAR
             $new_social_user->gravatar = Gravatar::get($user->email);
             // ADD ROLE
             $role = Role::whereName('user')->first();
             $new_social_user->assignRole($role);
             $social_user = $new_social_user;
             // LINK TO PROFILE TABLE
             $profile = new Profile();
             $social_user->profile()->save($profile);
         } else {
             //Load this existing social user
             $social_user = $same_social_id->user;
         }
     }
     $this->auth->login($social_user, true);
     if ($this->auth->user()->hasRole('user')) {
         //return redirect()->route('user.home');
         return redirect('dashboard');
     }
     if ($this->auth->user()->hasRole('administrator')) {
         return redirect('dashboard');
         //return redirect()->route('admin.home');
     }
     return \App::abort(500);
 }
示例#4
0
文件: User.php 项目: pyvil/yii-app
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSocial()
 {
     return $this->hasMany(Social::className(), ['id_user' => 'id']);
 }
示例#5
0
 /**
  * Provider used the "callback URL" and now we process the returned information
  */
 public function getSocialHandle(Request $request, $provider, AppMailer $mailer)
 {
     Log::info($request->ip() . ' - getSocialHandle - User gave consent to register using ' . $provider);
     $social = Socialite::driver($provider);
     $user = $social->user();
     $code = Input::get('code');
     if (!$code) {
         return redirect('login')->with('status', 'danger')->with('message', 'You did not share your profile data with c-SPOT.');
     }
     if (!$user->email) {
         return redirect('login')->with('status', 'danger')->with('message', 'You did not share your email with c-SPOT. You need to visit your ' . $provider . ' App Settings and remove c-SPOT, than you can come back here and login again. Or you can create a new account.');
     }
     $socialUser = null;
     //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)) {
             // As there is no combination of this social id and provider,
             // we create a new one
             $newSocialUser = new User();
             // the email address as provided by the service provider
             $newSocialUser->email = $user->email;
             // perhaps the email contains a name?
             $emailName = explode('@', $user->email)[0];
             if (strlen($user->name) < 3) {
                 $user->name = str_replace('.', ' ', $emailName);
             }
             // the name is hopefully a full name with first- and lastname
             $name = explode(' ', $user->name);
             $newSocialUser->first_name = $name[0];
             $newSocialUser->last_name = count($name) > 1 ? $name[1] : $name[0];
             // save the new user
             $newSocialUser->save();
             // Add role
             $role = Role::whereName('user')->first();
             $newSocialUser->assignRole($role);
             // create record in the social table
             $socialData = new Social();
             $socialData->social_id = $user->id;
             $socialData->provider = $provider;
             $newSocialUser->social()->save($socialData);
             $socialUser = $newSocialUser;
         } else {
             //Load this existing social user
             $socialUser = $sameSocialId->user;
         }
     }
     Log::info($request->ip() . ' - getSocialHandle - trying to do social-sign in');
     $mailer->notifyAdmin($socialUser, 'User confirmed via ' . $provider);
     // $this->auth->login($socialUser, true);
     Auth::login($socialUser, true);
     // write last login field in users table
     Auth::user()->update(['last_login' => Carbon::now()]);
     return redirect()->intended($this->redirectPath());
 }
 public function getSocialHandle($provider)
 {
     $user = Socialite::driver($provider)->user();
     $code = Input::get('code');
     if (!$code) {
         return redirect()->route('auth.login')->with('status', 'danger')->with('message', 'You did not share your profile data with our socail app.');
     }
     if (!$user->email) {
         return redirect()->route('auth.login')->with('status', 'danger')->with('message', 'You did not share your email with our social app. You need to visit App Settings and remove our app, than you can come back here and login again. Or you can create new account.');
     }
     $socialUser = null;
     //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;
             $name = explode(' ', $user->name);
             $newSocialUser->first_name = $name[0];
             $newSocialUser->last_name = $name[1];
             $newSocialUser->save();
             $socialData = new Social();
             $socialData->social_id = $user->id;
             $socialData->provider = $provider;
             $newSocialUser->social()->save($socialData);
             // Add role
             $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()->route('user.home');
     }
     if ($this->auth->user()->hasRole('administrator')) {
         return redirect()->route('admin.home');
     }
     return \App::abort(500);
 }