ADAuthAdapter.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * This is the packaged Active Directory authorization adapter. This adapter requires the following
  4. * settings to be included in the application.ini or the auth.ini file:
  5. *
  6. * enabled - Set to 1 or 0 to enable or disable authentication. 1 is the default setting, if excluded.
  7. * adapter - Tells the Staple_Main class which AuthAdapter to load.
  8. * authtable - (optional) Specifies the database table where auth credentials reside.
  9. * uidfield - (optional) Defines the username or user identifer field.
  10. *
  11. *
  12. * @author Hans Heeling
  13. * @copyright Copywrite (c) 2011, STAPLE CODE
  14. *
  15. * This file is part of the STAPLE Framework.
  16. *
  17. * The STAPLE Framework is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by the
  19. * Free Software Foundation, either version 3 of the License, or (at your option)
  20. * any later version.
  21. *
  22. * The STAPLE Framework is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  24. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  25. * more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. */
  31. class Staple_ADAuthAdapter implements Staple_AuthAdapter
  32. {
  33. /**
  34. *
  35. * Enter description here ...
  36. * @var array
  37. */
  38. private $_settings = array();
  39. /**
  40. *
  41. * Store the user identifier. Usually the username.
  42. * @var string
  43. */
  44. private $uid;
  45. /**
  46. *
  47. * The constructor loads and checks the adapter configuration.
  48. * @throws Exception
  49. */
  50. public function __construct()
  51. {
  52. if(file_exists(CONFIG_ROOT.'application.ini'))
  53. {
  54. $curConfig = parse_ini_file(CONFIG_ROOT.'application.ini',true);
  55. if($this->checkConfig($curConfig['auth']))
  56. {
  57. $this->_settings = $curConfig['auth'];
  58. }
  59. }
  60. elseif(file_exists(CONFIG_ROOT.'auth.ini'))
  61. {
  62. $curConfig = parse_ini_file(CONFIG_ROOT.'auth.ini');
  63. if($this->checkConfig($curConfig))
  64. {
  65. $this->_settings = $curConfig;
  66. }
  67. }
  68. else
  69. {
  70. throw new Exception('Staple_ADAuthAdapter critical failure.',500);
  71. }
  72. }
  73. /**
  74. * getAuth checks Active Directory for valid credentials and returns true if they are found.
  75. * @param array $cred
  76. * @return bool
  77. * @see Staple_AuthAdapter::getAuth()
  78. */
  79. public function getAuth($cred)
  80. {
  81. if($this->checkConfig($this->_settings))
  82. {
  83. if(array_key_exists('username', $cred) AND array_key_exists('password', $cred))
  84. {
  85. if(strlen($cred['username']) >= 1 && strlen($cred['password']) >= 1)
  86. {
  87. if(Staple_AD::validchars($cred['username']) == TRUE && Staple_AD::validchars($cred['password']) == TRUE)
  88. {
  89. $pass = $cred['password'];
  90. $LDAP = Staple_AD::get();
  91. $this->uid = $cred['username'];
  92. if($LDAP->bind($this->uid, $pass))
  93. {
  94. return true;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Gets the access level for the supplied $uid.
  104. * @param string $uid
  105. * @return int
  106. * @see Staple_AuthAdapter::getLevel()
  107. */
  108. public function getLevel($uid)
  109. {
  110. if($this->checkConfig($this->_settings))
  111. {
  112. if(array_key_exists('rolefield', $this->_settings))
  113. {
  114. $db = Staple_DB::get();
  115. $sql = 'SELECT '.$db->real_escape_string($this->_settings['rolefield']).'
  116. FROM '.$db->real_escape_string($this->_settings['authtable']).'
  117. WHERE '.$db->real_escape_string($this->_settings['uidfield']).' = '.
  118. '\''.$db->real_escape_string($uid).'\';';
  119. $result = $db->query($sql);
  120. if($result !== false)
  121. {
  122. $myrow = $result->fetch_array();
  123. $level = (int)$myrow[$this->_settings['rolefield']];
  124. if($level < 0)
  125. {
  126. return 0;
  127. }
  128. else
  129. {
  130. return $level;
  131. }
  132. }
  133. else
  134. {
  135. return 0;
  136. }
  137. }
  138. else
  139. {
  140. return 1;
  141. }
  142. }
  143. }
  144. /**
  145. *
  146. * Checks the configuration fields for validity
  147. * @param array $config
  148. * @throws Exception
  149. */
  150. protected function checkConfig(array $config)
  151. {
  152. $keys = array('enabled','adapter');
  153. foreach($keys as $value)
  154. {
  155. if(!array_key_exists($value, $config))
  156. {
  157. throw new Exception('Staple_ADAuthAdapter configuration error.',Staple_Error::AUTH_ERROR);
  158. }
  159. }
  160. if($config['adapter'] != get_class($this))
  161. {
  162. throw new Exception('Staple_ADAuthAdapter configuration error.',Staple_Error::AUTH_ERROR);
  163. }
  164. return true;
  165. }
  166. /**
  167. * Returns the User ID from the adapter.
  168. * @return string
  169. * @see Staple_AuthAdapter::getUserId()
  170. */
  171. public function getUserId()
  172. {
  173. return $this->uid;
  174. }
  175. }
  176. ?>