Переглянути джерело

Allow multiple anon users setting see BT#13324

//$_configuration['max_anonymous_users'] = 0;
jmontoyaa 7 роки тому
батько
коміт
7dce46d968
3 змінених файлів з 61 додано та 4 видалено
  1. 45 1
      main/inc/lib/api.lib.php
  2. 13 3
      main/inc/local.inc.php
  3. 3 0
      main/install/configuration.dist.php

+ 45 - 1
main/inc/lib/api.lib.php

@@ -1715,6 +1715,8 @@ function api_get_course_setting($setting_name, $course_code = null)
     return -1;
 }
 
+
+
 /**
  * Gets an anonymous user ID
  *
@@ -1726,8 +1728,44 @@ function api_get_course_setting($setting_name, $course_code = null)
  */
 function api_get_anonymous_id()
 {
+    // Find if another anon is connected now
+    $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
+    $now = api_get_utc_datetime();
+    $ip = api_get_real_ip();
+    $max = api_get_configuration_value('max_anonymous_users');
+
+    if ($max >= 2) {
+        $sql = "SELECT * FROM $table 
+                WHERE ('$now' BETWEEN login_date AND logout_date) AND user_ip = '$ip'";
+        $result = Database::query($sql);
+        if (empty(Database::num_rows($result))) {
+            $login = uniqid('anon_');
+            $anonList = UserManager::get_user_list(['status' => ANONYMOUS], ['registration_date ASC']);
+            if (count($anonList) == $max) {
+                foreach ($anonList as $userToDelete) {
+                    // Delete next older anon. Avoid the default anon user_id = 2 .
+                    if ($userToDelete['user_id'] != 2) {
+                        UserManager::delete_user($userToDelete['user_id']);
+                        break;
+                    }
+                }
+            }
+            $userId = UserManager::create_user(
+                $login,
+                'anon',
+                ANONYMOUS,
+                ' anonymous@localhost',
+                $login,
+                $login
+            );
+            return $userId;
+        }
+    }
+
     $table = Database::get_main_table(TABLE_MAIN_USER);
-    $sql = "SELECT user_id FROM $table WHERE status = ".ANONYMOUS;
+    $sql = "SELECT user_id 
+            FROM $table 
+            WHERE status = ".ANONYMOUS." ";
     $res = Database::query($sql);
     if (Database::num_rows($res) > 0) {
         $row = Database::fetch_array($res);
@@ -2190,10 +2228,16 @@ function api_set_anonymous()
     if (!empty($_user['user_id'])) {
         return false;
     }
+
     $user_id = api_get_anonymous_id();
     if ($user_id == 0) {
         return false;
     }
+
+    if (isset($_user['is_anonymous'])) {
+        return false;
+    }
+
     Session::erase('_user');
     $_user['user_id'] = $user_id;
     $_user['is_anonymous'] = true;

+ 13 - 3
main/inc/local.inc.php

@@ -816,12 +816,17 @@ if (!empty($_SESSION['_user']['user_id']) && !($login || $logout)) {
     //    $gidReset = true;
 } // end else
 
+$maxAnons = api_get_configuration_value('max_anonymous_users');
+
  // Now check for anonymous user mode
 if (isset($use_anonymous) && $use_anonymous) {
     //if anonymous mode is set, then try to set the current user as anonymous
     //if he doesn't have a login yet
-
-    api_set_anonymous();
+    $anonResult = api_set_anonymous();
+    if ($maxAnons >= 2 && $anonResult) {
+        $uidReset = true;
+        Event::eventLogin($_user['user_id']);
+    }
 } else {
     //if anonymous mode is not set, then check if this user is anonymous. If it
     //is, clean it from being anonymous (make him a nobody :-))
@@ -843,7 +848,12 @@ if (isset($uidReset) && $uidReset) {
     unset($_SESSION['_user']['uidReset']);
     $is_platformAdmin = false;
     $is_allowedCreateCourse = false;
-    if (isset($_user['user_id']) && $_user['user_id'] && !api_is_anonymous()) {
+    $isAnonymous = api_is_anonymous();
+    if ($maxAnons >= 2) {
+        $isAnonymous = false;
+    }
+
+    if (isset($_user['user_id']) && $_user['user_id'] && !$isAnonymous) {
         // a uid is given (log in succeeded)
         $_SESSION['loginFailed'] = false;
         unset($_SESSION['loginFailedCount']);

+ 3 - 0
main/install/configuration.dist.php

@@ -643,3 +643,6 @@ $_configuration['gradebook_badge_sidebar'] = [
 //$_configuration['allow_agenda_edit_for_hrm'] = false;
 // Allow double validation in registration page
 //$_configuration['allow_double_validation_in_registration'] = false;
+// Allow multiple anon users see BT#13324
+//$_configuration['max_anonymous_users'] = 0;
+