authcas.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/external_login/ldap.inc.php';
  8. require_once api_get_path(SYS_PATH).'main/auth/external_login/functions.inc.php';
  9. /**
  10. * @return true if cas is configured
  11. *
  12. **/
  13. function cas_configured()
  14. {
  15. global $cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri;
  16. $res = false;
  17. if (!empty($cas_auth_ver) && !empty($cas_auth_server) && !empty($cas_auth_port)) {
  18. $res = true;
  19. }
  20. return $res;
  21. }
  22. /**
  23. * checks if the user already get a session
  24. * @return the user login if the user already has a session ,false otherwise
  25. **/
  26. function cas_is_authenticated()
  27. {
  28. global $cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri;
  29. global $PHPCAS_CLIENT;
  30. global $logout;
  31. if (!cas_configured()) {
  32. return;
  33. }
  34. if (!is_object($PHPCAS_CLIENT)) {
  35. phpCAS::client($cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri);
  36. phpCAS::setNoCasServerValidation();
  37. }
  38. $auth = phpCAS::checkAuthentication();
  39. if ($auth) {
  40. $login = trim(phpCAS::getUser());
  41. /*
  42. Get user attributes. Here are the attributes for crdp platform
  43. sn => name
  44. ENTPersonMailInterne => mail
  45. ENTPersonAlias => login
  46. ENTPersonProfils => profil
  47. givenName => first name
  48. */
  49. /*$user=phpCAS::getAttributes();
  50. $firstName = trim($user['givenName']);
  51. $lastName = trim($user['sn']);
  52. $login = trim($user['ENTPersonAlias']);
  53. $profil = trim($user['ENTPersonProfils']);
  54. $email = trim($user['ENTPersonMailInterne']);
  55. $satus=5;
  56. switch ($profil){
  57. case 'admin_etab':
  58. $status=3; //Session admin
  59. break;
  60. case 'admin_sie':
  61. $status=3; //Session admin
  62. break;
  63. case 'National_3':
  64. $status=1; // Teacher
  65. break;
  66. case 'National_1':
  67. $status=5; // Student
  68. break;
  69. default:
  70. $status=5; // Student
  71. }*/
  72. if (!$logout) {
  73. // get user info from username
  74. $tab_user_info = api_get_user_info($login);
  75. // user found in the chamilo database
  76. if (is_array($tab_user_info)) {
  77. // if option is on we update user automatically from ldap server
  78. if (api_get_setting("update_user_info_cas_with_ldap") == "true") {
  79. $ldapuser = extldap_authenticate($login, 'nopass', true);
  80. if ($ldapuser !== false) {
  81. $chamilo_user = extldap_get_chamilo_user($ldapuser);
  82. $chamilo_user['user_id'] = $tab_user_info['user_id'];
  83. $chamilo_user['status'] = $tab_user_info['status'];
  84. UserManager::update_user($chamilo_user["user_id"], $chamilo_user["firstname"],
  85. $chamilo_user["lastname"], $login, null, null, $chamilo_user["email"],
  86. $chamilo_user["status"], '', '', '', '', 1, null, 0, null, '');
  87. }
  88. }
  89. return $login;
  90. } // user not found
  91. else {
  92. // if option is on we can ADD user automatically from ldap server or by modify own profil
  93. $user_added = false;
  94. switch (api_get_setting("cas_add_user_activate")) {
  95. case PLATFORM_AUTH_SOURCE :
  96. // user will have to modify firstname, lastname, email in chamilo profil edit
  97. $userdata = get_lang("EditInProfil");
  98. UserManager::create_user($userdata, $userdata, '5', $userdata, $login, 'casplaceholder', '', '',
  99. '', '', CAS_AUTH_SOURCE);
  100. $user_added = $login;
  101. break;
  102. case LDAP_AUTH_SOURCE :
  103. // user info are read from ldap connexion
  104. // get user info from ldap server
  105. // user has already been authenticated by CAS
  106. // If user not found in LDAP, user not created
  107. $ldapuser = extldap_authenticate($login, 'nopass', true);
  108. if ($ldapuser !== false) {
  109. $chamilo_user = extldap_get_chamilo_user($ldapuser);
  110. $chamilo_user['username'] = $login;
  111. $chamilo_user['auth_source'] = CAS_AUTH_SOURCE;
  112. $chamilo_uid = external_add_user($chamilo_user);
  113. $user_added = $login;
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. return $user_added;
  120. }
  121. }
  122. // //If the user is in the dokeos database and we are ,not in a logout request, we upgrade his infomration by ldap
  123. // if (! $logout){
  124. // $user_table = Database::get_main_table(TABLE_MAIN_USER);
  125. // $sql = "SELECT user_id, username, password, auth_source, active, expiration_date ".
  126. // "FROM $user_table ".
  127. // "WHERE username = '$login' ";
  128. //
  129. // $result = Database::query($sql,__FILE__,__LINE__);
  130. // if(mysql_num_rows($result) == 0) {
  131. // require_once(api_get_path(SYS_PATH).'main/inc/lib/usermanager.lib.php');
  132. // $rnumber=rand(0,256000);
  133. // UserManager::create_user($firstName, $lastName, $status, $email, $login, md5('casplaceholder'.$rnumber), $official_code='',$language='',$phone='',$picture_uri='',$auth_source = PLATFORM_AUTH_SOURCE);
  134. // }
  135. // else {
  136. // $user = mysql_fetch_assoc($result);
  137. // $user_id = intval($user['user_id']);
  138. // //echo "deb : $status";
  139. // UserManager::update_user ($user_id, $firstname, $lastname, $login, null, null, $email, $status, '', '', '', '', 1, null, 0, null,'') ;
  140. //
  141. // }
  142. // }
  143. return $login;
  144. } else {
  145. return false;
  146. }
  147. }
  148. /**
  149. * Logs out the user of the cas
  150. * The user MUST be logged in with cas to use this function
  151. *
  152. * @param $uinfo array user info (not needed)
  153. * @param $location string redirect url
  154. *
  155. * @see online_logout()
  156. */
  157. function cas_logout($uinfo = null, $location = null)
  158. {
  159. global $cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri;
  160. global $PHPCAS_CLIENT;
  161. if (!is_object($PHPCAS_CLIENT)) {
  162. phpCAS::client($cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri);
  163. phpCAS::setNoCasServerValidation();
  164. }
  165. if (!isset($location)) {
  166. $location = api_get_path(WEB_PATH);
  167. }
  168. phpCAS::logoutWithRedirectService($location);
  169. }
  170. /*
  171. * Return the direct URL to a course code with CAS login
  172. */
  173. function get_cas_direct_URL($in_course_code)
  174. {
  175. return api_get_path(WEB_PATH).'main/auth/cas/logincas.php?firstpage='.$in_course_code;
  176. }
  177. function getCASLogoHTML()
  178. {
  179. $out_res = "";
  180. if (api_get_setting("casLogoURL") != "") {
  181. $out_res = "<img src='".api_get_setting("casLogoURL")."' alt='CAS Logo' />";
  182. }
  183. return $out_res;
  184. }