示例#1
0
 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);
 }
示例#2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Auth::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(['user_id' => $this->user_id]);
     $query->andFilterWhere(['like', 'id', $this->id]);
     return $dataProvider;
 }
 /**
  * @param ClientInterface $client
  */
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     $email = ArrayHelper::getValue($attributes, 'email');
     /** @var Auth $auth */
     $auth = Auth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // login
             $user = $auth->user;
             Yii::$app->user->login($user, 3600 * 24 * 30);
         } else {
             // signup
             if (User::find()->where(['email' => $email])->exists()) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]);
             } else {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new User(['username' => $attributes['login'], 'email' => $email, 'password' => $password]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $auth = new Auth(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user, 3600 * 24 * 30);
                     } else {
                         print_r($auth->getErrors());
                         die;
                     }
                 } else {
                     print_r($user->getErrors());
                     die;
                 }
             }
         }
     } else {
         // user already logged in
         if (!$auth) {
             // add auth provider
             $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
 }
示例#4
0
 public function beforeAction($action)
 {
     $moduleID = $action->controller->module->id;
     $controllerID = $action->controller->id;
     $actionID = $action->id;
     $user = \Yii::$app->user;
     $userID = $user->id;
     if (!in_array($controllerID, ['default', 'site'])) {
         $auth = \app\models\Auth::find()->where(['module' => $moduleID, 'controller' => $controllerID, 'action' => $actionID, 'user_id' => $userID])->count();
         if ($auth == 0) {
             if (!$action instanceof \yii\web\ErrorAction) {
                 if ($user->getIsGuest()) {
                     $user->loginRequired();
                 } else {
                     throw new \yii\web\ForbiddenHttpException('Anda tidak diizinkan untuk mengakses halaman ' . $action->id . ' ini!');
                 }
             }
         }
     }
     return true;
 }
示例#5
0
 public function bootstrap($app)
 {
     $app->on(\yii\base\Application::EVENT_BEFORE_ACTION, function ($event) {
         $action = $event->action;
         $moduleID = $action->controller->module->id;
         $controllerID = $action->controller->id;
         $actionID = $action->id;
         $user = \Yii::$app->user;
         $userID = $user->id;
         if (!in_array($controllerID, ['default', 'site'])) {
             $auth = \app\models\Auth::find()->where(['module' => $moduleID, 'controller' => $controllerID, 'action' => $actionID, 'user_id' => $userID])->count();
             if ($auth == 0) {
                 if (!$action instanceof \yii\web\ErrorAction) {
                     if ($user->getIsGuest()) {
                         $user->loginRequired();
                     } else {
                         throw new \yii\web\ForbiddenHttpException('Anda tidak diizinkan untuk mengakses halaman ' . $action->id . ' ini!');
                     }
                 }
             }
         }
     });
 }
示例#6
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $auth = Models\Auth::find($id);
     $auth->delete();
     return redirect(action('Admin\\AuthController@index'));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $authObj = new Auth();
     $data = $request->all();
     $validator = $authObj->operationValidation();
     unset($data['_token']);
     if ($validator->fails()) {
         return redirect()->back()->withErrors($validator->messages())->with($data);
     }
     try {
         Auth::find($id)->update($data);
         return redirect()->action('Admin\\AuthController@index')->with(array('dialog' => array('title' => '修改权限信息成功', 'message' => $data)));
     } catch (\Exception $e) {
         return redirect()->back()->withErrors(['error' => '修改权限信息失败, 请重试'])->with($data);
     }
 }