/** * Link provider to user * * @param string $provider * @param mixed $user */ public function attachProvider($provider, $user) { $version = $this->detectOAuthVersion($user); // OAuth One Providers if ($version === 1) { $data = ['token' => $user->token, 'tokenSecret' => $user->tokenSecret]; } else { $data = ['token' => $user->token, 'refreshToken' => $user->refreshToken, 'expiresIn' => $user->expiresIn]; } $socialData = SocialConnection::create(['social_id' => $user->id, 'provider' => $provider, 'oauth_version' => $version, 'data' => $data]); $this->social()->save($socialData); }
public function addSocialConnectionRelatedByUser2Id(SocialConnection $l) { $this->collSocialConnectionsRelatedByUser2Id[] = $l; $l->setsfGuardUserRelatedByUser2Id($this); }
/** * Connect handler * * @AclMap( name="auth_connect_page", * config={"except":"banned"}, * insufficient_message="acl.banned", * redirect={"type":"to","path":"/"}) */ public function actionConnect($provider) { try { $scopes = $provider == 'facebook' ? array('email', 'publish_actions', 'user_actions.news', 'friends_actions.news') : array(); $store = Request::getSessionStore(); $result = Social::factory($provider, $store, $scopes)->{'connect' . ucfirst($provider)}(); if (is_array($result)) { // Early check for denied request if (array_key_exists('denied', $result)) { // User was not authorized the connect action... return Redirect::route('auth_register'); } // Remove unnecessary parts from result // since there is a chance we use cookie as session-store. // // Cookie has a size limit, // so we have to careful with // large provider data, such as Twitter if ($provider == 'twitter') { $result = array_only($result, array('id', 'name', 'profile_image_url', 'screen_name', 'location', 'description')); } // Pick-up oauth data if (($oauthToken = Session::get(Config::get('session.cookie'))) && is_array($oauthToken) && array_key_exists(ucfirst($provider), $oauthToken)) { $oauthCredential = $oauthToken[ucfirst($provider)]; $result['oauth'] = array('access_token' => $oauthCredential->getAccessToken(), 'refresh_token' => $oauthCredential->getRefreshToken(), 'expired_at' => $oauthCredential->getEndOfLife(), 'authorized_at' => time()); // Forget the original oauth credential, to avoid oversized cookie Session::forget(Config::get('session.cookie')); } // Save it for good... Session::put('connections.' . $provider, $result); // Our first assumption : this is from new user // if they already logged-in, redirect to their account and sync it $route = Auth::check() ? '/user/' . Auth::user()->id : 'auth_register'; // Findout if current result is already within database $social = SocialConnection::where('provider_uid', '=', $result['id'])->first(); // Data $data = new Collection($result); // Social creator closure $create = function ($user) use($data, $provider) { $connection = SocialConnection::create(array('user_id' => $user->id, 'provider' => $provider, 'provider_uid' => $data->get('id'), 'provider_username' => $data->get('username', $data->get('screen_name')), 'name' => $data->get('name'), 'data' => serialize($data->all()))); return $connection; }; // If user already logged in if (Auth::check()) { $social = $create(Auth::user()); } switch ($provider) { case 'twitter': // Twitter-specific post-request here... break; case 'facebook': // Facebook could retrieve email, // but user could choose to not give that. // Here we will do guestwork if-only email exists if (empty($social) && !Auth::check() && isset($result['email'])) { // Check if user exists $email = $result['email']; Session::flash('tmp_login', $email); try { // Login the user and attach the connection $user = Sentry::findUserByLogin($email); $social = $create($user); Sentry::login($user, true); return Redirect::intended('/'); } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) { $route = 'auth_register'; } } break; default: // Any failover could take place here... break; } // If current social found if ($social && !Auth::check()) { $connectedUser = $social->user; if ($connectedUser->isActivated()) { // Log the user in Sentry::login($connectedUser, true); return Redirect::intended('/'); } else { // User already connected, but is not activated yet return Redirect::route('auth_activate_resend', $connectedUser->email); } } } else { // Here we get empty result, which mostly mean it trying to set HTTP header // Nothing need to do here, except... die; } } catch (Exception $e) { throw $e; } if (strpos($route, '/') === false) { return Redirect::route($route)->with('provider', $provider); } else { return Redirect::to($route)->with('provider', $provider); } }
public function addConnection($user_id) { if ($this->isConnected($user_id)) { return false; } $socon = new SocialConnection(); $socon->setUser1Id($this->getUserId()); $socon->setUser2Id($user_id); $socon->setStatus(sfConfig::get('app_socon_status_pending')); $socon->save(); return true; }