Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add institution info for network/users.php page #159

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PressbooksMultiInstitution\Services\MenuManager;
use PressbooksMultiInstitution\Services\PermissionsManager;
use PressbooksMultiInstitution\Views\BookList;
use PressbooksMultiInstitution\Views\WpUserList;
use PressbooksMultiInstitution\Views\UserList;

/**
Expand Down Expand Up @@ -61,6 +62,7 @@ private function registerActions(): void
);
add_action('init', [InstitutionalManagerDashboard::class, 'init']);
add_action('init', fn () => app(InstitutionStatsService::class)->setupHooks());
add_action('init', fn () => app(WpUserList::class)->setupHooks());
}

private function registerBlade(): void
Expand Down
8 changes: 5 additions & 3 deletions src/Services/PermissionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function handlePagesPermissions($institution, $institutionalManagers, $in
*/

add_filter('can_edit_network', function ($canEdit) use ($allowedBooks) {
if (is_network_admin() && !in_array($_REQUEST['id'], $allowedBooks)) {
if (is_network_admin() && isset($_REQUEST['id']) && !in_array($_REQUEST['id'], $allowedBooks)) {
Copy link
Contributor Author

@richard015ar richard015ar Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got PHP Warning: Undefined array key "id" in /app/web/app/plugins/pressbooks-multi-institution/src/Services/PermissionsManager.php on line 68 when going to user.php (standard list) as an IM.

$canEdit = false;
}
return $canEdit;
Expand Down Expand Up @@ -188,8 +188,10 @@ private function currentUserHasAccess(string $currentPageParam, array $allowedBo
$institutionalUsers = apply_filters('pb_institutional_users', []);

if ($currentPageParam === 'pb_network_analytics_userlist' || $pagenow === 'users.php' || $pagenow === 'user-edit.php') {
if (isset($_REQUEST['user_id']) && in_array($_REQUEST['user_id'], $institutionalUsers)) {
$isAccessAllowed = true;
$isAccessAllowed = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can go to users.php list without having any $_REQUEST value as it has in edit/delete actions. In that case, we want to allow it.


if (isset($_REQUEST['user_id']) && ! in_array($_REQUEST['user_id'], $institutionalUsers)) {
$isAccessAllowed = false;
}

if (isset($_REQUEST['id']) && !in_array($_REQUEST['id'], $institutionalUsers)) {
Expand Down
88 changes: 88 additions & 0 deletions src/Views/WpUserList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace PressbooksMultiInstitution\Views;

use PressbooksMultiInstitution\Models\Institution;
use PressbooksMultiInstitution\Models\InstitutionUser;

use WP_User_Query;

use function PressbooksMultiInstitution\Support\get_institution_by_manager;

class WpUserList
{
public function setupHooks(): void
{
add_filter('wpmu_users_columns', [$this, 'addInstitutionColumn']);
add_filter('manage_users_custom_column', [$this, 'displayInstitutionValue'], 10, 3);

add_filter('manage_users-network_sortable_columns', [$this, 'makeInstitutionColumnSortable']);

add_action('pre_user_query', [$this, 'modifyUserQuery']);

add_filter('views_users-network', [$this, 'removeSuperAdminFilter']);
}

public function displayInstitutionValue(string $value, string $columnName, int $userId): string
{
if ($columnName !== 'institution') {
return $value;
}

return InstitutionUser::query()
->where('user_id', $userId)
->first()
?->institution
->name ?? __('Unassigned', 'pressbooks-multi-institution');
}

public function addInstitutionColumn(array $columns): array
{
return array_slice($columns, 0, 4, true) +
['institution' => __('Institution', 'pressbooks-multi-institution')] +
array_slice($columns, 4, null, true);
}

public function makeInstitutionColumnSortable(array $columns): array
{
$columns['institution'] = 'institution';
return $columns;
}

public function modifyUserQuery(WP_User_Query $query): void
{
global $pagenow;
if (! is_super_admin() || ! is_main_site() || $pagenow !== 'users.php') {
return;
}

global $wpdb;
$query->query_from .= " LEFT JOIN {$wpdb->base_prefix}institutions_users AS iu ON {$wpdb->users}.ID = iu.user_id";
$query->query_from .= " LEFT JOIN {$wpdb->base_prefix}institutions AS i ON iu.institution_id = i.id";

$institution = get_institution_by_manager();
if ($institution !== 0) {
$query->query_where .= $wpdb->prepare(" AND iu.institution_id = %d", $institution);
}

$order = (isset($_GET['order']) && $_GET['order'] === 'asc') ? 'ASC' : 'DESC';

$query->query_orderby = "ORDER BY i.name " . $order;
}

public function removeSuperAdminFilter(array $views): array
{
$institution = get_institution_by_manager();
if($institution === 0) {
return $views;
}

unset($views['super']);

$totalUsers = Institution::find($institution)->users()->count();
$views['all'] = "<a href='#' class='current' aria-current='page'> " .
__('All', 'pressbooks-multi-institution') . " <span class='count'>({$totalUsers})</span></a>";

return $views;
}
}
153 changes: 153 additions & 0 deletions tests/Feature/Views/WpUserListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Tests\Feature\Views;

use Pressbooks\Container;
use PressbooksMultiInstitution\Models\Institution;
use PressbooksMultiInstitution\Views\WpUserList;
use Tests\TestCase;
use Tests\Traits\CreatesModels;

/**
* @group wp-user-list
*/
class WpUserListTest extends TestCase
{
use CreatesModels;

/**
* @test
*/
public function it_registers_hook_actions(): void
{
$this->assertFalse(has_filter('wpmu_users_columns'));
$this->assertFalse(has_filter('manage_users_custom_column'));
$this->assertFalse(has_filter('manage_users-network_sortable_columns'));
$this->assertFalse(has_action('pre_user_query'));
$this->assertFalse(has_filter('views_users-network'));

Container::get(WpUserList::class)->setupHooks();

$this->assertTrue(has_filter('wpmu_users_columns'));
$this->assertTrue(has_filter('manage_users_custom_column'));
$this->assertTrue(has_filter('manage_users-network_sortable_columns'));
$this->assertTrue(has_action('pre_user_query'));
$this->assertTrue(has_filter('views_users-network'));
}

/**
* @test
*/
public function it_displays_institution_value(): void
{
$this->createInstitutionsUsers(1, 1);

$institution = Institution::query()->first();
$userId = $institution->users()->first()->user_id;

$this->assertEquals(
$institution->name,
Container::get(WpUserList::class)->displayInstitutionValue('', 'institution', $userId)
);

$this->assertEquals(
'Unassigned',
Container::get(WpUserList::class)->displayInstitutionValue('', 'institution', 99)
);
}

/**
* @test
*/
public function it_adds_institution_column(): void
{
$columns = ['name' => 'Name', 'email' => 'Email', 'registered' => 'Registered'];
$expected = ['name' => 'Name', 'email' => 'Email', 'institution' => 'Institution', 'registered' => 'Registered'];

$this->assertEquals(
$expected,
Container::get(WpUserList::class)->addInstitutionColumn($columns)
);
}

/**
* @test
*/
public function it_makes_institution_column_sortable(): void
{
$columns = ['name' => 'name', 'email' => 'email', 'registered' => 'registered'];
$expected = ['name' => 'name', 'email' => 'email', 'registered' => 'registered', 'institution' => 'institution'];

$this->assertEquals(
$expected,
Container::get(WpUserList::class)->makeInstitutionColumnSortable($columns)
);
}

/**
* @test
*/
public function it_modifies_user_query(): void
{
$query = new \WP_User_Query(['blog_id' => 1]);

global $pagenow;
$pagenow = 'users.php';
$userId = $this->newSuperAdmin();
wp_set_current_user($userId);
$_GET['order'] = 'desc';

Container::get(WpUserList::class)->modifyUserQuery($query);

$this->assertStringContainsString('LEFT JOIN', $query->query_from);
$this->assertStringContainsString('institutions_users', $query->query_from);
$this->assertStringContainsString('institutions', $query->query_from);
$this->assertStringContainsString('ORDER BY i.name DESC', $query->query_orderby);
}

/**
* @test
*/
public function it_add_where_conditions_to_query_for_ims(): void
{
$query = new \WP_User_Query(['blog_id' => 1]);

$this->createInstitutionsUsers(1, 3);

$institution = Institution::query()->first();
$userId = $this->newInstitutionalManager($institution);

grant_super_admin($userId);
wp_set_current_user($userId);

global $pagenow;
$pagenow = 'users.php';

Container::get(WpUserList::class)->modifyUserQuery($query);

$this->assertStringContainsString('LEFT JOIN', $query->query_from);
$this->assertStringContainsString('institutions_users', $query->query_from);
$this->assertStringContainsString('institutions', $query->query_from);
$this->assertStringContainsString('AND iu.institution_id = ' . $institution->id, $query->query_where);
}

/**
* @test
*/
public function it_removes_super_admin_filter(): void
{
$this->createInstitutionsUsers(1, 3);

$institution = Institution::query()->first();
$userId = $this->newInstitutionalManager($institution);
wp_set_current_user($userId);

$views = ['all' => 'All', 'super' => 'Super'];
$totalUsers = $institution->users()->count();

$this->assertEquals(
['all' => "<a href='#' class='current' aria-current='page'> All <span class='count'>({$totalUsers})</span></a>"],
Container::get(WpUserList::class)->removeSuperAdminFilter($views)
);
}
}
Loading