1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

Revised role index list to align with user list

This commit is contained in:
Dan Brown
2022-10-29 20:52:17 +01:00
parent 0ef06fd298
commit 98b59a1024
8 changed files with 109 additions and 43 deletions

View File

@ -0,0 +1,37 @@
<?php
namespace BookStack\Auth\Queries;
use BookStack\Auth\Role;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* Get all the roles in the system in a paginated format.
*/
class AllRolesPaginatedAndSorted
{
/**
* @param array{sort: string, order: string, search: string} $sortData
*/
public function run(int $count, array $sortData): LengthAwarePaginator
{
$sort = $sortData['sort'];
if ($sort === 'created_at') {
$sort = 'users.created_at';
}
$query = Role::query()->select(['*'])
->withCount(['users', 'permissions'])
->orderBy($sort, $sortData['order']);
if ($sortData['search']) {
$term = '%' . $sortData['search'] . '%';
$query->where(function ($query) use ($term) {
$query->where('display_name', 'like', $term)
->orWhere('description', 'like', $term);
});
}
return $query->paginate($count);
}
}

View File

@ -110,14 +110,6 @@ class Role extends Model implements Loggable
return static::query()->where('system_name', '=', $systemName)->first();
}
/**
* Get all visible roles.
*/
public static function visible(): Collection
{
return static::query()->where('hidden', '=', false)->orderBy('name')->get();
}
/**
* {@inheritdoc}
*/

View File

@ -3,6 +3,7 @@
namespace BookStack\Http\Controllers;
use BookStack\Auth\Permissions\PermissionsRepo;
use BookStack\Auth\Queries\AllRolesPaginatedAndSorted;
use BookStack\Auth\Role;
use BookStack\Exceptions\PermissionsException;
use Exception;
@ -11,11 +12,8 @@ use Illuminate\Validation\ValidationException;
class RoleController extends Controller
{
protected $permissionsRepo;
protected PermissionsRepo $permissionsRepo;
/**
* PermissionController constructor.
*/
public function __construct(PermissionsRepo $permissionsRepo)
{
$this->permissionsRepo = $permissionsRepo;
@ -24,14 +22,25 @@ class RoleController extends Controller
/**
* Show a listing of the roles in the system.
*/
public function index()
public function index(Request $request)
{
$this->checkPermission('user-roles-manage');
$roles = $this->permissionsRepo->getAllRoles();
$listDetails = [
'search' => $request->get('search', ''),
'sort' => setting()->getForCurrentUser('roles_sort', 'display_name'),
'order' => setting()->getForCurrentUser('roles_sort_order', 'asc'),
];
$roles = (new AllRolesPaginatedAndSorted())->run(20, $listDetails);
$roles->appends(['search' => $listDetails['search']]);
$this->setPageTitle(trans('settings.roles'));
return view('settings.roles.index', ['roles' => $roles]);
return view('settings.roles.index', [
'roles' => $roles,
'listDetails' => $listDetails,
]);
}
/**
@ -75,16 +84,11 @@ class RoleController extends Controller
/**
* Show the form for editing a user role.
*
* @throws PermissionsException
*/
public function edit(string $id)
{
$this->checkPermission('user-roles-manage');
$role = $this->permissionsRepo->getRoleById($id);
if ($role->hidden) {
throw new PermissionsException(trans('errors.role_cannot_be_edited'));
}
$this->setPageTitle(trans('settings.role_edit'));

View File

@ -251,7 +251,7 @@ class UserController extends Controller
*/
public function changeSort(Request $request, string $id, string $type)
{
$validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users'];
$validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles'];
if (!in_array($type, $validSortTypes)) {
return redirect()->back(500);
}
@ -318,7 +318,13 @@ class UserController extends Controller
$this->checkPermissionOrCurrentUser('users-manage', $userId);
$sort = $request->get('sort');
if (!in_array($sort, ['name', 'created_at', 'updated_at', 'default', 'email', 'last_activity_at'])) {
// TODO - Need to find a better way to validate sort options
// Probably better to do a simple validation here then validate at usage.
$validSorts = [
'name', 'created_at', 'updated_at', 'default', 'email', 'last_activity_at', 'display_name',
'users_count', 'permissions_count',
];
if (!in_array($sort, $validSorts)) {
$sort = 'name';
}