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   string $name 
  22. 22:  * @property   mixed $id 
  23. 23:  * @property   array $roles 
  24. 24:  *
  25. 25:  * @serializationVersion 0.9.3
  26. 26:  */
  27. 27: class Identity extends FreezableObject implements IIdentity
  28. 28: {
  29. 29:     /** @var string */
  30. 30:     private $name;
  31. 31:  
  32. 32:     /** @var array */
  33. 33:     private $roles;
  34. 34:  
  35. 35:     /** @var array */
  36. 36:     private $data;
  37. 37:  
  38. 38:  
  39. 39:     /**
  40. 40:      * @param  string  identity name
  41. 41:      * @param  mixed   roles
  42. 42:      * @param  array   user data
  43. 43:      */
  44. 44:     public function __construct($name$roles NULL$data NULL)
  45. 45:     {
  46. 46:         $this->setName($name);
  47. 47:         $this->setRoles((array) $roles);
  48. 48:         $this->data = (array) $data;
  49. 49:     }
  50. 50:  
  51. 51:  
  52. 52:  
  53. 53:     /**
  54. 54:      * Sets the name of user.
  55. 55:      * @param  string 
  56. 56:      * @return Identity  provides a fluent interface
  57. 57:      */
  58. 58:     public function setName($name)
  59. 59:     {
  60. 60:         $this->updating();
  61. 61:         $this->name = (string) $name;
  62. 62:         return $this;
  63. 63:     }
  64. 64:  
  65. 65:  
  66. 66:  
  67. 67:     /**
  68. 68:      * Returns the name of user.
  69. 69:      * @return string 
  70. 70:      */
  71. 71:     public function getName()
  72. 72:     {
  73. 73:         return $this->name;
  74. 74:     }
  75. 75:  
  76. 76:  
  77. 77:  
  78. 78:     /**
  79. 79:      * Sets a list of roles that the user is a member of.
  80. 80:      * @param  array 
  81. 81:      * @return Identity  provides a fluent interface
  82. 82:      */
  83. 83:     public function setRoles(array $roles)
  84. 84:     {
  85. 85:         $this->updating();
  86. 86:         $this->roles $roles;
  87. 87:         return $this;
  88. 88:     }
  89. 89:  
  90. 90:  
  91. 91:  
  92. 92:     /**
  93. 93:      * Returns a list of roles that the user is a member of.
  94. 94:      * @return array 
  95. 95:      */
  96. 96:     public function getRoles()
  97. 97:     {
  98. 98:         return $this->roles;
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * Returns a user data.
  105. 105:      * @return array 
  106. 106:      */
  107. 107:     public function getData()
  108. 108:     {
  109. 109:         return $this->data;
  110. 110:     }
  111. 111:  
  112. 112:  
  113. 113:  
  114. 114:     /**
  115. 115:      * Sets user data value.
  116. 116:      * @param  string  property name
  117. 117:      * @param  mixed   property value
  118. 118:      * @return void 
  119. 119:      */
  120. 120:     public function __set($key$value)
  121. 121:     {
  122. 122:         $this->updating();
  123. 123:         if ($key === 'name' || $key === 'roles'{
  124. 124:             parent::__set($key$value);
  125. 125:  
  126. 126:         else {
  127. 127:             $this->data[$key$value;
  128. 128:         }
  129. 129:     }
  130. 130:  
  131. 131:  
  132. 132:  
  133. 133:     /**
  134. 134:      * Returns user data value.
  135. 135:      * @param  string  property name
  136. 136:      * @return mixed 
  137. 137:      */
  138. 138:     public function &__get($key)
  139. 139:     {
  140. 140:         if ($key === 'name' || $key === 'roles'{
  141. 141:             return parent::__get($key);
  142. 142:  
  143. 143:         else {
  144. 144:             return $this->data[$key];
  145. 145:         }
  146. 146:     }
  147. 147: