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 all 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
16 changes: 16 additions & 0 deletions resources/views/table/wp-users/books-column.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@foreach ($books as $book)
<span class="site-1">
<a href="{{ $book->siteurl }}/wp-admin">
{{ $book->domain . $book->path }}
</a>
<small class="row-actions">
<span class="edit">
<a href="{{ $book->siteurl }}/wp-admin">{{ __('Dashboard', 'pressbooks-multi-institution') }}</a> |
</span>
<span class="view">
<a href="{{ $book->siteurl }}">{{ __('View', 'pressbooks-multi-institution') }}</a>
</span>
</small>
</span>
<br />
@endforeach
24 changes: 23 additions & 1 deletion src/Actions/AssignUserToInstitution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Support\Str;
use PressbooksMultiInstitution\Models\Institution;
use WP_User;

use function PressbooksMultiInstitution\Support\get_institution_by_manager;

class AssignUserToInstitution
{
Expand All @@ -15,6 +18,14 @@ public function handle(int $userId): bool
return false;
}

$institution = get_institution_by_manager();

return $institution === 0 ?
$this->assignByUserDomain($user) : $this->assignUserByInstitution($user, $institution);
}

private function assignByUserDomain(WP_User $user): bool
{
$email = Str::of($user->user_email);

$domain = (string) $email->after('@')->trim();
Expand All @@ -27,7 +38,18 @@ public function handle(int $userId): bool
}

$institution->users()->create([
'user_id' => $userId,
'user_id' => $user->ID,
]);

return true;
}

private function assignUserByInstitution(WP_User $user, int $institution): bool
{
$institution = Institution::find($institution);

$institution->users()->create([
'user_id' => $user->ID,
]);

return true;
Expand Down
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 @@ -40,6 +41,7 @@ public function setUp(): void

Container::getInstance()->singleton(BookList::class, fn () => new BookList(app('db')));
Container::getInstance()->singleton(UserList::class, fn () => new UserList(app('db')));
Container::getInstance()->singleton(WpUserList::class, fn () => new WpUserList);
}

private function registerActions(): void
Expand Down
40 changes: 27 additions & 13 deletions src/Services/MenuManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,35 @@ public function handleMenus(): void
if (!is_main_site() || !is_super_admin()) {
unset($submenu['index.php'][0]);
}

if (get_institution_by_manager() !== 0) {
remove_menu_page($this->getContextSlug('customize.php', true));
remove_menu_page($this->getContextSlug('edit.php?post_type=page', true));
// Remove the default dashboard page and point to the institutional dashboard
foreach ($menu as &$item) {
if ($item[2] == network_admin_url('index.php')) {
$item[2] = network_site_url('wp-admin/index.php?page=pb_institutional_manager');
break;
}
}
remove_menu_page($this->getContextSlug('admin.php?page=pb_network_integrations', false));
remove_menu_page('settings.php');
remove_menu_page('pb_network_integrations');
remove_menu_page($this->slug);
$this->removeMenuItems($menu);

add_action('admin_bar_menu', [$this, 'modifyAdminBarMenus'], 1000);

if (count($submenu['users.php']) > 1) {
// remove submenu items for users menu
$submenu['users.php'] = array_slice($submenu['users.php'], 0, 1);
}
}
}

