Example #1
0
 /**
  * Attach a provider to a user
  *
  * @param  User   $user
  * @param  array  $provider_details ['id'=>..., 'token'=>...]
  *
  * @return User
  */
 public function attachProvider(User $user, array $provider_details)
 {
     $user->flynsarmy_sociallogin_providers()->where('provider_id', $provider_details['provider_id'])->delete();
     $provider = new Provider($provider_details);
     $provider->user = $user;
     $provider->save();
     return $user;
 }
Example #2
0
 /**
  * Gets a code for when the user is persisted to a cookie or session which identifies the user.
  * @return string
  */
 public function getPersistCode()
 {
     // Option A: @todo config
     // return parent::getPersistCode();
     // Option B:
     if (!$this->persist_code) {
         return parent::getPersistCode();
     }
     return $this->persist_code;
 }
Example #3
0
 /**
  * After delete event
  * @return void
  */
 public function afterDelete()
 {
     if ($this->isSoftDelete()) {
         Event::fire('rainlab.user.deactivate', [$this]);
         return;
     }
     $this->avatar && $this->avatar->delete();
     parent::afterDelete();
 }
Example #4
0
 /**
  * Sends the confirmation email to a user, after activating
  * @param  string $code
  * @return void
  */
 public function attemptActivation($code)
 {
     $result = parent::attemptActivation($code);
     if ($result === false) {
         return false;
     }
     if (!($mailTemplate = UserSettings::get('welcome_template'))) {
         return;
     }
     $data = ['name' => $this->name, 'email' => $this->email];
     Mail::send($mailTemplate, $data, function ($message) {
         $message->to($this->email, $this->name);
     });
 }
Example #5
0
 public static function create_gp_access_token($code = '')
 {
     if (!$code) {
         return false;
     }
     if (empty(self::$gp_client)) {
         self::init_gp();
     }
     self::$gp_client->authenticate($code);
     $access_token = self::$gp_client->getAccessToken();
     // Get current user with access token
     $plus = new \Google_Service_Plus(self::$gp_client);
     $me = $plus->people->get("me");
     try {
         DB::beginTransaction();
         // Create new user with email if not exist in db
         if (!empty($me['emails']) && !empty($me['emails'][0])) {
             $email = $me['emails'][0]->getValue();
             $user = User::where('email', '=', $email)->first();
             if (!$user) {
                 $password = str_random(8);
                 $user = new User();
                 $user->email = $email;
                 $user->name = $me['displayName'];
                 $user->password = $password;
                 $user->password_confirmation = $password;
                 $user->is_activated = true;
                 $user->username = $email;
             }
             $user->gp_uid = $me['id'];
             $user->save();
             UserAccessToken::sendEmailAfterRegister($user->name, $user->email);
             // Save user avatar
             if (!empty($me['image']) && !empty($me['image']['url'])) {
                 $image_url = $me['image']['url'];
                 $image_url = str_replace('?sz=50', '?sz=250', $image_url);
                 self::getUserAvatar($user->id, $image_url);
             }
             // Save to UserAccessToken table
             $record = self::whereRaw('user_id = ? AND type = ?', array($user->id, 'googleplus'))->first();
             if (!$record) {
                 $record = new self();
                 $record->user_id = $user->id;
             }
             $record->access_token = $access_token;
             $record->type = 'googleplus';
             $record->save();
             Auth::login($user);
         }
     } catch (Exception $ex) {
         DB::rollback();
     }
     DB::commit();
     return $access_token;
 }
Example #6
0
 /**
  * Sends the confirmation email to a user, after activating.
  * @param  string $code
  * @return void
  */
 public function attemptActivation($code)
 {
     $result = parent::attemptActivation($code);
     if ($result === false) {
         return false;
     }
     if ($mailTemplate = UserSettings::get('welcome_template')) {
         Mail::sendTo($this, $mailTemplate, ['name' => $this->name, 'email' => $this->email]);
     }
     return true;
 }
Example #7
0
 public function getCallbackFB()
 {
     $data = get();
     if (!empty($data['code'])) {
         $app_id = UserAccessToken::$app_id;
         $app_secret = UserAccessToken::$app_secret;
         $redirect_uri = OCT_ROOT . '/api/v1/callback_fb';
         try {
             $url = self::$graph . "oauth/access_token?code={$data['code']}&client_id={$app_id}&client_secret={$app_secret}&redirect_uri={$redirect_uri}";
             $data = @file_get_contents($url);
             parse_str($data);
             if ($access_token) {
                 $access_token = trim($access_token);
                 // Grant access_token
                 $url = self::$graph . "oauth/access_token?client_id={$app_id}&client_secret={$app_secret}&grant_type=fb_exchange_token&fb_exchange_token={$access_token}";
                 $data = @file_get_contents($url);
                 parse_str($data);
                 if (Auth::check()) {
                     $user = Auth::getUser();
                     $user_id = $user->id;
                     $user->save();
                     $obj = UserAccessToken::check_fb_access_token($access_token);
                     if ($obj->user_id) {
                         $user->fb_uid = $obj->user_id;
                         $fb_uid = $obj->user_id;
                         $user->save();
                         self::saveUserAccessToken($user_id, $access_token, 'facebook');
                     }
                 } else {
                     // Get current facebook user email
                     $obj = UserAccessToken::check_fb_access_token($access_token);
                     if ($obj) {
                         $fb_uid = $obj->user_id;
                         $fb_user = UserAccessToken::get_fb_user_infor($fb_uid, $access_token);
                         if (!empty($fb_user->email)) {
                             DB::beginTransaction();
                             try {
                                 // Check user exists in DB
                                 $user = User::where('email', '=', $fb_user->email)->first();
                                 if (!$user) {
                                     $password = str_random(8);
                                     $user = new User();
                                     $user->password = $password;
                                     $user->password_confirmation = $password;
                                     $user->is_activated = true;
                                     $user->email = $fb_user->email;
                                     $user->name = $fb_user->name;
                                     $user->username = $fb_user->email;
                                 }
                                 $user->fb_uid = $fb_user->id;
                                 $user->save();
                                 UserAccessToken::sendEmailAfterRegister($user->name, $user->email);
                                 self::saveUserAccessToken($user->id, $access_token, 'facebook');
                                 Auth::login($user);
                             } catch (Exception $e) {
                                 DB::rollback();
                                 throw $e;
                             }
                             DB::commit();
                         }
                     }
                 }
                 // Save user avatar
                 if ($fb_uid) {
                     $image_url = "http://graph.facebook.com/v2.2/{$fb_uid}/picture?type=large";
                     UserAccessToken::getUserAvatar($user->id, $image_url);
                 }
                 return Redirect::to(HelperClassified::redirect_return_url());
             }
         } catch (Exception $ex) {
             throw $ex;
             return Response::json(array('status' => 'error', 'message' => $ex->getMessage()), 500);
         }
     }
     return Response::json(array('status' => 'success'), 200);
 }
Example #8
0
 /**
  * After login event
  * @return void
  */
 public function afterLogin()
 {
     parent::afterLogin();
     Event::fire('backend.user.login', [$this]);
 }