Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace frontend\controllers;
- use Yii;
- use yii\base\InvalidParamException;
- use yii\web\BadRequestHttpException;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use common\models\LoginForm;
- use frontend\models\PasswordResetRequestForm;
- use frontend\models\ResetPasswordForm;
- use frontend\models\SignupForm;
- use frontend\models\ContactForm;
- use common\models\User;
- use common\models\Logusuario;
- /**
- * Site controller
- */
- class SiteController extends Controller
- { public $successUrl = 'Success';
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'only' => ['logout', 'signup'],
- 'rules' => [
- [
- 'actions' => ['signup'],
- 'allow' => true,
- 'roles' => ['?'],
- ],
- [
- 'actions' => ['logout'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],
- ];
- }
- /**
- * @inheritdoc
- */
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- 'auth' => [
- 'class' => 'yii\authclient\AuthAction',
- 'successCallback' => [$this, 'successCallback'],
- ],
- ];
- }
- public function successCallback($client)
- {
- $attributes = $client->getUserAttributes();
- // user login or signup comes here
- /*
- Checking facebook email registered yet?
- Maxsure your registered email when login same with facebook email
- die(print_r($attributes));
- */
- $user = \common\models\User::find()->where(['username'=>$attributes['name']])->one();
- if(empty($user)){
- $model = new User();
- $tipo_auth='Facebook';
- $password = md5($attributes['email']);
- if(empty($attributes['email'])){
- $attributes['email']= '@'.$attributes['screen_name'];
- $tipo_auth='Twitter';
- }
- //$auth_key=$this->auth_key = Yii::$app->security->generateRandomString();
- $created_at = date('Y-m-d h:i:s');
- $update_at = date('Y-m-d h:i:s');
- $model -> username = $attributes['name'];
- $model->password = $password;
- $model->auth_key = $password;
- $model->email = $attributes['email'];
- $model->created_at = $created_at;
- $model->updated_at = $update_at;
- $model->tipo_autenticacion = $tipo_auth;
- $model->status = 10;
- $model->save();
- }
- $user = \common\models\User::find()->where(['username'=>$attributes['name']])->one();
- if(!empty($user)){
- Yii::$app->user->login($user);
- }else{
- // Save session attribute user from FB
- $session = Yii::$app->session;
- $session['attributes']=$attributes;
- // redirect to form signup, variabel global set to successUrl
- $this->successUrl = \yii\helpers\Url::to(['signup']);
- }
- }
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- public function actionIndex()
- {
- return $this->render('index');
- }
- /**
- * Logs in a user.
- *
- * @return mixed
- */
- public function actionLogin()
- {
- $model = new User();
- $tipo_auth='Facebook';
- //$auth_key=$this->auth_key = Yii::$app->security->generateRandomString();
- $created_at = date('Y-m-d h:i:s');
- $update_at = date('Y-m-d h:i:s');
- $model -> username = 'edo';
- $model->password = '1234556';
- $model->auth_key = '1234556';
- $model->email = 'gaedvago@h.com';
- $model->created_at = $created_at;
- $model->updated_at = $update_at;
- $model->tipo_autenticacion = $tipo_auth;
- $model->status = 10;
- $model->save();
- if(isset($_POST['LoginForm']))
- {
- $log= new Logusuario();
- $log->usuarioid='2';
- $log->fechaingresa='12-05-13';
- $log->usuarioid='12-05-13';
- $log->ip='12.05.13.12';
- $log->puerto='1223';
- if(!$model->save()){
- VarDumper::dump($model->errors, 10, true);
- }
- }
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- return $this->goBack();
- } else {
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Logs out the current user.
- *
- * @return mixed
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
- /**
- * Displays contact page.
- *
- * @return mixed
- */
- public function actionContact()
- {
- $model = new ContactForm();
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
- Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
- } else {
- Yii::$app->session->setFlash('error', 'There was an error sending email.');
- }
- return $this->refresh();
- } else {
- return $this->render('contact', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Displays about page.
- *
- * @return mixed
- */
- public function actionAbout()
- {
- return $this->render('about');
- }
- /**
- * Signs user up.
- *
- * @return mixed
- */
- public function actionSignup()
- {
- $model = new SignupForm();
- if ($model->load(Yii::$app->request->post())) {
- if ($user = $model->signup()) {
- if (Yii::$app->getUser()->login($user)) {
- return $this->goHome();
- }
- }
- }
- return $this->render('signup', [
- 'model' => $model,
- ]);
- }
- /**
- * Requests password reset.
- *
- * @return mixed
- */
- public function actionRequestPasswordReset()
- {
- $model = new PasswordResetRequestForm();
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- if ($model->sendEmail()) {
- Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
- return $this->goHome();
- } else {
- Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
- }
- }
- return $this->render('requestPasswordResetToken', [
- 'model' => $model,
- ]);
- }
- /**
- * Resets password.
- *
- * @param string $token
- * @return mixed
- * @throws BadRequestHttpException
- */
- public function actionResetPassword($token)
- {
- try {
- $model = new ResetPasswordForm($token);
- } catch (InvalidParamException $e) {
- throw new BadRequestHttpException($e->getMessage());
- }
- if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
- Yii::$app->session->setFlash('success', 'New password was saved.');
- return $this->goHome();
- }
- return $this->render('resetPassword', [
- 'model' => $model,
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement