Source for file SQLDatabaseAuthNMethod.class.php

Documentation is available at SQLDatabaseAuthNMethod.class.php

  1. <?php
  2. /**
  3. * @package harmoni.osid_v2.agentmanagement.authn_methods
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: SQLDatabaseAuthNMethod.class.php,v 1.13 2007/09/04 20:25:37 adamfranco Exp $
  9. */
  10. require_once(dirname(__FILE__)."/AuthNMethod.abstract.php");
  11.  
  12. /**
  13. * The SQLDatabaseAuthNMethod is used to authenticate against a SQL database.
  14. *
  15. * @package harmoni.osid_v2.agentmanagement.authn_methods
  16. *
  17. * @copyright Copyright &copy; 2005, Middlebury College
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  19. *
  20. * @version $Id: SQLDatabaseAuthNMethod.class.php,v 1.13 2007/09/04 20:25:37 adamfranco Exp $
  21. */
  22. class SQLDatabaseAuthNMethod
  23. extends AuthNMethod
  24. {
  25. /**
  26. * Stores the configuration. Calls the parent configuration first,
  27. * then does additional operations.
  28. *
  29. * @param object Properties $configuration
  30. * @return object
  31. * @access public
  32. * @since 3/24/05
  33. */
  34. function assignConfiguration ( $configuration ) {
  35. parent::assignConfiguration($configuration);
  36. // Validate the configuration options we use:
  37. ArgumentValidator::validate (
  38. $this->_configuration->getProperty('properties_fields'),
  39. ArrayValidatorRuleWithRule::getRule(StringValidatorRule::getRule()));
  40. ArgumentValidator::validate (
  41. $this->_configuration->getProperty('database_id'),
  42. IntegerValidatorRule::getRule());
  43. ArgumentValidator::validate (
  44. $this->_configuration->getProperty('authentication_table'),
  45. StringValidatorRule::getRule());
  46. ArgumentValidator::validate (
  47. $this->_configuration->getProperty('username_field'),
  48. StringValidatorRule::getRule());
  49. ArgumentValidator::validate (
  50. $this->_configuration->getProperty('password_field'),
  51. StringValidatorRule::getRule());
  52. ArgumentValidator::validate (
  53. $this->_configuration->getProperty('allow_token_addition'),
  54. OptionalRule::getRule(BooleanValidatorRule::getRule()));
  55. ArgumentValidator::validate (
  56. $this->_configuration->getProperty('allow_token_deletion'),
  57. OptionalRule::getRule(BooleanValidatorRule::getRule()));
  58. ArgumentValidator::validate (
  59. $this->_configuration->getProperty('allow_token_updates'),
  60. OptionalRule::getRule(BooleanValidatorRule::getRule()));
  61. ArgumentValidator::validate (
  62. $this->_configuration->getProperty('allow_property_updates'),
  63. OptionalRule::getRule(BooleanValidatorRule::getRule()));
  64. }
  65. /**
  66. * Create a Tokens Object
  67. *
  68. * @return object Tokens
  69. * @access public
  70. * @since 3/1/05
  71. */
  72. function createTokensObject () {
  73. $tokensClass = $this->_configuration->getProperty('tokens_class');
  74. $newTokens = new $tokensClass($this->_configuration);
  75. $validatorRule = ExtendsValidatorRule::getRule('UsernamePasswordAuthNTokens');
  76. if ($validatorRule->check($newTokens))
  77. return $newTokens;
  78. else
  79. throwError( new Error("Configuration Error: tokens_class, '".$tokensClass."' does not extend UsernamePasswordAuthNTokens.",
  80. "SQLDatabaseAuthNMethod", true));
  81. }
  82. /**
  83. * Authenticate an AuthNTokens object
  84. *
  85. * @param object AuthNTokens $authNTokens
  86. * @return boolean
  87. * @access public
  88. * @since 3/1/05
  89. */
  90. function authenticateTokens ( $authNTokens ) {
  91. ArgumentValidator::validate($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  92. $dbc = Services::getService("DatabaseManager");
  93. $dbId = $this->_configuration->getProperty('database_id');
  94. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  95. $usernameField = $this->_configuration->getProperty('username_field');
  96. $passwordField = $this->_configuration->getProperty('password_field');
  97. $query = new SelectQuery;
  98. $query->addTable($authenticationTable);
  99. $query->addColumn("COUNT(*)", "count");
  100. $query->addWhere(
  101. $usernameField."='".addslashes($authNTokens->getUsername())."'");
  102. $query->addWhere(
  103. $passwordField."='".addslashes($authNTokens->getPassword())."'", _AND);
  104. $result = $dbc->query($query, $dbId);
  105. if ($result->field("count") == 1) {
  106. $result->free();
  107. return TRUE;
  108. }
  109. else if ($result->field("count") == 0) {
  110. $result->free();
  111. return FALSE;
  112. }
  113. else
  114. throwError( new Error("Authorization Error: "
  115. .$result->field("count")
  116. ." results were returned when authenticating '"
  117. .$authNTokens->getUsername()."'; should be 0 or 1.",
  118. "SQLDatabaseAuthNMethod", true));
  119. }
  120. /**
  121. * Return true if the tokens can be matched in the system.
  122. *
  123. * @param object AuthNTokens $authNTokens
  124. * @return boolean
  125. * @access public
  126. * @since 3/1/05
  127. */
  128. function tokensExist ( $authNTokens ) {
  129. ArgumentValidator::validate($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  130. $dbc = Services::getService("DatabaseManager");
  131. $dbId = $this->_configuration->getProperty('database_id');
  132. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  133. $usernameField = $this->_configuration->getProperty('username_field');
  134. $query = new SelectQuery;
  135. $query->addTable($authenticationTable);
  136. $query->addColumn("COUNT(*)", "count");
  137. $query->addWhere(
  138. $usernameField."='".addslashes($authNTokens->getUsername())."'");
  139. $result = $dbc->query($query, $dbId);
  140. if ($result->field("count") == 1) {
  141. $result->free();
  142. return TRUE;
  143. }
  144. else if ($result->field("count") == 0) {
  145. $result->free();
  146. return FALSE;
  147. }
  148. else
  149. throwError( new Error("Authorization Error: "
  150. .$result->field("count")
  151. ." results were returned when checking the existance of '"
  152. .$authNTokens->getUsername()."'; should be 0 or 1.",
  153. "SQLDatabaseAuthNMethod", true));
  154. }
  155. /**
  156. * A private method used to populate the Properties that correspond to the
  157. * given AuthNTokens
  158. *
  159. * @param object AuthNTokens $authNTokens
  160. * @param object Properties $properties
  161. * @return void
  162. * @access private
  163. * @since 3/1/05
  164. */
  165. function _populateProperties ( $authNTokens, $properties ) {
  166. ArgumentValidator::validate($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  167. ArgumentValidator::validate($properties, ExtendsValidatorRule::getRule("Properties"));
  168. $dbc = Services::getService("DatabaseManager");
  169. $dbId = $this->_configuration->getProperty('database_id');
  170. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  171. $usernameField = $this->_configuration->getProperty('username_field');
  172. $propertiesFields =$this->_configuration->getProperty('properties_fields');
  173. // if we aren't looking for any properties from the database, don't
  174. // bother running a query.
  175. if (!is_array($propertiesFields) || !count($propertiesFields))
  176. return;
  177. $query = new SelectQuery;
  178. $query->addTable($authenticationTable);
  179. foreach ($propertiesFields as $propertyKey => $fieldName) {
  180. $query->addColumn($fieldName);
  181. }
  182. $query->addWhere(
  183. $usernameField."='".addslashes($authNTokens->getUsername())."'");
  184. $result =$dbc->query($query, $dbId);
  185. if ($result->getNumberOfRows() == 1) {
  186. foreach ($propertiesFields as $propertyKey => $fieldName) {
  187. // Properties take values by reference, so we have to work around
  188. // that by creating/unsetting variables.
  189. $value = $result->field($fieldName);
  190. $properties->addProperty($propertyKey, $value);
  191. unset($value);
  192. }
  193. $result->free();
  194. } else if ($result->getNumberOfRows() == 0) {
  195. $result->free();
  196. return;
  197. }
  198. else
  199. throwError( new Error("Authorization Error: "
  200. .$result->getNumberOfRows()
  201. ." results were returned when trying to populate the properties for '"
  202. .$authNTokens->getUsername()."'; should be 0 or 1.",
  203. "SQLDatabaseAuthNMethod", true));
  204. }
  205. /**
  206. * Get an iterator of the AuthNTokens that match the search string passed.
  207. * The '*' wildcard character can be present in the string and will be
  208. * converted to the system wildcard for the AuthNMethod if wildcards are
  209. * supported or removed (and the exact string searched for) if they are not
  210. * supported.
  211. *
  212. * When multiple fields are searched on an OR search is performed, i.e.
  213. * '*ach*' would match username/fullname 'achapin'/'Chapin, Alex' as well as
  214. * 'zsmith'/'Smith, Zach'.
  215. *
  216. * @param string $searchString
  217. * @return object ObjectIterator
  218. * @access public
  219. * @since 3/3/05
  220. */
  221. function getTokensBySearch ( $searchString ) {
  222. $dbc = Services::getService("DatabaseManager");
  223. $dbId = $this->_configuration->getProperty('database_id');
  224. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  225. $usernameField = $this->_configuration->getProperty('username_field');
  226. $propertiesFields =$this->_configuration->getProperty('properties_fields');
  227. $searchString = str_replace('*', '%', $searchString);
  228. $query = new SelectQuery;
  229. $query->addTable($authenticationTable);
  230. $query->addColumn($usernameField);
  231. $query->addWhere(
  232. $usernameField." LIKE ('".addslashes($searchString)."')", _OR);
  233. if (is_array($propertiesFields) && count($propertiesFields)) {
  234. foreach ($propertiesFields as $propertyKey => $fieldName) {
  235. $query->addWhere(
  236. $fieldName." LIKE ('".addslashes($searchString)."')", _OR);
  237. }
  238. }
  239. $result =$dbc->query($query, $dbId);
  240. $tokens = array();
  241. while ($result->hasMoreRows()) {
  242. $tokens[] =$this->createTokensForIdentifier($result->field($usernameField));
  243. $result->advanceRow();
  244. }
  245. $result->free();
  246. $obj = new HarmoniObjectIterator($tokens);
  247. return $obj;
  248. }
  249. /**
  250. * Return TRUE if this method supports token addition.
  251. *
  252. * @return boolean
  253. * @access public
  254. * @since 3/1/05
  255. */
  256. function supportsTokenAddition () {
  257. if ($this->_configuration->getProperty('allow_token_addition') !== NULL)
  258. return $this->_configuration->getProperty('allow_token_addition');
  259. else
  260. return TRUE;
  261. }
  262. /**
  263. * Add tokens to the system.
  264. *
  265. * @param object AuthNTokens $authNTokens
  266. * @return void
  267. * @access public
  268. * @since 3/1/05
  269. */
  270. function addTokens ( $authNTokens ) {
  271. ArgumentValidator::validate($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  272. if ($this->tokensExist($authNTokens)) {
  273. throwError( new Error("Token Addition Error: ".
  274. "'".$authNTokens->getUsername()."' already exists.",
  275. "SQLDatabaseAuthNMethod", true));
  276. } else {
  277. $dbc = Services::getService("DatabaseManager");
  278. $dbId = $this->_configuration->getProperty('database_id');
  279. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  280. $usernameField = $this->_configuration->getProperty('username_field');
  281. $passwordField = $this->_configuration->getProperty('password_field');
  282. $query = new InsertQuery;
  283. $query->setTable($authenticationTable);
  284. $query->setColumns(array(
  285. $usernameField,
  286. $passwordField
  287. ));
  288. $query->addRowOfValues(array(
  289. "'".addslashes($authNTokens->getUsername())."'",
  290. "'".addslashes($authNTokens->getPassword())."'"
  291. ));
  292. $result = $dbc->query($query, $dbId);
  293. }
  294. }
  295. /**
  296. * Return TRUE if this method supports token deletion.
  297. *
  298. * @return boolean
  299. * @access public
  300. * @since 3/1/05
  301. */
  302. function supportsTokenDeletion () {
  303. if ($this->_configuration->getProperty('allow_token_deletion') !== NULL)
  304. return $this->_configuration->getProperty('allow_token_deletion');
  305. else
  306. return TRUE;
  307. }
  308. /**
  309. * Add tokens and associated Properties to the system.
  310. *
  311. * @param object AuthNTokens $authNTokens
  312. * @return void
  313. * @access public
  314. * @since 3/1/05
  315. */
  316. function deleteTokens ( $authNTokens ) {
  317. ArgumentValidator::validate($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  318. if (!$this->tokensExist($authNTokens)) {
  319. throwError( new Error("Token Deletion Error: "
  320. ."'".$authNTokens->getUsername()."' does not exist.",
  321. "SQLDatabaseAuthNMethod", true));
  322. } else {
  323. $dbc = Services::getService("DatabaseManager");
  324. $dbId = $this->_configuration->getProperty('database_id');
  325. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  326. $usernameField = $this->_configuration->getProperty('username_field');
  327. $passwordField = $this->_configuration->getProperty('password_field');
  328. $query = new DeleteQuery;
  329. $query->setTable($authenticationTable);
  330. $query->addWhere(
  331. $usernameField."='".addslashes($authNTokens->getUsername())."'");
  332. $result = $dbc->query($query, $dbId);
  333. }
  334. }
  335. /**
  336. * Return TRUE if this method supports token updates.
  337. *
  338. * @return boolean
  339. * @access public
  340. * @since 3/1/05
  341. */
  342. function supportsTokenUpdates () {
  343. if ($this->_configuration->getProperty('allow_token_updates') !== NULL)
  344. return $this->_configuration->getProperty('allow_token_updates');
  345. else
  346. return TRUE;
  347. }
  348. /**
  349. * Update old tokens to new tokens in the system.
  350. *
  351. * @param object AuthNTokens $oldAuthNTokens
  352. * @param object AuthNTokens $newAuthNTokens
  353. * @return void
  354. * @access public
  355. * @since 3/1/05
  356. */
  357. function updateTokens ( $oldAuthNTokens, $newAuthNTokens ) {
  358. ArgumentValidator::validate($oldAuthNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  359. ArgumentValidator::validate($newAuthNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  360. if (!$this->tokensExist($oldAuthNTokens)) {
  361. throwError( new Error("Token Update Error: "
  362. ."'".$oldAuthNTokens->getUsername()."' does not exist.",
  363. "SQLDatabaseAuthNMethod", true));
  364. } else {
  365. $dbc = Services::getService("DatabaseManager");
  366. $dbId = $this->_configuration->getProperty('database_id');
  367. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  368. $usernameField = $this->_configuration->getProperty('username_field');
  369. $passwordField = $this->_configuration->getProperty('password_field');
  370. $query = new UpdateQuery;
  371. $query->setTable($authenticationTable);
  372. $query->setColumns(array(
  373. $usernameField,
  374. $passwordField
  375. ));
  376. $query->setValues(array(
  377. "'".addslashes($newAuthNTokens->getUsername())."'",
  378. "'".addslashes($newAuthNTokens->getPassword())."'"
  379. ));
  380. $query->addWhere(
  381. $usernameField."='".addslashes($oldAuthNTokens->getUsername())."'");
  382. $result = $dbc->query($query, $dbId);
  383. }
  384. }
  385. /**
  386. * Return TRUE if this method supports property updates.
  387. *
  388. * @return boolean
  389. * @access public
  390. * @since 3/1/05
  391. */
  392. function supportsPropertyUpdates () {
  393. if ($this->_configuration->getProperty('allow_property_updates') !== NULL)
  394. return $this->_configuration->getProperty('allow_property_updates');
  395. else
  396. return TRUE;
  397. }
  398. /**
  399. * Update the properties for the given tokens
  400. *
  401. * @param object AuthNTokens $authNTokens
  402. * @param object Properties $newProperties
  403. * @return void
  404. * @access public
  405. * @since 3/1/05
  406. */
  407. function updatePropertiesForTokens ( $authNTokens, $newProperties ) {
  408. ArgumentValidator::validate($authNTokens, ExtendsValidatorRule::getRule("AuthNTokens"));
  409. ArgumentValidator::validate($newProperties, ExtendsValidatorRule::getRule("Properties"));
  410. if (!$this->tokensExist($authNTokens)) {
  411. throwError( new Error("Properties Update Error: "
  412. ."'".$authNTokens->getUsername()."' does not exist.",
  413. "SQLDatabaseAuthNMethod", true));
  414. } else {
  415. $dbc = Services::getService("DatabaseManager");
  416. $dbId = $this->_configuration->getProperty('database_id');
  417. $authenticationTable = $this->_configuration->getProperty('authentication_table');
  418. $usernameField = $this->_configuration->getProperty('username_field');
  419. $passwordField = $this->_configuration->getProperty('password_field');
  420. $propertiesFields =$this->_configuration->getProperty('properties_fields');
  421. if (!is_array($propertiesFields) || !count($propertiesFields))
  422. return;
  423. $query = new UpdateQuery;
  424. $query->setTable($authenticationTable);
  425. $columns = array();
  426. $values = array();
  427. foreach ($propertiesFields as $propertyKey => $fieldName) {
  428. // Don't allow overwriting of tokens even if they are listed in the
  429. // properties array.
  430. if ($fieldName != $usernameField
  431. && $fieldName != $passwordField)
  432. {
  433. $columns[] = $fieldName;
  434. $values[] = "'"
  435. .addslashes($newProperties->getProperty($propertyKey))."'";
  436. }
  437. }
  438. $query->setColumns($columns);
  439. $query->setValues($values);
  440. $query->addWhere(
  441. $usernameField."='".addslashes($authNTokens->getUsername())."'");
  442. $result = $dbc->query($query, $dbId);
  443. }
  444. }
  445. }
  446.  
  447. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:39 -0400 by phpDocumentor 1.3.0RC3