-
Notifications
You must be signed in to change notification settings - Fork 1
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
richard015ar
wants to merge
6
commits into
dev
Choose a base branch
from
feat/open-source-users-list
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
17a63f1
feat: add sortable institution column to network/users.php list
richard015ar ac58a84
feat: restrict users for institutional managers
richard015ar b1b97a5
feat: remove super admin filter for institutional managers
richard015ar de610df
feat: add tests and tweak last implementation details
richard015ar 39be638
fix: allow user creation for institutional managers
richard015ar 58916c4
fix: permissions and books column
richard015ar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
@@ -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)) { | ||
$canEdit = false; | ||
} | ||
return $canEdit; | ||
|
@@ -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; | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can go to |
||
|
||
$userId = $_REQUEST['id'] ?? $_REQUEST['user_id'] ?? null; | ||
|
||
if (isset($_REQUEST['id']) && !in_array($_REQUEST['id'], $institutionalUsers)) { | ||
if ($userId && ! in_array($userId, $institutionalUsers)) { | ||
$isAccessAllowed = false; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.