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 2 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\OsUserList;
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(OsUserList::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
68 changes: 68 additions & 0 deletions src/Views/OsUserList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace PressbooksMultiInstitution\Views;

use PressbooksMultiInstitution\Models\InstitutionUser;

use function PressbooksMultiInstitution\Support\get_institution_by_manager;

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

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

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

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

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

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

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

public function modifyUserQuery(\WP_User_Query $query): void
{
global $pagenow;
if (!is_admin() || $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;
}
}
Loading