Advertisement
Guest User

Untitled

a guest
Apr 10th, 2012
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2. // app/Controller/UsersController.php
  3. class UsersController extends AppController {
  4.  
  5. //    public function beforeFilter() {
  6. //        parent::beforeFilter();
  7. //        $this->Auth->allow('add', 'logout');
  8. //    }
  9.  
  10.     public function index() {
  11.         $this->User->recursive = 0;
  12.         $this->set('users', $this->paginate());
  13.     }
  14.  
  15.     public function view($id = null) {
  16.         $this->User->id = $id;
  17.         if (!$this->User->exists()) {
  18.             throw new NotFoundException(__('Invalid user'));
  19.         }
  20.         $this->set('user', $this->User->read(null, $id));
  21.     }
  22.  
  23.     public function add() {
  24.         if ($this->request->is('post')) {
  25.             $this->User->create();
  26.             if ($this->User->save($this->request->data)) {
  27.                 $this->Session->setFlash(__('The user has been saved'));
  28.                 $this->redirect(array('action' => 'index'));
  29.             } else {
  30.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
  31.             }
  32.         }
  33.     }
  34.  
  35.     public function edit($id = null) {
  36.         $this->User->id = $id;
  37.         if (!$this->User->exists()) {
  38.             throw new NotFoundException(__('Invalid user'));
  39.         }
  40.         if ($this->request->is('post') || $this->request->is('put')) {
  41.             if ($this->User->save($this->request->data)) {
  42.                 $this->Session->setFlash(__('The user has been saved'));
  43.                 $this->redirect(array('action' => 'index'));
  44.             } else {
  45.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
  46.             }
  47.         } else {
  48.             $this->request->data = $this->User->read(null, $id);
  49.             unset($this->request->data['User']['password']);
  50.         }
  51.     }
  52.  
  53.     public function delete($id = null) {
  54.         if (!$this->request->is('post')) {
  55.             throw new MethodNotAllowedException();
  56.         }
  57.         $this->User->id = $id;
  58.         if (!$this->User->exists()) {
  59.             throw new NotFoundException(__('Invalid user'));
  60.         }
  61.         if ($this->User->delete()) {
  62.             $this->Session->setFlash(__('User deleted'));
  63.             $this->redirect(array('action' => 'index'));
  64.         }
  65.         $this->Session->setFlash(__('User was not deleted'));
  66.         $this->redirect(array('action' => 'index'));
  67.     }
  68.  
  69.     public function beforeFilter() {
  70.         parent::beforeFilter();
  71.         $this->Auth->allow('add'); // Letting users register themselves
  72.     }
  73.    
  74.     public function login() {
  75.         if ($this->request->is('post')) {
  76.             if ($this->Auth->login()) {
  77.                 $this->redirect($this->Auth->redirect());
  78.             } else {
  79.                 $this->Session->setFlash(__('Invalid username or password, try again'));
  80.             }
  81.         }
  82.     }
  83.    
  84.     public function logout() {
  85.         $this->redirect($this->Auth->logout());
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement