From a8b11d8923dcb2fed771619f07a8bd32fbc99e36 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Fri, 29 Nov 2024 14:33:07 +0700 Subject: [PATCH] MDL-82963 Tag: Improve validation for users tagged with sepecified tag --- tag/tests/behat/tagindex.feature | 42 ++++++++++++++++++++++++++------ user/lib.php | 11 ++++++++- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/tag/tests/behat/tagindex.feature b/tag/tests/behat/tagindex.feature index ffe4db35d2b..1816ec25011 100644 --- a/tag/tests/behat/tagindex.feature +++ b/tag/tests/behat/tagindex.feature @@ -6,10 +6,13 @@ Feature: Browsing tagged items Background: Given the following "users" exist: - | username | firstname | lastname | email | interests | - | user1 | User | 1 | user1@example.com | Cat | - | user2 | User | 2 | user1@example.com | Cat, Dog | - | user3 | User | 3 | user1@example.com | Dog | + | username | firstname | lastname | email | interests | + | user1 | User | 1 | user1@example.com | Cat, Zebra | + | user2 | User | 2 | user2@example.com | Cat, Dog, Zebra | + | user3 | User | 3 | user3@example.com | Zebra | + | user4 | User | 4 | user4@example.com | Zebra | + | user5 | User | 5 | user5@example.com | Zebra | + | user6 | User | 6 | user6@example.com | Zebra | And the following "courses" exist: | fullname | shortname | tags | | Course 1 | c1 | Cat, Dog | @@ -19,12 +22,15 @@ Feature: Browsing tagged items | Course 5 | c5 | Cat | | Course 6 | c6 | Cat | | Course 7 | c7 | Cat | + And the following config values are set as admin: + | unaddableblocks | | theme_boost| + And the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | moodle/user:viewdetails | Allow | user | System | | Scenario: Browse tag index with javascript disabled When I log in as "user1" And I turn editing mode on - And the following config values are set as admin: - | unaddableblocks | | theme_boost| # TODO MDL-57120 "Tags" link not accessible without navigation block. And I add the "Navigation" block if not present And I click on "Tags" "link" in the "Navigation" "block" @@ -72,8 +78,6 @@ Feature: Browsing tagged items Scenario: Browse tag index with javascript enabled When I log in as "user1" And I turn editing mode on - And the following config values are set as admin: - | unaddableblocks | | theme_boost| # TODO MDL-57120 "Tags" link not accessible without navigation block. And I add the "Navigation" block if not present And I click on "Site pages" "list_item" in the "Navigation" "block" @@ -114,3 +118,25 @@ Feature: Browsing tagged items And I should not see "Course2" And I should not see "Course1" And I log out + + Scenario Outline: Browse tag index and view other profiles + Given I log in as "user1" + And I turn editing mode on + And the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | moodle/user:viewdetails | | user | System | | + # TODO MDL-57120 "Tags" link not accessible without navigation block. + And I add the "Navigation" block if not present + When I click on "Tags" "link" in the "Navigation" "block" + And I follow "Zebra" + Then I should "User 2" + And I should "User 3" + And I should "User 4" + And I should "User 5" + # Pagination test. + And I should "More" in the "#tagarea-core-user" "css_element" + + Examples: + | permission | action1 | action2 | + | Prevent | not see | not see | + | Allow | see | see | diff --git a/user/lib.php b/user/lib.php index f9d9d88685b..beb2ef25ca0 100644 --- a/user/lib.php +++ b/user/lib.php @@ -1283,15 +1283,24 @@ function user_get_tagged_users($tag, $exclusivemode = false, $fromctx = 0, $ctx } $perpage = $exclusivemode ? 24 : 5; $content = ''; - $totalpages = ceil($usercount / $perpage); + $excludedusers = 0; if ($usercount) { $userlist = $tag->get_tagged_items('core', 'user', $page * $perpage, $perpage, 'it.deleted=:notdeleted', array('notdeleted' => 0)); + foreach ($userlist as $user) { + if (!user_can_view_profile($user)) { + unset($userlist[$user->id]); + $excludedusers++; + } + } $renderer = $PAGE->get_renderer('core', 'user'); $content .= $renderer->user_list($userlist, $exclusivemode); } + // Calculate the total number of pages. + $totalpages = ceil(($usercount - $excludedusers) / $perpage); + return new core_tag\output\tagindex($tag, 'core', 'user', $content, $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages); }