Source for file Identity.php

Documentation is available at Identity.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Security
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Security/IIdentity.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../FreezableObject.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Default implementation of IIdentity.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Security
  34. 34:  *
  35. 35:  * @property   string $name 
  36. 36:  * @property   mixed $id 
  37. 37:  * @property   array $roles 
  38. 38:  */
  39. 39: class Identity extends FreezableObject implements IIdentity
  40. 40: {
  41. 41:     /** @var string */
  42. 42:     private $name;
  43. 43:  
  44. 44:     /** @var array */
  45. 45:     private $roles;
  46. 46:  
  47. 47:     /** @var array */
  48. 48:     private $data;
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * @param  string  identity name
  53. 53:      * @param  mixed   roles
  54. 54:      * @param  array   user data
  55. 55:      */
  56. 56:     public function __construct($name$roles NULL$data NULL)
  57. 57:     {
  58. 58:         $this->setName($name);
  59. 59:         $this->setRoles((array) $roles);
  60. 60:         $this->data = (array) $data;
  61. 61:     }
  62. 62:  
  63. 63:  
  64. 64:  
  65. 65:     /**
  66. 66:      * Sets the name of user.
  67. 67:      * @param  string 
  68. 68:      * @return Identity  provides a fluent interface
  69. 69:      */
  70. 70:     public function setName($name)
  71. 71:     {
  72. 72:         $this->updating();
  73. 73:         $this->name = (string) $name;
  74. 74:         return $this;
  75. 75:     }
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /**
  80. 80:      * Returns the name of user.
  81. 81:      * @return string 
  82. 82:      */
  83. 83:     public function getName()
  84. 84:     {
  85. 85:         return $this->name;
  86. 86:     }
  87. 87:  
  88. 88:  
  89. 89:  
  90. 90:     /**
  91. 91:      * Sets a list of roles that the user is a member of.
  92. 92:      * @param  array 
  93. 93:      * @return Identity  provides a fluent interface
  94. 94:      */
  95. 95:     public function setRoles(array $roles)
  96. 96:     {
  97. 97:         $this->updating();
  98. 98:         $this->roles $roles;
  99. 99:         return $this;
  100. 100:     }
  101. 101:  
  102. 102:  
  103. 103:  
  104. 104:     /**
  105. 105:      * Returns a list of roles that the user is a member of.
  106. 106:      * @return array 
  107. 107:      */
  108. 108:     public function getRoles()
  109. 109:     {
  110. 110:         return $this->roles;
  111. 111:     }
  112. 112:  
  113. 113:  
  114. 114:  
  115. 115:     /**
  116. 116:      * Returns a user data.
  117. 117:      * @return array 
  118. 118:      */
  119. 119:     public function getData()
  120. 120:     {
  121. 121:         return $this->data;
  122. 122:     }
  123. 123:  
  124. 124:  
  125. 125:  
  126. 126:     /**
  127. 127:      * Sets user data value.
  128. 128:      * @param  string  property name
  129. 129:      * @param  mixed   property value
  130. 130:      * @return void 
  131. 131:      */
  132. 132:     public function __set($key$value)
  133. 133:     {
  134. 134:         $this->updating();
  135. 135:         if ($key === 'name' || $key === 'roles'{
  136. 136:             parent::__set($key$value);
  137. 137:  
  138. 138:         else {
  139. 139:             $this->data[$key$value;
  140. 140:         }
  141. 141:     }
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * Returns user data value.
  147. 147:      * @param  string  property name
  148. 148:      * @return mixed 
  149. 149:      */
  150. 150:     public function &__get($key)
  151. 151:     {
  152. 152:         if ($key === 'name' || $key === 'roles'{
  153. 153:             return parent::__get($key);
  154. 154:  
  155. 155:         else {
  156. 156:             return $this->data[$key];
  157. 157:         }
  158. 158:     }
  159. 159: