Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.14 KB | None | 0 0
  1. <?php
  2. namespace frontend\controllers;
  3.  
  4. use Yii;
  5. use yii\base\InvalidParamException;
  6. use yii\web\BadRequestHttpException;
  7. use yii\web\Controller;
  8. use yii\filters\VerbFilter;
  9. use yii\filters\AccessControl;
  10. use common\models\LoginForm;
  11. use frontend\models\PasswordResetRequestForm;
  12. use frontend\models\ResetPasswordForm;
  13. use frontend\models\SignupForm;
  14. use frontend\models\ContactForm;
  15. use common\models\User;
  16. use common\models\Logusuario;
  17. /**
  18.  * Site controller
  19.  */
  20. class SiteController extends Controller
  21. { public $successUrl = 'Success';
  22.     /**
  23.      * @inheritdoc
  24.      */
  25.     public function behaviors()
  26.     {
  27.         return [
  28.             'access' => [
  29.                 'class' => AccessControl::className(),
  30.                 'only' => ['logout', 'signup'],
  31.                 'rules' => [
  32.                     [
  33.                         'actions' => ['signup'],
  34.                         'allow' => true,
  35.                         'roles' => ['?'],
  36.                     ],
  37.                     [
  38.                         'actions' => ['logout'],
  39.                         'allow' => true,
  40.                         'roles' => ['@'],
  41.                     ],
  42.                 ],
  43.             ],
  44.             'verbs' => [
  45.                 'class' => VerbFilter::className(),
  46.                 'actions' => [
  47.                     'logout' => ['post'],
  48.                 ],
  49.             ],
  50.         ];
  51.     }
  52.  
  53.     /**
  54.      * @inheritdoc
  55.      */
  56.     public function actions()
  57.     {
  58.         return [
  59.             'error' => [
  60.                 'class' => 'yii\web\ErrorAction',
  61.             ],
  62.             'captcha' => [
  63.                 'class' => 'yii\captcha\CaptchaAction',
  64.                 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  65.             ],
  66.     'auth' => [
  67.             'class' => 'yii\authclient\AuthAction',
  68.             'successCallback' => [$this, 'successCallback'],
  69.         ],
  70.         ];
  71.     }  
  72.      
  73. public function successCallback($client)
  74. {  
  75.     $attributes = $client->getUserAttributes();
  76.         // user login or signup comes here
  77.         /*  
  78.         Checking facebook email registered yet?
  79.         Maxsure your registered email when login same with facebook email
  80.         die(print_r($attributes));
  81.         */
  82.         $user = \common\models\User::find()->where(['username'=>$attributes['name']])->one();
  83.         if(empty($user)){
  84.         $model = new User();
  85.      
  86.         $tipo_auth='Facebook';
  87.        
  88.         $password = md5($attributes['email']);
  89.         if(empty($attributes['email'])){
  90.         $attributes['email']= '@'.$attributes['screen_name'];
  91.         $tipo_auth='Twitter';
  92.         }
  93.    
  94.         //$auth_key=$this->auth_key = Yii::$app->security->generateRandomString();
  95.         $created_at = date('Y-m-d h:i:s');
  96.         $update_at = date('Y-m-d h:i:s');
  97.  
  98.         $model -> username = $attributes['name'];
  99.         $model->password = $password;
  100.         $model->auth_key = $password;
  101.         $model->email = $attributes['email'];
  102.         $model->created_at = $created_at;
  103.         $model->updated_at = $update_at;
  104.         $model->tipo_autenticacion = $tipo_auth;
  105.         $model->status = 10;
  106.         $model->save();
  107.         }
  108.      
  109.         $user = \common\models\User::find()->where(['username'=>$attributes['name']])->one();
  110.         if(!empty($user)){
  111.             Yii::$app->user->login($user);
  112.        
  113.         }else{
  114.             // Save session attribute user from FB
  115.             $session = Yii::$app->session;
  116.             $session['attributes']=$attributes;
  117.             // redirect to form signup, variabel global set to successUrl
  118.             $this->successUrl = \yii\helpers\Url::to(['signup']);
  119.         }
  120. }
  121.     /**
  122.      * Displays homepage.
  123.      *
  124.      * @return mixed
  125.      */
  126.     public function actionIndex()
  127.     {
  128.         return $this->render('index');
  129.     }
  130.  
  131.     /**
  132.      * Logs in a user.
  133.      *
  134.      * @return mixed
  135.      */
  136.     public function actionLogin()
  137.     {    
  138.         $model = new User();    
  139.         $tipo_auth='Facebook';
  140.         //$auth_key=$this->auth_key = Yii::$app->security->generateRandomString();
  141.         $created_at = date('Y-m-d h:i:s');
  142.         $update_at = date('Y-m-d h:i:s');
  143.  
  144.         $model -> username = 'edo';
  145.         $model->password = '1234556';
  146.         $model->auth_key = '1234556';
  147.         $model->email = 'gaedvago@h.com';
  148.         $model->created_at = $created_at;
  149.         $model->updated_at = $update_at;
  150.         $model->tipo_autenticacion = $tipo_auth;
  151.         $model->status = 10;
  152.         $model->save();
  153.        
  154.             if(isset($_POST['LoginForm']))
  155.         {    
  156.        
  157.         $log= new Logusuario();
  158.        
  159.         $log->usuarioid='2';
  160.         $log->fechaingresa='12-05-13';
  161.         $log->usuarioid='12-05-13';
  162.         $log->ip='12.05.13.12';
  163.         $log->puerto='1223';
  164.         if(!$model->save()){
  165.         VarDumper::dump($model->errors, 10, true);
  166.          }  
  167.         }
  168.        
  169.         if (!Yii::$app->user->isGuest) {
  170.             return $this->goHome();
  171.        
  172.         }
  173.  
  174.         $model = new LoginForm();
  175.         if ($model->load(Yii::$app->request->post()) && $model->login()) {
  176.             return $this->goBack();
  177.      
  178.         } else {
  179.             return $this->render('login', [
  180.                 'model' => $model,
  181.             ]);
  182.         }
  183.     }
  184.  
  185.     /**
  186.      * Logs out the current user.
  187.      *
  188.      * @return mixed
  189.      */
  190.     public function actionLogout()
  191.     {
  192.         Yii::$app->user->logout();
  193.  
  194.         return $this->goHome();
  195.     }
  196.  
  197.     /**
  198.      * Displays contact page.
  199.      *
  200.      * @return mixed
  201.      */
  202.    public function actionContact()
  203.     {
  204.         $model = new ContactForm();
  205.         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  206.             if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  207.                 Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  208.             } else {
  209.                 Yii::$app->session->setFlash('error', 'There was an error sending email.');
  210.             }
  211.  
  212.             return $this->refresh();
  213.         } else {
  214.             return $this->render('contact', [
  215.                 'model' => $model,
  216.             ]);
  217.         }
  218.     }
  219.  
  220.     /**
  221.      * Displays about page.
  222.      *
  223.      * @return mixed
  224.      */
  225.     public function actionAbout()
  226.     {
  227.         return $this->render('about');
  228.     }
  229.  
  230.     /**
  231.      * Signs user up.
  232.      *
  233.      * @return mixed
  234.      */
  235.     public function actionSignup()
  236.     {
  237.         $model = new SignupForm();
  238.         if ($model->load(Yii::$app->request->post())) {
  239.             if ($user = $model->signup()) {
  240.                 if (Yii::$app->getUser()->login($user)) {
  241.                     return $this->goHome();
  242.                 }
  243.             }
  244.         }
  245.  
  246.         return $this->render('signup', [
  247.             'model' => $model,
  248.         ]);
  249.     }
  250.  
  251.     /**
  252.      * Requests password reset.
  253.      *
  254.      * @return mixed
  255.      */
  256.     public function actionRequestPasswordReset()
  257.     {
  258.         $model = new PasswordResetRequestForm();
  259.         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  260.             if ($model->sendEmail()) {
  261.                 Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  262.  
  263.                 return $this->goHome();
  264.             } else {
  265.                 Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
  266.             }
  267.         }
  268.  
  269.         return $this->render('requestPasswordResetToken', [
  270.             'model' => $model,
  271.         ]);
  272.     }
  273.  
  274.     /**
  275.      * Resets password.
  276.      *
  277.      * @param string $token
  278.      * @return mixed
  279.      * @throws BadRequestHttpException
  280.      */
  281.     public function actionResetPassword($token)
  282.     {
  283.         try {
  284.             $model = new ResetPasswordForm($token);
  285.         } catch (InvalidParamException $e) {
  286.             throw new BadRequestHttpException($e->getMessage());
  287.         }
  288.  
  289.         if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  290.             Yii::$app->session->setFlash('success', 'New password was saved.');
  291.  
  292.             return $this->goHome();
  293.         }
  294.  
  295.         return $this->render('resetPassword', [
  296.             'model' => $model,
  297.         ]);
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement