Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.62 KB | None | 0 0
  1. <?php
  2. namespace App\Controller;
  3.  
  4. use App\Controller\AppController;
  5. use Cake\Event\Event;
  6. use Cake\Mailer\Email;
  7. use Cake\ORM\TableRegistry;
  8. use Cake\Auth\DefaultPasswordHasher;
  9.  
  10. /**
  11.  * Users Controller
  12.  *
  13.  * @property \App\Model\Table\UsersTable $Users
  14.  */
  15. class UsersController extends AppController
  16. {
  17.  
  18.     public function beforeFilter(Event $event)
  19.     {
  20.         parent::beforeFilter($event);
  21.         $this->Auth->allow(['add', 'activate']);
  22.     }
  23.  
  24.     public function login()
  25.     {
  26.         if ($this->request->is('post')) {
  27.             $user = $this->Auth->identify();
  28.             if ($user) {
  29.                 if($user['active']){
  30.                     $this->Auth->setUser($user);
  31.                     return $this->redirect($this->Auth->redirectUrl());
  32.                 }else{
  33.                     $this->Flash->error(__('Usuario no activo. Verifique su correo electrónico por favor.'));
  34.                 }
  35.             }else{
  36.                 $this->Flash->error(__('Correo electrónico o contraseña incorrectas'));
  37.             }
  38.         }
  39.     }
  40.  
  41.     public function logout()
  42.     {
  43.         $this->request->session()->destroy();
  44.         return $this->redirect($this->Auth->logout());
  45.     }
  46.    
  47.     /**
  48.      * Add method
  49.      *
  50.      * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
  51.      */
  52.     public function add()
  53.     {
  54.         $user = $this->Users->newEntity();
  55.         if ($this->request->is('post')) {
  56.            
  57.             $this->request->data['active'] = false;
  58.             $this->request->data['new'] = true;
  59.             $this->request->data['token'] = sha1(md5($this->request->data['email']));
  60.             $this->request->data['refer'] = 0;
  61.            
  62.             $user = $this->Users->patchEntity($user, $this->request->data);
  63.             if ($lastId = $this->Users->save($user)) {
  64.                 $this->Flash->success(__('Tu registro fue un éxito. Te enviamos un correo electrónico para que termines de validar tu cuenta.'));
  65.  
  66.                 $token = $lastId->id . '.' . $this->request->data['token'];
  67.                 $name = $this->request->data['name'];
  68.                 $email = $this->request->data['email'];
  69.                
  70.                 $this->sendWelcomeMail($email, $name, $token);
  71.  
  72.                 return $this->redirect(['controller'=>'users', 'action' => 'login']);
  73.             } else {
  74.                 $this->Flash->error(__('Ocurrió un error en el registro. Verifica todos los campos.'));
  75.             }
  76.         }
  77.         $this->set(compact('user'));
  78.         $this->set('_serialize', ['user']);
  79.     }
  80.  
  81.     public function activate(){
  82.  
  83.         $name = $this->request->query['n'];
  84.         $tokenid = $this->request->query['t'];
  85.         $pos = strpos($tokenid, '.');
  86.         $id = substr($tokenid, 0, $pos);
  87.         $token = substr($tokenid,$pos+1);
  88.  
  89.         $this->set('name', $name);
  90.        
  91.         $usersTable = TableRegistry::get('Users');
  92.         $user = $usersTable->get($id); // return id from user to activate
  93.  
  94.         if ($token != $user->token){
  95.             $this->Flash->error(__('Los campos de verificación no son correctos. No se puede validar correo electrónico.'));
  96.             return $this->redirect(['controller'=>'pages', 'action' => 'home']);
  97.         }else{
  98.             $user->active = true;
  99.             $usersTable->save($user);
  100.             $this->Flash->success(__('Correo electrónico validado. Ahora inicia sesión para viajar con nosotros.'));
  101.         }
  102.        
  103.     }
  104.  
  105.     /**
  106.      * Edit method
  107.      *
  108.      * @param string|null $id User id.
  109.      * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
  110.      * @throws \Cake\Network\Exception\NotFoundException When record not found.
  111.      */
  112.     public function edit()
  113.     {
  114.         $id = $this->Auth->user('id');
  115.        
  116.         $user = $this->Users->get($id, [
  117.             'contain' => []
  118.         ]);
  119.         if ($this->request->is(['patch', 'post', 'put'])) {
  120.             $user = $this->Users->patchEntity($user, $this->request->data);
  121.             if ($this->Users->save($user)) {
  122.                 $this->Flash->success(__('Tu perfil fué guardado correctamente.'));
  123.                 return $this->redirect(['action' => 'edit']);
  124.             } else {
  125.                 $this->Flash->error(__('Ocurrió un error.'));
  126.             }
  127.         }
  128.         $this->set(compact('user'));
  129.         $this->set('_serialize', ['user']);
  130.     }
  131.  
  132.     public function changepassword(){
  133.         if ($this->request->is('post')){
  134.            
  135.             $newpassword = $this->request->data['newpassword'];
  136.             $rnewpassword = $this->request->data['rnewpassword'];
  137.            
  138.             if($newpassword != $rnewpassword){
  139.                 $this->Flash->error(__('Las contraseñas no coinciden.'));
  140.             }else{
  141.                
  142.                 $usersTable = TableRegistry::get('Users');
  143.                 $user = $usersTable->get($this->Auth->user('id'));
  144.                 $user->password = $newpassword;
  145.                 $usersTable->save($user);
  146.                 $this->Flash->success(__('Contraseña cambiada correctamente.'));
  147.                 return $this->redirect(['action' => 'edit']);
  148.             }
  149.         }
  150.     }
  151.  
  152.     public function sendWelcomeMail($email, $name, $token){
  153.         $Email = new Email();
  154.         $Email->viewVars(['name' => $name, 'token' => $token]);
  155.         $Email->template('welcome', 'base')
  156.             ->emailFormat('html')
  157.             ->to($email)
  158.             ->from('registro@zmconexiones.com')
  159.             ->subject('Bievenido a ZMConexiones ' . $name)
  160.             ->send();
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement