authcas.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. function cas_configured()
  13. {
  14. global $cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri;
  15. $res = false;
  16. if (!empty($cas_auth_ver) && !empty($cas_auth_server) && !empty($cas_auth_port)) {
  17. $res = true;
  18. }
  19. return $res;
  20. }
  21. /**
  22. * checks if the user already get a session.
  23. *
  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(
  85. $chamilo_user["user_id"],
  86. $chamilo_user["firstname"],
  87. $chamilo_user["lastname"],
  88. $login,
  89. null,
  90. null,
  91. $chamilo_user["email"],
  92. $chamilo_user["status"],
  93. '',
  94. '',
  95. '',
  96. '',
  97. 1,
  98. null,
  99. 0,
  100. null,
  101. ''
  102. );
  103. }
  104. }
  105. return $login;
  106. } // user not found
  107. else {
  108. // if option is on we can ADD user automatically from ldap server or by modify own profil
  109. $user_added = false;
  110. switch (api_get_setting("cas_add_user_activate")) {
  111. case PLATFORM_AUTH_SOURCE:
  112. // user will have to modify firstname, lastname, email in chamilo profil edit
  113. $userdata = get_lang("EditInProfil");
  114. UserManager::create_user(
  115. $userdata,
  116. $userdata,
  117. '5',
  118. $userdata,
  119. $login,
  120. 'casplaceholder',
  121. '',
  122. '',
  123. '',
  124. '',
  125. CAS_AUTH_SOURCE
  126. );
  127. $user_added = $login;
  128. break;
  129. case LDAP_AUTH_SOURCE:
  130. // user info are read from ldap connexion
  131. // get user info from ldap server
  132. // user has already been authenticated by CAS
  133. // If user not found in LDAP, user not created
  134. $ldapuser = extldap_authenticate($login, 'nopass', true);
  135. if ($ldapuser !== false) {
  136. $chamilo_user = extldap_get_chamilo_user($ldapuser);
  137. $chamilo_user['username'] = $login;
  138. $chamilo_user['auth_source'] = CAS_AUTH_SOURCE;
  139. $chamilo_uid = external_add_user($chamilo_user);
  140. $user_added = $login;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. return $user_added;
  147. }
  148. }
  149. // //If the user is in the dokeos database and we are ,not in a logout request, we upgrade his infomration by ldap
  150. // if (! $logout){
  151. // $user_table = Database::get_main_table(TABLE_MAIN_USER);
  152. // $sql = "SELECT user_id, username, password, auth_source, active, expiration_date ".
  153. // "FROM $user_table ".
  154. // "WHERE username = '$login' ";
  155. //
  156. // $result = Database::query($sql,__FILE__,__LINE__);
  157. // if(mysql_num_rows($result) == 0) {
  158. // require_once(api_get_path(SYS_PATH).'main/inc/lib/usermanager.lib.php');
  159. // $rnumber=rand(0,256000);
  160. // UserManager::create_user($firstName, $lastName, $status, $email, $login, md5('casplaceholder'.$rnumber), $official_code='',$language='',$phone='',$picture_uri='',$auth_source = PLATFORM_AUTH_SOURCE);
  161. // }
  162. // else {
  163. // $user = mysql_fetch_assoc($result);
  164. // $user_id = intval($user['user_id']);
  165. // //echo "deb : $status";
  166. // UserManager::update_user ($user_id, $firstname, $lastname, $login, null, null, $email, $status, '', '', '', '', 1, null, 0, null,'') ;
  167. //
  168. // }
  169. // }
  170. return $login;
  171. } else {
  172. return false;
  173. }
  174. }
  175. /**
  176. * Logs out the user of the cas
  177. * The user MUST be logged in with cas to use this function.
  178. *
  179. * @param $uinfo array user info (not needed)
  180. * @param $location string redirect url
  181. *
  182. * @see online_logout()
  183. */
  184. function cas_logout($uinfo = null, $location = null)
  185. {
  186. global $cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri;
  187. global $PHPCAS_CLIENT;
  188. if (!is_object($PHPCAS_CLIENT)) {
  189. phpCAS::client($cas_auth_ver, $cas_auth_server, $cas_auth_port, $cas_auth_uri);
  190. phpCAS::setNoCasServerValidation();
  191. }
  192. if (!isset($location)) {
  193. $location = api_get_path(WEB_PATH);
  194. }
  195. phpCAS::logoutWithRedirectService($location);
  196. }
  197. /*
  198. * Return the direct URL to a course code with CAS login
  199. */
  200. function get_cas_direct_URL($in_course_code)
  201. {
  202. return api_get_path(WEB_PATH).'main/auth/cas/logincas.php?firstpage='.$in_course_code;
  203. }
  204. function getCASLogoHTML()
  205. {
  206. $out_res = "";
  207. if (api_get_setting("casLogoURL") != "") {
  208. $out_res = "<img src='".api_get_setting("casLogoURL")."' alt='CAS Logo' />";
  209. }
  210. return $out_res;
  211. }