示例#1
0
文件: User.php 项目: lmkhang/mcntw
 /**
  * @author: lmkhang - skype
  * @date: 2016-01-05
  * Getting and Processing registration from GG API
  *
  */
 public function callback_google()
 {
     //Check isLogged
     if ($this->isLogged()) {
         die;
     }
     $session = new \Symfony\Component\HttpFoundation\Session\Session();
     //get Info of Google's API
     $google['api_key'] = \App\Config::where(['prefix' => 'google', 'name' => 'api_key', 'del_flg' => 1])->get()[0]['value'];
     $google['client_id'] = \App\Config::where(['prefix' => 'google', 'name' => 'client_id', 'del_flg' => 1])->get()[0]['value'];
     $google['client_secret'] = \App\Config::where(['prefix' => 'google', 'name' => 'client_secret', 'del_flg' => 1])->get()[0]['value'];
     $google['scope'] = \App\Config::where(['prefix' => 'google', 'name' => 'scope', 'del_flg' => 1])->get()[0]['value'];
     $google['url_callback'] = \App\Config::where(['prefix' => 'google', 'name' => 'url_callback', 'del_flg' => 1])->get()[0]['value'];
     //Create Client Request to access Google API
     $client = new \Google_Client();
     $client->setApplicationName("Media Center Network");
     $client->setClientId($google['client_id']);
     $client->setClientSecret($google['client_secret']);
     $client->setRedirectUri(config('app.url') . $google['url_callback']);
     $client->setDeveloperKey($google['api_key']);
     $client->addScope(explode(',', $google['scope']));
     //Send Client Request
     $objOAuthService = new \Google_Service_Oauth2($client);
     //Authenticate code from Google OAuth Flow
     //Add Access Token to Session
     if (isset($_GET['code'])) {
         $client->authenticate($_GET['code']);
         //set Facebook Login SESSION
         $session->set('google_access_token', $client->getAccessToken());
         return redirect(filter_var(config('app.url') . $google['url_callback'], FILTER_SANITIZE_URL));
     }
     //Set Access Token to make Request
     if ($session->has('google_access_token')) {
         $client->setAccessToken($session->get('google_access_token'));
     }
     //Get User Data from Google Plus
     //If New, Insert to Database
     if ($client->getAccessToken()) {
         $userData = $objOAuthService->userinfo->get();
         $user_get = get_object_vars($userData);
         if ($user_get && is_array($user_get)) {
             //Check existed account
             $registration_system = config('constant.registration');
             $user = $this->checkAccountSNS(['username' => $user_get['id']], $registration_system['google']);
             if (!$user) {
                 //insert
                 $match = new Libraries\Math();
                 $register['refer'] = $match->to_base(rand(10, 30) . substr(time(), 5, 10) . rand(10, 30), 62) . $match->to_base(rand(100, 300) . substr(time(), 5, 10) . rand(100, 300), 62) . $match->to_base(rand(100, 300) . substr(time(), 5, 10) . rand(100, 300), 62);
                 //from_refer
                 if ($this->hasFlash('refer')) {
                     $register['from_refer'] = $this->getFlash('refer');
                 } else {
                     $register['from_refer'] = 'no_refer';
                 }
                 $message_refer = $this->checkUserAttributes($register);
                 $user = new \App\User();
                 $user->refer = $register['refer'];
                 $user->from_refer = !$message_refer ? $register['from_refer'] : '';
                 $user->username = $user_get['id'];
                 $user->first_name = $user_get['givenName'];
                 $user->last_name = $user_get['familyName'];
                 $user->full_name = $user_get['name'];
                 $user->email = '';
                 //$user_get['email'];
                 $user->gavatar = $user_get['picture'];
                 //                    $user->country = strtoupper($user_get['locale']);
                 $user->del_flg = 1;
                 $user->registration_system = $registration_system['google'];
                 $user->save();
                 //Insert User Stats
                 $user_stats = new \App\UserStats();
                 $user_stats->user_id = $user->user_id;
                 $user_stats->total = 0;
                 $user_stats->del_flg = $user->del_flg;
                 $user_stats->save();
             }
             //Set Session
             $this->setLogSession(['email' => $user->email, 'user_id' => $user->user_id, 'registration_system' => $user->registration_system]);
             //set Flash Message
             $this->setFlash('message', 'Welcome to MCN!');
             //                return Redirect::intended('/')->with('message', 'Register successfully!');
         }
     }
     //        return Redirect::intended('/')->with('message', 'Back Home!');
     if (!$this->hasFlash('message')) {
         //set Flash Message
         $this->setFlash('message', 'Error!');
     }
     return redirect('/');
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public static function has($name)
 {
     return static::$instance->has($name);
 }