Guest User

Untitled

a guest
May 21st, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.45 KB | None | 0 0
  1. <?php
  2. App::uses('AppController', 'Controller');
  3. /**
  4. * Users Controller
  5. *
  6. * @property User $User
  7. */
  8. class UsersController extends AppController {
  9.     var $components = array('Auth'); //No es necesario si se declaro en el app controller
  10.     var $helpers=array("Session", "Html", "Form");
  11.     /**
  12.      * El AuthComponent proporciona la funcionalidad necesaria
  13.      * para el acceso (login), por lo que se puede dejar esta función en blanco.
  14.      */
  15.     public function login(){
  16.         if($this->request->is('post')){
  17.             if($this->Auth->login()){
  18.                 return $this->redirect($this->Auth->redirect());
  19.             } else {
  20.                 $this->Session->setFlash(__('Usuario o Password invalido.'), 'default', array(), 'auth');
  21.             }
  22.         }
  23.     }
  24.    
  25.     function logout() {
  26.         $this->redirect($this->Auth->logout());
  27.     }
  28.    
  29. /**
  30.  * index method
  31.  *
  32.  * @return void
  33.  */
  34.     public function index() {
  35.         $this->User->recursive = 0;
  36.         $this->set('users', $this->paginate());
  37.     }
  38.  
  39. /**
  40.  * view method
  41.  *
  42.  * @param string $id
  43.  * @return void
  44.  */
  45.     public function view($id = null) {
  46.         $this->User->id = $id;
  47.         if (!$this->User->exists()) {
  48.             throw new NotFoundException(__('Invalid user'));
  49.         }
  50.         $this->set('user', $this->User->read(null, $id));
  51.     }
  52.  
  53. /**
  54.  * add method
  55.  *
  56.  * @return void
  57.  */
  58.     public function add() {
  59.         if ($this->request->is('post')) {
  60.             $this->User->create();
  61.             if(!empty($this->data["User"]["password"])){
  62.                 $this->request->data["User"]["password"]=Security::hash($this->data["User"]["password"],null, true);
  63.             }else{
  64.                 $this->Session->setFlash(__('La contraseña no puede estar vacia'));
  65.                 $this->redirect(array('action' => 'add'));
  66.             }
  67.             if ($this->User->save($this->request->data)) {
  68.                 $this->Session->setFlash(__('The user has been saved'));
  69.                 $this->redirect(array('action' => 'index'));
  70.             } else {
  71.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
  72.             }
  73.         }
  74.         $typeusers = $this->User->Typeuser->find('list');
  75.         $sections = $this->User->Section->find('list');
  76.         $this->set(compact('typeusers', 'sections'));
  77.     }
  78.  
  79. /**
  80.  * edit method
  81.  *
  82.  * @param string $id
  83.  * @return void
  84.  */
  85.     public function edit($id = null) {
  86.         $this->User->id = $id;
  87.         if (!$this->User->exists()) {
  88.             throw new NotFoundException(__('Invalid user'));
  89.         }
  90.         if ($this->request->is('post') || $this->request->is('put')) {
  91.             if(!empty($this->data["User"]["password"])){
  92.                 $this->request->data["User"]["password"]=Security::hash($this->data["User"]["password"],null, true);
  93.             }else{
  94.                 unset($this->request->data["User"]["password"]);
  95.             }
  96.             if ($this->User->save($this->request->data)) {
  97.                 $this->Session->setFlash(__('El usuario ha sido guardado'));
  98.                 //$this->redirect(array('action' => 'index'));
  99.             } else {
  100.                 $this->Session->setFlash(__('El usuario no ha sido guardado. Por favor, intente de nuevo.'));
  101.             }
  102.         } else {
  103.             $this->request->data = $this->User->read(null, $id);
  104.         }
  105.     }
  106.  
  107. /**
  108.  * delete method
  109.  *
  110.  * @param string $id
  111.  * @return void
  112.  */
  113.     public function delete($id = null) {
  114.         if (!$this->request->is('post')) {
  115.             throw new MethodNotAllowedException();
  116.         }
  117.         $this->User->id = $id;
  118.         if (!$this->User->exists()) {
  119.             throw new NotFoundException(__('Invalid user'));
  120.         }
  121.         if ($this->User->delete()) {
  122.             $this->Session->setFlash(__('User deleted'));
  123.             $this->redirect(array('action'=>'index'));
  124.         }
  125.         $this->Session->setFlash(__('User was not deleted'));
  126.         $this->redirect(array('action' => 'index'));
  127.     }
  128. }
Add Comment
Please, Sign In to add comment