11.28 php saved
markstory
Tags add more
AuthComponent  
Note
markstory did not leave a note
  1. /** app controller. **/
  2.  
  3. class AppController extends Controller {
  4.     var $components = array( 'Acl', 'Auth');
  5.    
  6.     function beforeFilter(){
  7.         Security::setHash('md5');
  8.        
  9.         //Set up Auth Component
  10.         $this->Auth->autoRedirect = false;
  11.         $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
  12.         $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'index');
  13.         $this->Auth->logoutRedirect = '/users/login';
  14.        
  15.         // Acl Seems like overkill for a simple blog site.
  16.         //$this->Auth->authorize = 'actions'; //Check ACL against Controller::action
  17.         $this->Auth->authorize = 'controller';
  18.        
  19.         //$this->Auth->actionPath = 'controllers/'; //controllers are stored under controllers.
  20.         $this->Auth->loginError = 'Could not Log in Try again';
  21.        
  22.         $this->Auth->userScope = array('User.active' => 1); //user needs to be active.
  23.        
  24.         //Default user
  25.         if(!$this->Session->check('Auth.User')){
  26.             $this->Session->write('Auth.User', array('id' => 2,
  27.                                                      'username' => 'anonymous',
  28.                                                      'group_id' => '3',
  29.                                                      'active' => '1'                                               
  30.                                                     ));
  31.         }      
  32.     }
  33.    
  34.     function isAuthorized(){
  35.         if(isset($this->params[Configure::read('Routing.admin')])){
  36.             if($this->Auth->user()){
  37.                
  38.             }
  39.             return true;
  40.         }
  41.         return true;
  42.     }
  43.    
  44. }
  45. ?>
  46.  
  47. /** users controller **/
  48.  
  49. class usersController extends AppController
  50. {
  51.     var $name = 'Users';
  52.     var $helpers = array('Html', 'Form');
  53.     var $uses = array('User');
  54.    
  55.    
  56.     function login(){
  57.         if(!empty($this->data['User'])){
  58.             if($this->Auth->user()){
  59.                 $this->Session->setFlash('Thanks!');
  60.                
  61.                 $this->redirect($this->Auth->redirect());
  62.             }
  63.         }
  64.     }
  65.    
  66.     function logout(){
  67.         $this->Session->setFlash('Good-Bye');
  68.         $this->redirect($this->Auth->logout());
  69.     }
  70. } // END class usersController extends AppController
Parsed in 0.112 seconds, using GeSHi 1.0.7.14

Modify this Paste