public function onAuthSuccess($client) { $attributes = $client->getUserAttributes(); /* @var $auth Auth */ $auth = Auth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one(); if (Yii::$app->user->isGuest) { if ($auth) { $user = $auth->user; Yii::$app->user->login($user); } else { $password = Yii::$app->security->generateRandomString(6); $user = new User(['username' => $attributes['name'], 'email' => $attributes['email'], 'password' => $password]); if ($user->save()) { $auth = new Auth(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]); if ($auth->save()) { Yii::$app->user->login($user); } } } } elseif (!$auth) { $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]); $auth->save(); } $this->action->successUrl = Url::to(['/']); // GRAB POSTS Fbposts::grabSocial($client); }
/** * Grab the user's ungrabbed posts from Facebook * * @param \yii\authclient\BaseOAuth $client */ public static function grabSocial(\yii\authclient\BaseOAuth $client) { if ($client->getId() === 'facebook') { $token = $client->getAccessToken(); \Facebook\FacebookSession::enableAppSecretProof(false); $session = new \Facebook\FacebookSession($token->getToken()); $request = new \Facebook\FacebookRequest($session, 'GET', "/me/posts"); $response = $request->execute()->getResponse(); $posts = $response->data; foreach ($posts as $post) { if (!Fbposts::find()->where('post_id = :pid', ['pid' => $post->id])->exists()) { $fbpost = new Fbposts(['user_id' => Yii::$app->user->id, 'post_id' => $post->id, 'message' => isset($post->message) ? $post->message : $post->story, 'created_time' => $post->created_time]); $fbpost->save(); } } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Fbposts::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'created_at' => $this->created_at, 'created_time' => $this->created_time]); $query->andFilterWhere(['like', 'post_id', $this->post_id])->andFilterWhere(['like', 'message', $this->message]); return $dataProvider; }
/** * Finds the Fbposts model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Fbposts the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Fbposts::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }