05.04 php saved
AppControllerwAuth
Tags add more
Auth Tracker  
Note
AppControllerwAuth did not leave a note
  1. <?php
  2. class AppController extends Controller {
  3.  
  4.     var $components = array('Auth');
  5.     var $uses = array('Tracker');
  6.  
  7. /**
  8. * Load the Authentication
  9. *
  10. * @access public
  11. */
  12.     function beforeFilter(){
  13.         //Set up Auth Component
  14.         $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
  15.         $this->Auth->logoutRedirect = '/users/login';
  16.         //$this->Auth->allow('display');  // We want to force a login for all pages.
  17.        
  18.         // Controller autorization is the simplest form.
  19.         $this->Auth->authorize = 'controller';
  20.         $this->Auth->loginError = 'Login failed. Invalid username or password.';
  21.         $this->Auth->authError = 'Please login in order to access the requested resource.';
  22.        
  23.         // Additional criteria for loging.
  24.         $this->Auth->userScope = array('User.active' => 1); // user needs to be active.
  25.        
  26.         $this->recordActivity();
  27.         $this->log('Last line in AppController::beforeFilter()');
  28.     }
  29. /**
  30. * Ensure user is authorized.
  31. *
  32. * In its purest form, this function MUST be present and return true. It works sort of like a prevalidation for additional criteria.
  33. * I've done a little extra to make sure the user with admin routing is the admin:
  34. * If using admin route, ensure user is admin (group_id ==1). If not, don't authorize.
  35. * @access public
  36. */
  37.     function isAuthorized() {
  38.         if (isset($this->params[Configure::read('Routing.admin')])) {
  39.             if ($this->Auth->user('group_id') != 1) {
  40.                 return false;
  41.             }
  42.         }
  43.         return true;
  44.    }
  45.  
  46.     function recordActivity() {
  47.         $pages = str_replace("/index.php",'', $_SERVER['REQUEST_URI']);
  48.         $pages = explode("/", $pages);
  49.         $this->data['Tracker']['user_id'] = $this->Auth->user('id');
  50.         $this->data['Tracker']['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  51.         $this->data['Tracker']['user_ip'] = $_SERVER['REMOTE_ADDR'];
  52.         $this->data['Tracker']['controller'] = (isset($pages[1]) ? $pages[1] : '');
  53.         $this->data['Tracker']['action'] = (isset($pages[2]) ? $pages[2] : '');
  54.         if(isset($pages[3])) {
  55.             $params = $pages;
  56.             array_shift($params);
  57.             array_shift($params);
  58.             array_shift($params);
  59.             $this->data['Tracker']['params'] = implode('/', $params);
  60.         }
  61.         if(isset($_SERVER['HTTP_REFERER'])) {
  62.             $this->data['Tracker']['referer'] = $_SERVER['HTTP_REFERER'];
  63.         } else {
  64.             $this->data['Tracker']['referer'] = '';
  65.         }
  66.         $this->Tracker->save($this->data);
  67.         unset($this->data['Tracker']);
  68.     }
  69. }
  70. ?>
Parsed in 0.146 seconds, using GeSHi 1.0.7.14

Modify this Paste