Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller;
- use App\Controller\AppController;
- use Cake\Event\Event;
- use Cake\Mailer\Email;
- use Cake\ORM\TableRegistry;
- use Cake\Auth\DefaultPasswordHasher;
- /**
- * Users Controller
- *
- * @property \App\Model\Table\UsersTable $Users
- */
- class UsersController extends AppController
- {
- public function beforeFilter(Event $event)
- {
- parent::beforeFilter($event);
- $this->Auth->allow(['add', 'activate']);
- }
- public function login()
- {
- if ($this->request->is('post')) {
- $user = $this->Auth->identify();
- if ($user) {
- if($user['active']){
- $this->Auth->setUser($user);
- return $this->redirect($this->Auth->redirectUrl());
- }else{
- $this->Flash->error(__('Usuario no activo. Verifique su correo electrónico por favor.'));
- }
- }else{
- $this->Flash->error(__('Correo electrónico o contraseña incorrectas'));
- }
- }
- }
- public function logout()
- {
- $this->request->session()->destroy();
- return $this->redirect($this->Auth->logout());
- }
- /**
- * Add method
- *
- * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
- */
- public function add()
- {
- $user = $this->Users->newEntity();
- if ($this->request->is('post')) {
- $this->request->data['active'] = false;
- $this->request->data['new'] = true;
- $this->request->data['token'] = sha1(md5($this->request->data['email']));
- $this->request->data['refer'] = 0;
- $user = $this->Users->patchEntity($user, $this->request->data);
- if ($lastId = $this->Users->save($user)) {
- $this->Flash->success(__('Tu registro fue un éxito. Te enviamos un correo electrónico para que termines de validar tu cuenta.'));
- $token = $lastId->id . '.' . $this->request->data['token'];
- $name = $this->request->data['name'];
- $email = $this->request->data['email'];
- $this->sendWelcomeMail($email, $name, $token);
- return $this->redirect(['controller'=>'users', 'action' => 'login']);
- } else {
- $this->Flash->error(__('Ocurrió un error en el registro. Verifica todos los campos.'));
- }
- }
- $this->set(compact('user'));
- $this->set('_serialize', ['user']);
- }
- public function activate(){
- $name = $this->request->query['n'];
- $tokenid = $this->request->query['t'];
- $pos = strpos($tokenid, '.');
- $id = substr($tokenid, 0, $pos);
- $token = substr($tokenid,$pos+1);
- $this->set('name', $name);
- $usersTable = TableRegistry::get('Users');
- $user = $usersTable->get($id); // return id from user to activate
- if ($token != $user->token){
- $this->Flash->error(__('Los campos de verificación no son correctos. No se puede validar correo electrónico.'));
- return $this->redirect(['controller'=>'pages', 'action' => 'home']);
- }else{
- $user->active = true;
- $usersTable->save($user);
- $this->Flash->success(__('Correo electrónico validado. Ahora inicia sesión para viajar con nosotros.'));
- }
- }
- /**
- * Edit method
- *
- * @param string|null $id User id.
- * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
- * @throws \Cake\Network\Exception\NotFoundException When record not found.
- */
- public function edit()
- {
- $id = $this->Auth->user('id');
- $user = $this->Users->get($id, [
- 'contain' => []
- ]);
- if ($this->request->is(['patch', 'post', 'put'])) {
- $user = $this->Users->patchEntity($user, $this->request->data);
- if ($this->Users->save($user)) {
- $this->Flash->success(__('Tu perfil fué guardado correctamente.'));
- return $this->redirect(['action' => 'edit']);
- } else {
- $this->Flash->error(__('Ocurrió un error.'));
- }
- }
- $this->set(compact('user'));
- $this->set('_serialize', ['user']);
- }
- public function changepassword(){
- if ($this->request->is('post')){
- $newpassword = $this->request->data['newpassword'];
- $rnewpassword = $this->request->data['rnewpassword'];
- if($newpassword != $rnewpassword){
- $this->Flash->error(__('Las contraseñas no coinciden.'));
- }else{
- $usersTable = TableRegistry::get('Users');
- $user = $usersTable->get($this->Auth->user('id'));
- $user->password = $newpassword;
- $usersTable->save($user);
- $this->Flash->success(__('Contraseña cambiada correctamente.'));
- return $this->redirect(['action' => 'edit']);
- }
- }
- }
- public function sendWelcomeMail($email, $name, $token){
- $Email = new Email();
- $Email->viewVars(['name' => $name, 'token' => $token]);
- $Email->template('welcome', 'base')
- ->emailFormat('html')
- ->to($email)
- ->from('registro@zmconexiones.com')
- ->subject('Bievenido a ZMConexiones ' . $name)
- ->send();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement