Advertisement
patrickfabrizius

ObjectAdapter.php (for Pimcore authentication)

Feb 2nd, 2014
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Website_Auth_ObjectAdapter
  5. *
  6. * Website_Auth_ObjectAdapter provides the ability to authenticate against
  7. * credentials stored in an Pimcore object. All configuration options can
  8. * be set through the constructor and through instance methods, one for each
  9. * option.
  10. *
  11. * @version 0.5
  12. */
  13.  
  14. class Website_Auth_ObjectAdapter implements Zend_Auth_Adapter_Interface
  15. {
  16. /**
  17. * $_identityValue - Identity value
  18. *
  19. * @var string
  20. */
  21. protected $_identityValue;
  22.  
  23. /**
  24. * $_credentialValue - Credential value
  25. *
  26. * @var string
  27. */
  28. protected $_credentialValue;
  29.  
  30. /**
  31. * $_identityClassname - Classname of the object
  32. *
  33. * @var string
  34. */
  35. protected $_identityClassname;
  36.  
  37. /**
  38. * $_identityColumn - The column to use as the identity
  39. *
  40. * @var string
  41. */
  42. protected $_identityColumn;
  43.  
  44. /**
  45. * $_credentialColumn - The column to use as the credential
  46. *
  47. * @var string
  48. */
  49. protected $_credentialColumn;
  50.  
  51. /**
  52. * $_objectPath - Path in the object tree where the identity oject is stored
  53. *
  54. * @var string
  55. */
  56. protected $_objectPath;
  57.  
  58. /**
  59. * __construct() - Sets the configaration options
  60. *
  61. * @param string $identityClassname
  62. * @param string $identityColumn
  63. * @param string $credentialColumn
  64. * @param string $objectPath
  65. * @return void
  66. */
  67. public function __construct($identityClassname = null, $identityColumn = null, $credentialColumn = null, $objectPath = null)
  68. {
  69.  
  70. # PF: Changed. Stopped working in Pimcore 1.4+.
  71. # Zend_Db_Table::setDefaultAdapter(Pimcore_Resource_Mysql::get());
  72. Zend_Db_Table::setDefaultAdapter(Pimcore_Resource_Mysql::get()->getResource());
  73.  
  74. if (null !== $identityClassname) {
  75. $this->setIdentityClassname($identityClassname);
  76. }
  77.  
  78. if (null !== $identityColumn) {
  79. $this->setIdentityColumn($identityColumn);
  80. }
  81.  
  82. if (null !== $credentialColumn) {
  83. $this->setCredentialColumn($credentialColumn);
  84. }
  85.  
  86. if (null !== $objectPath) {
  87. $this->setObjectPath($objectPath);
  88. }
  89. }
  90.  
  91. /**
  92. * setIdentityClassname() - set classname of the object
  93. *
  94. * @param string $identityClassname
  95. * @throws Zend_Auth_Adapter_Exception
  96. * @return Website_Auth_ObjectAdapter Provides a fluent interface
  97. */
  98. public function setIdentityClassname($identityClassname)
  99. {
  100. if (!class_exists($identityClassname)) {
  101. throw new Zend_Auth_Adapter_Exception('invalid classname [' . $identityClassname . ']');
  102. }
  103.  
  104. $this->_identityClassname = $identityClassname;
  105. return $this;
  106. }
  107.  
  108. /**
  109. * authenticate() - Performs an authentication attempt
  110. *
  111. * @throws Zend_Auth_Adapter_Exception If authentication cannot be performed
  112. * @return Zend_Auth_Result
  113. */
  114. public function authenticate()
  115. {
  116. $this->_authenticateSetup();
  117.  
  118. $authResultCode = Zend_Auth_Result::FAILURE;
  119. $authResultIdentity = null;
  120. $authResultMessages = array();
  121.  
  122. $identities = $this->_getIdentityFromObject();
  123.  
  124. if (count($identities) == 0) {
  125. $authResultCode = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  126. } elseif (count($identities) == 1) {
  127.  
  128. $identity = $identities->current();
  129. if ($this->_checkCredential($identity)) {
  130. $authResultCode = Zend_Auth_Result::SUCCESS;
  131. $authResultIdentity = $identity;
  132. } else {
  133. $authResultCode = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  134. }
  135.  
  136. } else {
  137. $authResultCode = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
  138. }
  139.  
  140. return new Zend_Auth_Result($authResultCode, $authResultIdentity, $authResultMessages);
  141. }
  142.  
  143. /**
  144. * _authenticateSetup() - This method abstracts the steps involved with
  145. * making sure that this adapter was indeed setup properly with all
  146. * required pieces of information.
  147. *
  148. * @throws Zend_Auth_Adapter_Exception - in the event that setup was not done properly
  149. * @return true
  150. */
  151. protected function _authenticateSetup()
  152. {
  153. $exception = null;
  154.  
  155. if ($this->_identityClassname == '') {
  156. $exception = 'A classname must be supplied for the ' . __CLASS__ . ' authentication adapter.';
  157. } elseif ($this->_identityColumn == '') {
  158. $exception = 'An identity column must be supplied for the ' . __CLASS__ . ' authentication adapter.';
  159. } elseif ($this->_credentialColumn == '') {
  160. $exception = 'A credential column must be supplied for the ' . __CLASS__ . ' authentication adapter.';
  161. } elseif ($this->_identityValue == '') {
  162. $exception = 'A value for the identity was not provided prior to authentication with ' . __CLASS__ . '.';
  163. } elseif ($this->_credentialValue === null) {
  164. $exception = 'A credential value was not provided prior to authentication with ' . __CLASS__ . '.';
  165. }
  166.  
  167. if (null !== $exception) {
  168. throw new Zend_Auth_Adapter_Exception($exception);
  169. }
  170.  
  171. return true;
  172. }
  173.  
  174. /**
  175. * _getIdentityFromObject() - loads the identity from the database
  176. *
  177. * @return Object_List_Concrete
  178. */
  179. protected function _getIdentityFromObject()
  180. {
  181. $className = $this->_identityClassname . '_List';
  182. $objectList = new $className;
  183. $objectList->setCondition($this->_getCondition());
  184. $objectList->load();
  185.  
  186. return $objectList;
  187. }
  188.  
  189. /**
  190. * _getCondition() - build the conditions for getting the identity
  191. *
  192. * @return string
  193. */
  194. protected function _getCondition()
  195. {
  196. $conditions = array();
  197. $conditions[] = Zend_Db_Table::getDefaultAdapter()->quoteInto($this->_identityColumn . ' = ?', $this->_identityValue);
  198.  
  199. if ($this->_objectPath) {
  200. $conditions[] = Zend_Db_Table::getDefaultAdapter()->quoteInto('o_path = ?', $this->_objectPath);
  201. }
  202.  
  203. return implode(' AND ', $conditions);
  204. }
  205.  
  206. /**
  207. * _checkCredential() - This method attempts to validate that
  208. * the record in the result is indeed a record that matched the
  209. * identity provided to this adapter.
  210. *
  211. * @param Object_Concrete $user
  212. * @return bool
  213. */
  214. protected function _checkCredential(Object_Concrete $user)
  215. {
  216. return $user->{$this->_credentialColumn} == md5($this->_credentialValue);
  217. }
  218.  
  219. /**
  220. * setIdentityColumn() - set the column name to be used as the identity column
  221. *
  222. * @param string $identityColumn
  223. * @return Website_Auth_ObjectAdapter Provides a fluent interface
  224. */
  225. public function setIdentityColumn($identityColumn)
  226. {
  227. $this->_identityColumn = $identityColumn;
  228. return $this;
  229. }
  230.  
  231. /**
  232. * setCredentialColumn() - set the column name to be used as the credential column
  233. *
  234. * @param string $credentialColumn
  235. * @return Website_Auth_ObjectAdapter Provides a fluent interface
  236. */
  237. public function setCredentialColumn($credentialColumn)
  238. {
  239. $this->_credentialColumn = $credentialColumn;
  240. return $this;
  241. }
  242.  
  243. /**
  244. * setObjectPath() - sets the path in the object tree where the identity oject is stored.
  245. * This setting is optional, when no path is set the path will not been checked
  246. *
  247. * @param string $objectPath
  248. * @return Website_Auth_ObjectAdapter Provides a fluent interface
  249. */
  250. public function setObjectPath($objectPath)
  251. {
  252. $this->_objectPath = $objectPath;
  253. return $this;
  254. }
  255.  
  256. /**
  257. * setIdentity() - set the value to be used as the identity
  258. *
  259. * @param string $identityValue
  260. * @return Website_Auth_ObjectAdapter Provides a fluent interface
  261. */
  262. public function setIdentity($identityValue)
  263. {
  264. $this->_identityValue = $identityValue;
  265. return $this;
  266. }
  267.  
  268. /**
  269. * setCredential() - set the credential value to be used
  270. *
  271. * @param string $credentialValue
  272. * @return Website_Auth_ObjectAdapter Provides a fluent interface
  273. */
  274. public function setCredential($credentialValue)
  275. {
  276. $this->_credentialValue = $credentialValue;
  277. return $this;
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement