[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Security/ -> TUser.php (source)

   1  <?php
   2  /**
   3   * TUser class file.
   4   *
   5   * @author Qiang Xue <qiang.xue@gmail.com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TUser.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Security
  11   */
  12  
  13  /**
  14   * Using IUserManager interface
  15   */
  16  Prado::using('System.Security.IUserManager');
  17  
  18  /**
  19   * TUser class
  20   *
  21   * TUser implements basic user functionality for a prado application.
  22   * To get the name of the user, use {@link getName Name} property.
  23   * The property {@link getIsGuest IsGuest} tells if the user a guest/anonymous user.
  24   * To obtain or test the roles that the user is in, use property
  25   * {@link getRoles Roles} and call {@link isInRole()}, respectively.
  26   *
  27   * TUser is meant to be used together with {@link IUserManager}.
  28   *
  29   * @author Qiang Xue <qiang.xue@gmail.com>
  30   * @version $Id: TUser.php 1397 2006-09-07 07:55:53Z wei $
  31   * @package System.Security
  32   * @since 3.0
  33   */
  34  class TUser extends TComponent implements IUser
  35  {
  36      /**
  37       * @var IUserManager user manager
  38       */
  39      private $_manager;
  40      /**
  41       * @var boolean if the user is a guest
  42       */
  43      private $_isGuest=true;
  44      /**
  45       * @var string username
  46       */
  47      private $_name='';
  48      /**
  49       * @var array user roles
  50       */
  51      private $_roles=array();
  52  
  53      /**
  54       * Constructor.
  55       * @param IUserManager user manager
  56       */
  57  	public function __construct(IUserManager $manager)
  58      {
  59          $this->_manager=$manager;
  60          $this->_name=$manager->getGuestName();
  61      }
  62  
  63      /**
  64       * @return IUserManager user manager
  65       */
  66  	public function getManager()
  67      {
  68          return $this->_manager;
  69      }
  70  
  71      /**
  72       * @return string username
  73       */
  74  	public function getName()
  75      {
  76          return $this->_name;
  77      }
  78  
  79      /**
  80       * @param string username
  81       */
  82  	public function setName($value)
  83      {
  84          $this->_name=$value;
  85      }
  86  
  87      /**
  88       * @return boolean if the user is a guest
  89       */
  90  	public function getIsGuest()
  91      {
  92          return $this->_isGuest;
  93      }
  94  
  95      /**
  96       * @param boolean if the user is a guest
  97       */
  98  	public function setIsGuest($value)
  99      {
 100          if($this->_isGuest=TPropertyValue::ensureBoolean($value))
 101          {
 102              $this->_name=$this->_manager->getGuestName();
 103              $this->_roles=array();
 104          }
 105      }
 106  
 107      /**
 108       * @return array list of roles that the user is of
 109       */
 110  	public function getRoles()
 111      {
 112          return $this->_roles;
 113      }
 114  
 115      /**
 116       * @return array|string list of roles that the user is of. If it is a string, roles are assumed by separated by comma
 117       */
 118  	public function setRoles($value)
 119      {
 120          if(is_array($value))
 121              $this->_roles=$value;
 122          else
 123          {
 124              $this->_roles=array();
 125              foreach(explode(',',$value) as $role)
 126              {
 127                  if(($role=trim($role))!=='')
 128                      $this->_roles[]=$role;
 129              }
 130          }
 131      }
 132  
 133      /**
 134       * @param string role to be tested. Note, role is case-insensitive.
 135       * @return boolean whether the user is of this role
 136       */
 137  	public function isInRole($role)
 138      {
 139          foreach($this->_roles as $r)
 140              if(strcasecmp($role,$r)===0)
 141                  return true;
 142          return false;
 143      }
 144  
 145      /**
 146       * @return string user data that is serialized and will be stored in session
 147       */
 148  	public function saveToString()
 149      {
 150          return serialize(array($this->_name,$this->_roles,$this->_isGuest));
 151      }
 152  
 153      /**
 154       * @param string user data that is serialized and restored from session
 155       * @return IUser the user object
 156       */
 157  	public function loadFromString($data)
 158      {
 159          if(!empty($data))
 160          {
 161              $array=unserialize($data);
 162              $this->_name=$array[0];
 163              $this->_roles=$array[1];
 164              $this->_isGuest=$array[2];
 165          }
 166          return $this;
 167      }
 168  }
 169  
 170  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7