shibboleth_upgrade.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Shibboleth;
  3. use \Database;
  4. /**
  5. * Migrate the datatabase. Adds needed fields by Shibboleth to the User table.
  6. * Upgrade is checked at each user login so there is no need to manually run
  7. * an upgrade.
  8. *
  9. * @license see /license.txt
  10. * @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
  11. */
  12. class ShibbolethUpgrade
  13. {
  14. /**
  15. * Create additional fields required by the shibboleth plugin if those
  16. * are missing.
  17. */
  18. public static function update()
  19. {
  20. static $done = false;
  21. if ($done)
  22. {
  23. return false;
  24. }
  25. $done = true;
  26. self::create_shibb_unique_id_field_if_missing();
  27. self::create_shibb_persistent_id_field_if_missing();
  28. }
  29. /**
  30. * Creates the 'shibb_unique_id' field in the table 'user' of the main Chamilo database if it doesn't exist yet
  31. *
  32. * @author Nicolas Rod
  33. * @return void
  34. */
  35. public static function create_shibb_unique_id_field_if_missing()
  36. {
  37. $db_name = Database :: get_main_database();
  38. $sql = "SELECT * FROM `$db_name`.`user` LIMIT 1";
  39. $result = Database::query($sql);
  40. $row = mysql_fetch_assoc($result);
  41. $exists = array_key_exists('shibb_unique_id', $row);
  42. if ($exists)
  43. {
  44. return false;
  45. }
  46. //create the 'shibb_unique_id' field
  47. $sql = "ALTER TABLE `$db_name`.`user` ADD `shibb_unique_id` VARCHAR( 60 ) AFTER `auth_source`";
  48. $result_alter = Database::query($sql);
  49. /*
  50. * Index cannot be a UNIQUE index as it may exist users which don't log in through Shibboleth
  51. * and therefore don't have any value for 'shibb_unique_id'
  52. */
  53. $sql = "ALTER TABLE `$db_name`.`user` ADD INDEX ( `shibb_unique_id` )";
  54. $result_alter = Database::query($sql);
  55. }
  56. public static function create_shibb_persistent_id_field_if_missing()
  57. {
  58. $db_name = Database :: get_main_database();
  59. $sql = "SELECT * FROM $db_name.user LIMIT 1";
  60. $result = Database::query($sql);
  61. $row = mysql_fetch_assoc($result);
  62. $exists = array_key_exists('shibb_persistent_id', $row);
  63. if ($exists)
  64. {
  65. return false;
  66. }
  67. $sql = "ALTER table $db_name.user ADD COLUMN shibb_persistent_id varchar(255) NULL DEFAULT NULL;";
  68. $result = Database::query($sql);
  69. return (bool) $result;
  70. }
  71. }