private function removeMenuItems(array &$menu): void
{
remove_menu_page($this->getContextSlug('customize.php', true));
remove_menu_page($this->getContextSlug('edit.php?post_type=page', true));
remove_menu_page($this->getContextSlug('admin.php?page=pb_network_integrations', false));
remove_menu_page('settings.php');
remove_menu_page('pb_network_integrations');
remove_menu_page($this->slug);
remove_menu_page('h5p');

// Remove the default dashboard page and point to the institutional dashboard
foreach ($menu as &$item) {
if ($item[2] == network_admin_url('index.php')) {
$item[2] = network_site_url('wp-admin/index.php?page=pb_institutional_manager');
break;
}
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/Services/PermissionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use PressbooksMultiInstitution\Views\BookList;
use PressbooksMultiInstitution\Views\UserList;

use PressbooksMultiInstitution\Views\WpUserList;

use function Pressbooks\Admin\NetworkManagers\_restricted_users;
use function PressbooksMultiInstitution\Support\get_allowed_book_pages;
use function PressbooksMultiInstitution\Support\get_restricted_book_pages;
use function PressbooksMultiInstitution\Support\get_allowed_pages;
use function PressbooksMultiInstitution\Support\get_institution_books;
use function PressbooksMultiInstitution\Support\get_institution_by_manager;
Expand Down Expand Up @@ -39,6 +41,7 @@ public function setupFilters(): void
Container::get(TableViews::class)->init();
Container::get(BookList::class)->init();
Container::get(UserList::class)->init();
Container::get(WpUserList::class)->init();

do_action('pb_institutional_filters_created', $institution, $institutionalManagers, $institutionalUsers);
}
Expand All @@ -65,7 +68,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 @@ -157,7 +160,7 @@ private function currentUserHasAccess(string $currentPageParam, array $allowedBo
global $pagenow;

$allowedPages = get_allowed_pages();
$bookPages = get_allowed_book_pages();
$restrictedBookPages = get_restricted_book_pages();

$isAccessAllowed = false;

Expand All @@ -181,18 +184,21 @@ private function currentUserHasAccess(string $currentPageParam, array $allowedBo
// Check if the current page is a book page and if the user has access to it
$userBooks = array_slice(array_keys(get_blogs_of_user(get_current_user_id())), 1); // remove the main site

if ((in_array($currentBlogId, $userBooks) || in_array($currentBlogId, $allowedBooks)) && !in_array($pagenow, $bookPages)) {
if (
(in_array($currentBlogId, $userBooks) || in_array($currentBlogId, $allowedBooks)) &&
!in_array($pagenow, $restrictedBookPages)
) {
$isAccessAllowed = true;
}

$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.


$userId = $_REQUEST['id'] ?? $_REQUEST['user_id'] ?? null;

if (isset($_REQUEST['id']) && !in_array($_REQUEST['id'], $institutionalUsers)) {
if ($userId && ! in_array($userId, $institutionalUsers)) {
$isAccessAllowed = false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ function get_allowed_pages(): array
'plugins.php',
'media-new.php',
'users.php',
'user-new.php',
'export-personal-data.php',
'erase-personal-data.php',
'options-privacy.php'
];
}

function get_allowed_book_pages(): array
function get_restricted_book_pages(): array
{
return [
'site-info.php',
Expand Down
103 changes: 103 additions & 0 deletions src/Views/WpUserList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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 init(): void
{
add_filter('wpmu_users_columns', [$this, 'manageTableColumns']);
add_filter('manage_users_custom_column', [$this, 'displayCustomColumns'], 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 displayCustomColumns(string $value, string $columnName, int $userId): string
{
return match ($columnName) {
'institution' => InstitutionUser::query()
->where('user_id', $userId)
->first()
?->institution
->name ?? __('Unassigned', 'pressbooks-multi-institution'),
'books' => $this->getBooksColumnValue($userId),
default => $value,
};
}

private function getBooksColumnValue(int $userId): string
{
$blogs = get_blogs_of_user($userId);

unset($blogs[get_main_site_id()]);

return app('Blade')->render('PressbooksMultiInstitution::table.wp-users.books-column', [
'books' => $blogs,
]);
}

public function manageTableColumns(array $columns): array
{
unset($columns['blogs']);

return array_slice($columns, 0, 4, true) +
['institution' => __('Institution', 'pressbooks-multi-institution')] +
array_slice($columns, 4, null, true) +
['books' => __('Books', 'pressbooks-multi-institution')];
}

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;
}
}
Loading
Loading