/application/src/Application/Model/Authentication/Innovative.php

https://github.com/dswalker/xerxes · PHP · 211 lines · 99 code · 53 blank · 59 comment · 10 complexity · f8b78871f9d10194398d70797796084f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Xerxes.
  4. *
  5. * (c) California State University <library@calstate.edu>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Application\Model\Authentication;
  11. use Xerxes\Mvc\Exception\AccessDeniedException;
  12. use Xerxes\Utility\Factory;
  13. use Xerxes\Mvc\Request;
  14. /**
  15. * Authenticates users and downloads data from the Innovative Patron API
  16. *
  17. * Based on the functions originally developed by John Blyberg
  18. *
  19. * @author David Walker <dwalker@calstate.edu>
  20. */
  21. class Innovative extends Scheme
  22. {
  23. protected $server;
  24. protected $user_data;
  25. /*
  26. * Create new Innovative authentication
  27. */
  28. public function __construct(Request $request)
  29. {
  30. parent::__construct($request);
  31. $this->server = $this->registry->getConfig( "INNOVATIVE_PATRON_API", true );
  32. $this->server = rtrim($this->server, '/');
  33. }
  34. /**
  35. * Authenticate the user against the III Patron API
  36. */
  37. public function onCallBack()
  38. {
  39. $strUsername = $this->request->getParam( "username" ); // barcode
  40. $strPassword = $this->request->getParam( "password" ); // pin
  41. $bolAuth = $this->authenticate( $strUsername, $strPassword );
  42. $this->user_data = $this->getUserData($strUsername);
  43. // print_r($this->user_data); exit;
  44. if ( $bolAuth == true )
  45. {
  46. // make sure user is in the list of approved patron types
  47. $configPatronTypes = $this->registry->getConfig( "INNOVATIVE_PATRON_TYPES", false );
  48. if ( $configPatronTypes != null )
  49. {
  50. $arrTypes = explode(",", $configPatronTypes);
  51. // make them all integers for consitency
  52. for ( $x = 0; $x < count($arrTypes); $x++ )
  53. {
  54. $arrTypes[$x] = (int) $arrTypes[$x];
  55. }
  56. if ( ! in_array( (int) $this->user_data["P TYPE"], $arrTypes) )
  57. {
  58. throw new AccessDeniedException("User is not authorized to use this service");
  59. }
  60. }
  61. // register the user and stop the flow
  62. $this->user->username = $strUsername;
  63. $this->mapUserData();
  64. return $this->register();
  65. }
  66. else
  67. {
  68. return self::FAILED;
  69. }
  70. }
  71. /**
  72. * Innovative_Local class defines this
  73. */
  74. protected function mapUserData()
  75. {
  76. }
  77. /**
  78. * Returns patron data from the API as array
  79. *
  80. * @param string $id barcode
  81. * @return array data returned by the api as associative array
  82. * @exception throws exception when iii patron api reports error
  83. */
  84. protected function getUserData( $id )
  85. {
  86. // normalize the barcode
  87. $id = str_replace(" ", "", $id);
  88. // fetch data from the api
  89. $url = $this->server . "/PATRONAPI/$id/dump";
  90. $arrData = $this->getContent($url);
  91. // if something went wrong
  92. if ( array_key_exists("ERRMSG", $arrData ) )
  93. {
  94. throw new \Exception($arrData["ERRMSG"]);
  95. }
  96. return $arrData;
  97. }
  98. /**
  99. * Checks tha validity of a barcode / pin combo, essentially a login test
  100. *
  101. * @param string $id barcode
  102. * @param string $pin the pin to use with $id
  103. * @return bool true if valid, false if not
  104. */
  105. protected function authenticate ( $id, $pin )
  106. {
  107. // normalize the barcode and pin
  108. $id = str_replace(" ", "", $id);
  109. $pin = str_replace(" ", "", $pin);
  110. // fetch data from the api
  111. $pin = urlencode($pin);
  112. $url = $this->server . "/PATRONAPI/$id/$pin/pintest";
  113. $arrData = $this->getContent($url);
  114. // check pin test for error message, indicating
  115. // failure
  116. if ( array_key_exists("ERRMSG", $arrData ) )
  117. {
  118. return false;
  119. }
  120. else
  121. {
  122. return true;
  123. }
  124. }
  125. /**
  126. * Fetches and normalize the API data
  127. *
  128. * @param string $url url of patron dump or pint test
  129. * @return array patron data
  130. */
  131. private function getContent( $url )
  132. {
  133. $arrRawData = array();
  134. $arrData = array();
  135. // get the data and strip out html tags
  136. $client = Factory::getHttpClient();
  137. $strResponse = $client->getUrl($url);
  138. $strResponse = trim(strip_tags($strResponse));
  139. if ( $strResponse == "" )
  140. {
  141. throw new \Exception("Could not connect to Innovative Patron API");
  142. }
  143. else
  144. {
  145. // cycle thru each line in the response, splitting each
  146. // on the equal sign into an associative array
  147. $arrRawData = explode("\n", $strResponse);
  148. foreach ($arrRawData as $strLine)
  149. {
  150. $arrLine = explode("=", $strLine);
  151. // strip out the code, leaving just the attribute name
  152. $arrLine[0] = preg_replace('/\[[^\]]{1,}\]/', "", $arrLine[0]);
  153. $arrData[trim($arrLine[0])] = trim( $arrLine[1] );
  154. }
  155. }
  156. return $arrData;
  157. }
  158. }