Browse Source

Check if course tool is available otherwise block access BT#15892

Julio Montoya 5 years ago
parent
commit
c40550d0d8
4 changed files with 24 additions and 13 deletions
  1. 1 1
      main/group/group.php
  2. 1 1
      main/group/group_space.php
  3. 20 8
      main/inc/lib/api.lib.php
  4. 2 3
      main/user/user.php

+ 1 - 1
main/group/group.php

@@ -27,7 +27,7 @@ $course_id = api_get_course_int_id();
 $sessionId = api_get_session_id();
 
 // Notice for unauthorized people.
-api_protect_course_script(true);
+api_protect_course_script(true, false, 'group');
 
 $htmlHeadXtra[] = '<script>
 $(function() {

+ 1 - 1
main/group/group_space.php

@@ -13,7 +13,7 @@ require_once __DIR__.'/../inc/global.inc.php';
 $current_course_tool = TOOL_GROUP;
 
 // Notice for unauthorized people.
-api_protect_course_script(true);
+api_protect_course_script(true, false, 'group');
 
 require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
 

+ 20 - 8
main/inc/lib/api.lib.php

@@ -1140,6 +1140,7 @@ function api_valid_email($address)
  *
  * @param bool Option to print headers when displaying error message. Default: false
  * @param bool whether session admins should be allowed or not
+ * @param bool $checkTool check if tool is available for users (user, group)
  *
  * @return bool True if the user has access to the current course or is out of a course context, false otherwise
  *
@@ -1147,7 +1148,7 @@ function api_valid_email($address)
  *
  * @author Roan Embrechts
  */
-function api_protect_course_script($print_headers = false, $allow_session_admins = false, $allow_drh = false)
+function api_protect_course_script($print_headers = false, $allow_session_admins = false, $checkTool = '')
 {
     $course_info = api_get_course_info();
     if (empty($course_info)) {
@@ -1170,26 +1171,26 @@ function api_protect_course_script($print_headers = false, $allow_session_admins
         return true;
     }
 
-    $is_allowed_in_course = api_is_allowed_in_course();
+    $isAllowedInCourse = api_is_allowed_in_course();
     $is_visible = false;
     if (isset($course_info) && isset($course_info['visibility'])) {
         switch ($course_info['visibility']) {
             default:
             case COURSE_VISIBILITY_CLOSED:
                 // Completely closed: the course is only accessible to the teachers. - 0
-                if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
+                if (api_get_user_id() && !api_is_anonymous() && $isAllowedInCourse) {
                     $is_visible = true;
                 }
                 break;
             case COURSE_VISIBILITY_REGISTERED:
                 // Private - access authorized to course members only - 1
-                if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
+                if (api_get_user_id() && !api_is_anonymous() && $isAllowedInCourse) {
                     $is_visible = true;
                 }
                 break;
             case COURSE_VISIBILITY_OPEN_PLATFORM:
                 // Open - access allowed for users registered on the platform - 2
-                if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
+                if (api_get_user_id() && !api_is_anonymous() && $isAllowedInCourse) {
                     $is_visible = true;
                 }
                 break;
@@ -1206,7 +1207,7 @@ function api_protect_course_script($print_headers = false, $allow_session_admins
         }
 
         //If password is set and user is not registered to the course then the course is not visible
-        if ($is_allowed_in_course == false &&
+        if ($isAllowedInCourse == false &&
             isset($course_info['registration_code']) &&
             !empty($course_info['registration_code'])
         ) {
@@ -1214,12 +1215,23 @@ function api_protect_course_script($print_headers = false, $allow_session_admins
         }
     }
 
+    if (!empty($checkTool)) {
+        if (!api_is_allowed_to_edit(true, true, true)) {
+            $toolInfo = api_get_tool_information_by_name($checkTool);
+            if (!empty($toolInfo) && isset($toolInfo['visibility']) && $toolInfo['visibility'] == 0) {
+                api_not_allowed(true);
+
+                return false;
+            }
+        }
+    }
+
     // Check session visibility
     $session_id = api_get_session_id();
 
     if (!empty($session_id)) {
-        //$is_allowed_in_course was set in local.inc.php
-        if (!$is_allowed_in_course) {
+        // $isAllowedInCourse was set in local.inc.php
+        if (!$isAllowedInCourse) {
             $is_visible = false;
         }
     }

+ 2 - 3
main/user/user.php

@@ -18,7 +18,7 @@ $current_course_tool = TOOL_USER;
 $this_section = SECTION_COURSES;
 
 // notice for unauthorized people.
-api_protect_course_script(true);
+api_protect_course_script(true, false, 'user');
 
 if (!api_is_platform_admin(true)) {
     if (!api_is_course_admin() && !api_is_coach()) {
@@ -39,10 +39,9 @@ $_user = api_get_user_info();
 $courseCode = $course_info['code'];
 $courseId = $course_info['real_id'];
 $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
-
 $canEditUsers = api_get_setting('allow_user_course_subscription_by_course_admin') == 'true' || api_is_platform_admin();
 
-//Can't auto unregister from a session
+// Can't auto unregister from a session
 if (!empty($sessionId)) {
     $course_info['unsubscribe'] = 0;
 }