浏览代码

Conditional login [5233] : generic system for update user data at login. Example with phone number.

Noel Dieschburg 13 年之前
父节点
当前提交
7b82aedbb5

+ 32 - 0
main/auth/conditional_login/complete_phone_number.php

@@ -0,0 +1,32 @@
+<?php
+require_once(dirname(__FILE__).'/../../inc/global.inc.php');
+require_once (api_get_path(LIBRARY_PATH).'conditionallogin.lib.php');
+require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
+$url =  api_get_path(WEB_PATH).'main/auth/conditional_login/complete_phone_number.php';
+
+if (! isset($_SESSION['conditional_login']['uid']))
+  die("Not Authorised");
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html lang="fr" xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
+  </head>
+  <body>
+  <form id="data_completion" name="data_completion" method="post" action="<?php echo $url?>">
+        Téléphone : <input type="text" name="phone_number" />
+        <input type="submit" name="submit" value="Submit" />
+    </form>
+  </body>
+</html>
+<?php
+if (isset($_POST['submit'])){
+  $u = UserManager::get_user_info_by_id($_SESSION['conditional_login']['uid']);
+  $u['phone'] = $_POST['phone_number'];
+  $password = null; // we don't want to change the password 
+  $updated = UserManager::update_user($u['user_id'], $u['firstname'], $u['lastname'], $u['username'], $password, $u['auth_source'], $u['email'], $u['status'], $u['official_code'], $u['phone'], $u['picture_uri'], $u['expiration_date'], $u['active'], $u['creator_id'], $u['hr_dept_id'], $u['extra'], $u['language'],'');
+  if ($updated) {
+    ConditionalLogin::login();
+  }
+}
+?>

+ 23 - 0
main/auth/conditional_login/conditional_login.php

@@ -0,0 +1,23 @@
+<?php
+/*
+ This scrip is included by local.inc.php to redirect users to some url if some conditions are satisfied. Please populate the $dc_conditions array with a conditional function and an url. If the conditional function returns true the user will be redirected to URL at login
+
+ This array must be filled for this module to work. This is an example asking the user to enter his phone number if it is empty. Note you can enter more than one condition in the array. They will be checked in the array order.
+*/
+$dc_conditions = array();
+//array_push($dc_conditions, array(
+//  'conditional_function' => 'dc_check_phone_number',
+//  'url' => api_get_path(WEB_PATH).'main/auth/conditional_login/complete_phone_number.php'
+//));
+require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
+/*
+  Please implements the functions of the $dc_conditions array. Each of these fucntion will take a user array (user_id, username, password (crypted), auth_sourcen, active, expiration_date)
+ */
+function dc_check_phone_number($user){
+  $uInfo = UserManager::get_user_info_by_id($user['user_id']);
+  if ( empty($uInfo['phone'])) {
+    return true;
+  }
+  return false;
+}
+?>

+ 28 - 0
main/inc/lib/conditionallogin.lib.php

@@ -0,0 +1,28 @@
+<?php
+// Conditional login
+// Used to implement the loading of custom pages
+// 2011, Noel Dieschburg <noel@cblue.be>
+
+class ConditionalLogin {
+
+  public static function check_conditions($user) {
+    if (file_exists(api_get_path(SYS_PATH).'main/auth/conditional_login/conditional_login.php')) {
+      include_once(api_get_path(SYS_PATH).'main/auth/conditional_login/conditional_login.php');
+      foreach ($dc_conditions as $dc_condition) {
+        if ($dc_condition['conditional_function']($user)) {
+          $_SESSION['conditional_login']['uid'] = $user['user_id'];
+          $_SESSION['conditional_login']['can_login'] = false;
+          header("Location:". $dc_condition['url']);
+          exit();
+        }
+      }
+    }
+  }
+
+  public static function login(){
+    $_SESSION['conditional_login']['can_login'] = true;
+    header('location: '.api_get_path(WEB_PATH).api_get_setting('page_after_login').$param);
+    exit();
+  }
+}
+?>

+ 17 - 1
main/inc/local.inc.php

@@ -129,12 +129,24 @@ The course id is stored in $_cid session variable.
 */
 
 require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
+require_once (api_get_path(LIBRARY_PATH).'conditionallogin.lib.php');
 
 // verified if exists the username and password in session current
 if (isset($_SESSION['info_current_user'][1]) && isset($_SESSION['info_current_user'][2])) {
 	require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
 	require_once (api_get_path(LIBRARY_PATH).'legal.lib.php');
 }
+//Conditional login
+if (isset($_SESSION['conditional_login']['uid']) && $_SESSION['conditional_login']['can_login']=== true){
+	require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
+  $uData = UserManager::get_user_info_by_id($_SESSION['conditional_login']['uid']);
+  ConditionalLogin::check_conditions($uData);
+  $_user['user_id'] = $_SESSION['conditional_login']['uid'];
+  api_session_register('_user');
+  api_session_unregister('conditional_login');
+  $uidReset=true;
+  event_login();
+} 
 // parameters passed via GET
 $logout = isset($_GET["logout"]) ? $_GET["logout"] : '';
 $gidReq = isset($_GET["gidReq"]) ? Database::escape_string($_GET["gidReq"]) : '';
@@ -243,7 +255,7 @@ if (!empty($_SESSION['_user']['user_id']) && ! ($login || $logout)) {
     $result = Database::query($sql);
 
     if (Database::num_rows($result) > 0) {
-      $uData = Database::fetch_array($result);
+       $uData = Database::fetch_array($result);
 
       if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
         //the authentification of this user is managed by Chamilo itself
@@ -313,6 +325,7 @@ if (!empty($_SESSION['_user']['user_id']) && ! ($login || $logout)) {
                   if (is_array($my_url_list) && count($my_url_list)>0 ){
                     // the user have the permissions to enter at this site
                     if (in_array($current_access_url_id, $my_url_list)) {
+                      ConditionalLogin::check_conditions($uData);
                       $_user['user_id'] = $uData['user_id'];
                       api_session_register('_user');
                       event_login();
@@ -331,6 +344,7 @@ if (!empty($_SESSION['_user']['user_id']) && ! ($login || $logout)) {
                 } else { //Only admins of the "main" (first) Chamilo portal can login wherever they want
                   //var_dump($current_access_url_id, $my_url_list); exit;
                   if (in_array(1, $my_url_list)) { //Check if this admin have the access_url_id = 1 which means the principal
+                    ConditionalLogin::check_conditions($uData);
                     $_user['user_id'] = $uData['user_id'];
                     api_session_register('_user');
                     event_login();
@@ -349,6 +363,7 @@ if (!empty($_SESSION['_user']['user_id']) && ! ($login || $logout)) {
                   }
                 }
               } else {
+                ConditionalLogin::check_conditions($uData);
                 $_user['user_id'] = $uData['user_id'];
                 api_session_register('_user');
                 event_login();
@@ -774,6 +789,7 @@ if (isset($uidReset) && $uidReset) {	// session data refresh requested
 
             $is_platformAdmin        = (bool) (! is_null( $uData['is_admin']));
             $is_allowedCreateCourse  = (bool) (($uData ['status'] == 1) or (api_get_setting('drhCourseManagerRights') and $uData['status'] == 4));
+            ConditionalLogin::check_conditions($uData);
 
             api_session_register('_user');
         } else {