PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Security/Identity.php

https://github.com/bazo/Mokuji
PHP | 172 lines | 62 code | 38 blank | 72 comment | 5 complexity | d34bcef4128bb9bad4cc05e93a5e5cb2 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Security
  10. */
  11. /**
  12. * Default implementation of IIdentity.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette\Security
  16. *
  17. * @property mixed $id
  18. * @property array $roles
  19. *
  20. * @serializationVersion 1.0
  21. */
  22. class Identity extends FreezableObject implements IIdentity
  23. {
  24. /** @var mixed */
  25. private $id;
  26. /** @var array */
  27. private $roles;
  28. /** @var array */
  29. private $data;
  30. /**
  31. * @param mixed identity ID
  32. * @param mixed roles
  33. * @param array user data
  34. */
  35. public function __construct($id, $roles = NULL, $data = NULL)
  36. {
  37. $this->setId($id);
  38. $this->setRoles((array) $roles);
  39. $this->data = (array) $data;
  40. }
  41. /**
  42. * Sets the ID of user.
  43. * @param mixed
  44. * @return Identity provides a fluent interface
  45. */
  46. public function setId($id)
  47. {
  48. $this->updating();
  49. $this->id = is_numeric($id) ? 1 * $id : $id;
  50. return $this;
  51. }
  52. /**
  53. * Returns the ID of user.
  54. * @return mixed
  55. */
  56. public function getId()
  57. {
  58. return $this->id;
  59. }
  60. /**
  61. * Sets a list of roles that the user is a member of.
  62. * @param array
  63. * @return Identity provides a fluent interface
  64. */
  65. public function setRoles(array $roles)
  66. {
  67. $this->updating();
  68. $this->roles = $roles;
  69. return $this;
  70. }
  71. /**
  72. * Returns a list of roles that the user is a member of.
  73. * @return array
  74. */
  75. public function getRoles()
  76. {
  77. return $this->roles;
  78. }
  79. /**
  80. * Returns a user data.
  81. * @return array
  82. */
  83. public function getData()
  84. {
  85. return $this->data;
  86. }
  87. /**
  88. * Sets user data value.
  89. * @param string property name
  90. * @param mixed property value
  91. * @return void
  92. */
  93. public function __set($key, $value)
  94. {
  95. $this->updating();
  96. if (parent::__isset($key)) {
  97. parent::__set($key, $value);
  98. } else {
  99. $this->data[$key] = $value;
  100. }
  101. }
  102. /**
  103. * Returns user data value.
  104. * @param string property name
  105. * @return mixed
  106. */
  107. public function &__get($key)
  108. {
  109. if (parent::__isset($key)) {
  110. return parent::__get($key);
  111. } else {
  112. return $this->data[$key];
  113. }
  114. }
  115. /**
  116. * Is property defined?
  117. * @param string property name
  118. * @return bool
  119. */
  120. public function __isset($key)
  121. {
  122. return isset($this->data[$key]) || parent::__isset($key);
  123. }
  124. /**
  125. * Removes property.
  126. * @param string property name
  127. * @return void
  128. * @throws MemberAccessException
  129. */
  130. public function __unset($name)
  131. {
  132. throw new MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
  133. }
  134. }