PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Rediska/Zend/Auth/Adapter/Redis.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 191 lines | 78 code | 26 blank | 87 comment | 10 complexity | bb12c9cd78d7397f740c2c025e2879c9 MD5 | raw file
  1. <?php
  2. // Require Rediska
  3. require_once dirname(__FILE__) . '/../../../../Rediska.php';
  4. /**
  5. * @see Zend_Auth_Adapter_Interface
  6. */
  7. require_once 'Zend/Auth/Adapter/Interface.php';
  8. /**
  9. * @see Zend_Auth_Result
  10. */
  11. require_once 'Zend/Auth/Result.php';
  12. /**
  13. * @see Zend_Auth_Adapter_Exception
  14. */
  15. require_once 'Zend/Auth/Adapter/Exception.php';
  16. /**
  17. * @see Zend_Config
  18. */
  19. require_once 'Zend/Config.php';
  20. /**
  21. * Redis adapter for Zend_Auth
  22. *
  23. * @author Ivan Shumkov
  24. * @package Rediska
  25. * @subpackage ZendFrameworkIntegration
  26. * @version 0.5.7
  27. * @link http://rediska.geometria-lab.net
  28. * @license http://www.opensource.org/licenses/bsd-license.php
  29. */
  30. class Rediska_Zend_Auth_Adapter_Redis extends Rediska_Options_RediskaInstance implements Zend_Auth_Adapter_Interface
  31. {
  32. /**
  33. * User identity
  34. *
  35. * @var string
  36. */
  37. protected $_identity;
  38. /**
  39. * User credential
  40. *
  41. * @var string
  42. */
  43. protected $_credential;
  44. /**
  45. * User data
  46. *
  47. * @var array|object
  48. */
  49. protected $_userData;
  50. /**
  51. * Exception class name for options
  52. *
  53. * @var string
  54. */
  55. protected $_optionsException = 'Zend_Auth_Adapter_Exception';
  56. /**
  57. * Configuration
  58. *
  59. * userIdKey - Redis key where you store relation between login and id. '*' replaced to identity (login)
  60. * userDataKey - Redis key where you store user data
  61. * credentialAttributeName - Name of credential (password) attribute in user data
  62. * userDataIsArray - Set true if you store user data in associative array
  63. * identity - User identity (login) for authorization
  64. * credential - User credintial (password) for authorization
  65. * rediska - Rediska instance name, Rediska object or array of options
  66. *
  67. * @var array
  68. */
  69. protected $_options = array(
  70. 'userIdKey' => 'user_ids:*',
  71. 'userDataKey' => 'users:*',
  72. 'credentialAttributeName' => 'password',
  73. 'userDataIsArray' => false,
  74. );
  75. /**
  76. * Set identity (login)
  77. *
  78. * @param string $credential Identity (login)
  79. * @return Rediska_Zend_Auth_Adapter_Redis
  80. */
  81. public function setIdentity($identity)
  82. {
  83. $this->_identity = $identity;
  84. return $this;
  85. }
  86. /**
  87. * Get identity (login)
  88. *
  89. * @return string
  90. */
  91. public function getIdentity()
  92. {
  93. return $this->_identity;
  94. }
  95. /**
  96. * Set credential (password)
  97. *
  98. * @param string $credential Credential (password)
  99. * @return Rediska_Zend_Auth_Adapter_Redis
  100. */
  101. public function setCredential($credential)
  102. {
  103. $this->_credential = $credential;
  104. return $this;
  105. }
  106. /**
  107. * Get credential (password)
  108. *
  109. * @return string
  110. */
  111. public function getCredential()
  112. {
  113. return $this->_credential;
  114. }
  115. /**
  116. * Get result user data
  117. *
  118. * @return object|array
  119. */
  120. public function getResultUserData()
  121. {
  122. return $this->_userData;
  123. }
  124. /**
  125. * (non-PHPdoc)
  126. * @see Zend_Auth_Adapter_Interface#authenticate()
  127. */
  128. public function authenticate()
  129. {
  130. $identity = $this->getIdentity();
  131. $userIdKey = str_replace('*', $identity, $this->getOption('userIdKey'));
  132. $userId = $this->getRediska()->get($userIdKey);
  133. if (is_null($userId)) {
  134. $code = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  135. $message = 'User with the supplied identity could not be found.';
  136. } else {
  137. $userDataKey = str_replace('*', $userId, $this->getOption('userDataKey'));
  138. $userData = $this->getRediska()->get($userDataKey);
  139. if (is_null($userData)) {
  140. throw new Zend_Auth_Adapter_Exception("User data key '$userDataKey' not found");
  141. }
  142. $credentialAttributeName = $this->getOption('credentialAttributeName');
  143. if ($this->getOption('userDataIsArray')) {
  144. if (!array_key_exists($credentialAttributeName, $userData)) {
  145. throw new Zend_Auth_Adapter_Exception("Credential key with name '$credentialAttributeName' not found in user data");
  146. }
  147. $credential = $userData[$credentialAttributeName];
  148. } else {
  149. if (!isset($userData->$credentialAttributeName)) {
  150. throw new Zend_Auth_Adapter_Exception("Credential attribute with name '$credentialAttributeName' not found in user data");
  151. }
  152. $credential = $userData->$credentialAttributeName;
  153. }
  154. if ($this->getCredential() == $credential) {
  155. $code = Zend_Auth_Result::SUCCESS;
  156. $message = 'Authentication successful.';
  157. $this->_userData = $userData;
  158. } else {
  159. $code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  160. $message = 'Supplied credential is invalid.';
  161. }
  162. }
  163. return new Zend_Auth_Result($code, $identity, array($message));
  164. }
  165. }