Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- App::uses('AppController', 'Controller');
- /**
- * Users Controller
- *
- * @property User $User
- */
- class UsersController extends AppController {
- var $components = array('Auth'); //No es necesario si se declaro en el app controller
- var $helpers=array("Session", "Html", "Form");
- /**
- * El AuthComponent proporciona la funcionalidad necesaria
- * para el acceso (login), por lo que se puede dejar esta función en blanco.
- */
- public function login(){
- if($this->request->is('post')){
- if($this->Auth->login()){
- return $this->redirect($this->Auth->redirect());
- } else {
- $this->Session->setFlash(__('Usuario o Password invalido.'), 'default', array(), 'auth');
- }
- }
- }
- function logout() {
- $this->redirect($this->Auth->logout());
- }
- /**
- * index method
- *
- * @return void
- */
- public function index() {
- $this->User->recursive = 0;
- $this->set('users', $this->paginate());
- }
- /**
- * view method
- *
- * @param string $id
- * @return void
- */
- public function view($id = null) {
- $this->User->id = $id;
- if (!$this->User->exists()) {
- throw new NotFoundException(__('Invalid user'));
- }
- $this->set('user', $this->User->read(null, $id));
- }
- /**
- * add method
- *
- * @return void
- */
- public function add() {
- if ($this->request->is('post')) {
- $this->User->create();
- if(!empty($this->data["User"]["password"])){
- $this->request->data["User"]["password"]=Security::hash($this->data["User"]["password"],null, true);
- }else{
- $this->Session->setFlash(__('La contraseña no puede estar vacia'));
- $this->redirect(array('action' => 'add'));
- }
- if ($this->User->save($this->request->data)) {
- $this->Session->setFlash(__('The user has been saved'));
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
- }
- }
- $typeusers = $this->User->Typeuser->find('list');
- $sections = $this->User->Section->find('list');
- $this->set(compact('typeusers', 'sections'));
- }
- /**
- * edit method
- *
- * @param string $id
- * @return void
- */
- public function edit($id = null) {
- $this->User->id = $id;
- if (!$this->User->exists()) {
- throw new NotFoundException(__('Invalid user'));
- }
- if ($this->request->is('post') || $this->request->is('put')) {
- if(!empty($this->data["User"]["password"])){
- $this->request->data["User"]["password"]=Security::hash($this->data["User"]["password"],null, true);
- }else{
- unset($this->request->data["User"]["password"]);
- }
- if ($this->User->save($this->request->data)) {
- $this->Session->setFlash(__('El usuario ha sido guardado'));
- //$this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('El usuario no ha sido guardado. Por favor, intente de nuevo.'));
- }
- } else {
- $this->request->data = $this->User->read(null, $id);
- }
- }
- /**
- * delete method
- *
- * @param string $id
- * @return void
- */
- public function delete($id = null) {
- if (!$this->request->is('post')) {
- throw new MethodNotAllowedException();
- }
- $this->User->id = $id;
- if (!$this->User->exists()) {
- throw new NotFoundException(__('Invalid user'));
- }
- if ($this->User->delete()) {
- $this->Session->setFlash(__('User deleted'));
- $this->redirect(array('action'=>'index'));
- }
- $this->Session->setFlash(__('User was not deleted'));
- $this->redirect(array('action' => 'index'));
- }
- }
Add Comment
Please, Sign In to add comment