Authentication.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ XESih5KhAF28smjTMCRZ3gRv83OcT4MT79/tW16d4/U=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * authentication service class
  14. */
  15. namespace Cube\Authentication;
  16. class Authentication
  17. {
  18. /**
  19. *
  20. * auth adapter interface
  21. *
  22. * @var \Cube\Authentication\Adapter\AdapterInterface
  23. */
  24. protected $_adapter;
  25. /**
  26. *
  27. * auth storage interface
  28. *
  29. * @var \Cube\Authentication\Storage\StorageInterface
  30. */
  31. protected $_storage;
  32. /**
  33. *
  34. * holds an instance of the object
  35. *
  36. * @var \Cube\Authentication\Authentication
  37. */
  38. protected static $_instance;
  39. /**
  40. *
  41. * class constructor
  42. */
  43. public function __construct(Storage\StorageInterface $storage = null)
  44. {
  45. $this->setStorage($storage);
  46. }
  47. /**
  48. *
  49. * returns an instance of the object and creates it if it wasnt instantiated yet
  50. *
  51. * @return \Cube\Authentication\Authentication
  52. */
  53. public static function getInstance()
  54. {
  55. if (!self::$_instance instanceof self) {
  56. self::$_instance = new self();
  57. }
  58. return self::$_instance;
  59. }
  60. /**
  61. *
  62. * get authentication adapter
  63. *
  64. * @return \Cube\Authentication\Adapter\AdapterInterface
  65. */
  66. public function getAdapter()
  67. {
  68. return $this->_adapter;
  69. }
  70. /**
  71. *
  72. * set authentication adapter
  73. *
  74. * @param \Cube\Authentication\Adapter\AdapterInterface $adapter
  75. * @return \Cube\Authentication\Authentication
  76. */
  77. public function setAdapter(Adapter\AdapterInterface $adapter)
  78. {
  79. $this->_adapter = $adapter;
  80. return $this;
  81. }
  82. /**
  83. *
  84. * get authentication storage
  85. *
  86. * @return \Cube\Authentication\Storage\StorageInterface
  87. */
  88. public function getStorage()
  89. {
  90. return $this->_storage;
  91. }
  92. /**
  93. *
  94. * set authentication storage
  95. * if no storage is submitted, a default session storage is created
  96. *
  97. * @param \Cube\Authentication\Storage\StorageInterface $storage
  98. * @return \Cube\Authentication\Authentication
  99. */
  100. public function setStorage(Storage\StorageInterface $storage = null)
  101. {
  102. if ($storage === null) {
  103. $storage = new Storage\Session();
  104. }
  105. $this->_storage = $storage;
  106. return $this;
  107. }
  108. /**
  109. *
  110. * authenticate using the supplied adapter
  111. *
  112. * @param \Cube\Authentication\Adapter\AdapterInterface $adapter
  113. * @return \Cube\Authentication\Result
  114. * @throws \RuntimeException
  115. */
  116. public function authenticate(Adapter\AdapterInterface $adapter = null)
  117. {
  118. if ($adapter === null) {
  119. if ((!$adapter = $this->getAdapter()) instanceof Adapter\AdapterInterface) {
  120. throw new \RuntimeException("The authentication service requires
  121. an object of type \Cube\Authentication\Adapter\AdapterInterface.");
  122. }
  123. }
  124. $result = $adapter->authenticate();
  125. if ($this->hasIdentity()) {
  126. $this->clearIdentity();
  127. }
  128. if ($result->isValid()) {
  129. $this->getStorage()->write(
  130. $result->getIdentity());
  131. }
  132. return $result;
  133. }
  134. /**
  135. *
  136. * check if identity exists
  137. *
  138. * @return bool
  139. */
  140. public function hasIdentity()
  141. {
  142. return !$this->getStorage()->isEmpty();
  143. }
  144. /**
  145. *
  146. * get identity
  147. *
  148. * @return null|mixed
  149. */
  150. public function getIdentity()
  151. {
  152. $storage = $this->getStorage();
  153. if ($storage->isEmpty()) {
  154. return null;
  155. }
  156. return $storage->read();
  157. }
  158. /**
  159. *
  160. * clear identity
  161. */
  162. public function clearIdentity()
  163. {
  164. $this->getStorage()->clear();
  165. }
  166. }