01.31 php saved
the_undefined
Tags add more
ACL and auth  
Note
SimpleAuthComponent.

See http://www.thinkingphp.org/2006/10/03/a-lightweight-approach-to-acl-the-33-lines-of-magic/ for more information.
  1. <?php
  2. /**
  3. * Created: Sun Sep 17 14:44:50 CEST 2006
  4. *
  5. * DESCRIPTION
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * Copyright (c) Felix Geisendörfer <info@fg-webdesign.de>
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright      Copyright (c) 2006, Felix Geisendörfer.
  15. * @link            http://www.fg-webdesign.de/
  16. * @link            http://www.thinkingphp.org/
  17. * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
  18. */
  19.  
  20. class SimpleAuthComponent extends Object
  21. {
  22.     /**
  23.      * You can set this to false in your AppController's beforeFilter to deactivate this Component
  24.      *
  25.      * @var boolean
  26.      */
  27.     var $enabled = true;
  28.  
  29.     /**
  30.      * If you use a different Model for your users table, change this value in your
  31.      * AppController's beforeFilter.
  32.      *
  33.      * @var string
  34.      */
  35.     var $userModel = 'User';
  36.    
  37.     /**
  38.      * If you use a different Model for your groups table, change this value in your
  39.      * AppController's beforeFilter.
  40.      *
  41.      * @var string
  42.      */
  43.     var $groupModel = 'Group';
  44.        
  45.     /**
  46.      * Name of the field a User can be uniquely identified besides the id field.
  47.      *
  48.      * @var string
  49.      */
  50.     var $userIdentifier = 'name';
  51.    
  52.     /**
  53.      * The $userIdentifier (name) of the default user which *has* to exist in the
  54.      * $userModel and is used if there is no active (logged in) user yet as the default
  55.      * account.
  56.      *
  57.      * @var string
  58.      */
  59.     var $defaultUser = 'guest';
  60.  
  61.     /**
  62.      * In case the $defaultUser couldn't be found can can provide a callback that handles this
  63.      * situation or leave this empty in which case a fatal php error will show up (the one
  64.      * already mentioned on the variable above).
  65.      *
  66.      * @var mixed
  67.      */
  68.     var $criticalErrorCallback = null;
  69.    
  70.     /**
  71.      * Contains all data associated to the active user
  72.      *
  73.      * @var array
  74.      */
  75.     var $activeUser = null;
  76.    
  77.     /**
  78.      * Contains the reference to the Controller.
  79.      *
  80.      * @var object
  81.      */
  82.     var $Controller;
  83.    
  84.     function startup(&$Controller)
  85.     {
  86.         $this->Controller = &$Controller;
  87.        
  88.         // If the component got disabled, exit.
  89.         if ($this->enabled===false)
  90.             return;
  91.        
  92.         // In case a cakeError is raised, this will make sure our class continues to work
  93.         if (!isset($this->Controller->{$this->userModel}))
  94.             $this->Controller->constructClasses();       
  95.            
  96.         // Get the activeUser (array)
  97.         $this->activeUser = $this->getActiveUser();
  98.  
  99.         // If no activeUser, not even the default one, could be found, raise an error
  100.         if (empty($this->activeUser))
  101.         {
  102.             // In case there is no registered Callback, print a simple php error message.
  103.             if (empty($this->criticalErrorCallback))
  104.             {               
  105.                 trigger_error('Unable to find a user account "'.$this->defaultUser.'". Permission Denied for security reasons.', E_USER_ERROR);               
  106.                
  107.                 // This exit should not be needed, but since I'm not sure if there is a way you can mess things up in php.ini this stays here
  108.                 exit;
  109.             }           
  110.             else // Otherwise, call the callback function and return
  111.                 return call_user_func_array($this->criticalErrorCallback, array($controller, $action, &$this));
  112.         }
  113.     }
  114.    
  115.     function getActiveUser()
  116.     {   
  117.         // If we already have an activeUser set, return it
  118.         if (!empty($this->activeUser))
  119.             return $this->activeUser;
  120.    
  121.         // See if the activeUserId is stored in our session and fetch the User for it if so
  122.         if ($activeUserId=$this->Controller->Session->read($this->userModel.'.id'))
  123.             $user = $this->Controller->{$this->userModel}->findById($activeUserId);
  124.  
  125.         // If no activeUserId was set, or it couldn't be found, fall back to the defaultUser account
  126.         if (!isset($user) || empty($user))
  127.             $user = $this->Controller->{$this->userModel}->find(array($this->userModel.'.'.$this->userIdentifier => $this->defaultUser));
  128.        
  129.         return $user;
  130.     }
  131.    
  132.     function setActiveUser($id, $refresh = false)
  133.     {
  134.         // If no $refresh is required, check if $id already is the active User
  135.         if (($refresh==false) && !empty($this->activeUser) && ($this->activeUser[$this->userModel]['id']==$id))
  136.             return true;
  137.  
  138.         // If a numeric $id was given, find the corresponding user
  139.         if (is_numeric($id))
  140.             $user = $this->Controller->{$this->userModel}->findById($id);
  141.         else // Or if not, find the default User
  142.             $user = $this->Controller->{$this->userModel}->find(array($this->userModel.'.'.$this->userIdentifier => $this->defaultUser));
  143.        
  144.         // If we couldn't find any User to make active return false
  145.         if (empty($user))
  146.             return false;
  147.            
  148.         // Set the activeUser for this class
  149.         $this->activeUser = $user;
  150.        
  151.         // And save our activeUser to the Session
  152.         $this->Controller->Session->write($this->userModel.'.id', $user[$this->userModel]['id']);
  153.        
  154.         // Job complete
  155.         return true;
  156.     }
  157. }
  158.  
  159. ?>
Parsed in 0.165 seconds, using GeSHi 1.0.7.14

Modify this Paste