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