authcas.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*Written by Noel Dieschburg <noel@cblue.be> for the paris5 university
  3. * Checks if the user is already logged in via the cas system
  4. * Gets all the info via the ldap module (ldap has to work)
  5. */
  6. require_once(api_get_path(SYS_PATH).'main/auth/cas/cas_var.inc.php');
  7. require_once(api_get_path(SYS_PATH).'main/auth/ldap/authldap.php');
  8. /**
  9. * checks if the user already get a session
  10. * @return the user login if the user already has a session ,false otherwise
  11. **/
  12. function cas_is_authenticated()
  13. {
  14. global $cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri;
  15. global $PHPCAS_CLIENT;
  16. global $logout;
  17. if (!is_object($PHPCAS_CLIENT) )
  18. {
  19. phpCAS::client($cas_auth_ver,$cas_auth_server,$cas_auth_port,$cas_auth_uri);
  20. // die("phpCAS::client($cas_auth_ver,$cas_auth_server,$cas_auth_port,$cas_auth_uri);");
  21. phpCAS::setNoCasServerValidation();
  22. }
  23. $auth = phpCAS::checkAuthentication();
  24. if ($auth) {
  25. $login= trim(phpCAS::getUser());
  26. /*
  27. Get user attributes. Here are the attributes for crdp platform
  28. sn => name
  29. ENTPersonMailInterne => mail
  30. ENTPersonAlias => login
  31. ENTPersonProfils => profil
  32. givenName => first name
  33. */
  34. /*$user=phpCAS::getAttributes();
  35. $firstName = trim($user['givenName']);
  36. $lastName = trim($user['sn']);
  37. $login = trim($user['ENTPersonAlias']);
  38. $profil = trim($user['ENTPersonProfils']);
  39. $email = trim($user['ENTPersonMailInterne']);
  40. $satus=5;
  41. switch ($profil){
  42. case 'admin_etab':
  43. $status=3; //Session admin
  44. break;
  45. case 'admin_sie':
  46. $status=3; //Session admin
  47. break;
  48. case 'National_3':
  49. $status=1; // Teacher
  50. break;
  51. case 'National_1':
  52. $status=5; // Student
  53. break;
  54. default:
  55. $status=5; // Student
  56. }*/
  57. //If the user is in the dokeos database and we are ,not in a logout request, we upgrade his infomration by ldap
  58. if (! $logout){
  59. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  60. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date ".
  61. "FROM $user_table ".
  62. "WHERE username = '$login' ";
  63. $result = api_sql_query($sql,__FILE__,__LINE__);
  64. if(mysql_num_rows($result) == 0) {
  65. require_once(api_get_path(SYS_PATH).'main/inc/lib/usermanager.lib.php');
  66. $rnumber=rand(0,256000);
  67. UserManager::create_user($firstName, $lastName, $status, $email, $login, md5('casplaceholder'.$rnumber), $official_code='',$language='',$phone='',$picture_uri='',$auth_source = PLATFORM_AUTH_SOURCE);
  68. }
  69. else {
  70. $user = mysql_fetch_assoc($result);
  71. $user_id = intval($user['user_id']);
  72. //echo "deb : $status";
  73. UserManager::update_user ($user_id, $firstname, $lastname, $login, null, null, $email, $status, '', '', '', '', 1, null, 0, null,'') ;
  74. }
  75. }
  76. return($login);
  77. }
  78. else
  79. {
  80. return(false);
  81. }
  82. }
  83. /**
  84. * Logs out the user of the cas
  85. * The user MUST be logged in with cas to use this function
  86. **/
  87. function cas_logout()
  88. {
  89. //phpCAS::logoutWithRedirectService("fmc.univ-paris5.fr");
  90. phpCAS::logoutWithRedirectService(api_get_path(WEB_PATH));
  91. }
  92. ?>