From bbce7b40ca27d1d01ae75a63237832c945255454 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 19 Sep 2024 17:20:56 +0100 Subject: [PATCH] Additional consistencies Signed-off-by: snipe --- .../Api/AssetMaintenancesController.php | 2 +- app/Http/Controllers/Api/GroupsController.php | 27 ++++++++++++++----- .../Controllers/Api/ReportsController.php | 2 +- app/Http/Transformers/GroupsTransformer.php | 5 +++- app/Models/Actionlog.php | 2 +- app/Models/AssetMaintenance.php | 14 ---------- app/Models/Group.php | 7 ++++- app/Models/Loggable.php | 2 +- app/Presenters/ActionlogPresenter.php | 2 +- resources/views/groups/index.blade.php | 2 +- 10 files changed, 37 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Api/AssetMaintenancesController.php b/app/Http/Controllers/Api/AssetMaintenancesController.php index df0a3e30a8..6386f04a9d 100644 --- a/app/Http/Controllers/Api/AssetMaintenancesController.php +++ b/app/Http/Controllers/Api/AssetMaintenancesController.php @@ -80,7 +80,7 @@ class AssetMaintenancesController extends Controller switch ($sort) { case 'created_by': - $maintenances = $maintenances->OrderAdmin($order); + $maintenances = $maintenances->OrderByCreatedBy($order); break; case 'supplier': $maintenances = $maintenances->OrderBySupplier($order); diff --git a/app/Http/Controllers/Api/GroupsController.php b/app/Http/Controllers/Api/GroupsController.php index 878650c718..81217ce8db 100644 --- a/app/Http/Controllers/Api/GroupsController.php +++ b/app/Http/Controllers/Api/GroupsController.php @@ -23,9 +23,8 @@ class GroupsController extends Controller $this->authorize('superadmin'); $this->authorize('view', Group::class); - $allowed_columns = ['id', 'name', 'created_at', 'users_count']; - $groups = Group::select('id', 'name', 'permissions', 'created_at', 'updated_at', 'created_by')->with('admin')->withCount('users as users_count'); + $groups = Group::select('id', 'name', 'permissions', 'created_at', 'updated_at', 'created_by')->with('adminuser')->withCount('users as users_count'); if ($request->filled('search')) { $groups = $groups->TextSearch($request->input('search')); @@ -35,13 +34,29 @@ class GroupsController extends Controller $groups->where('name', '=', $request->input('name')); } - // Make sure the offset and limit are actually integers and do not exceed system limits + $offset = ($request->input('offset') > $groups->count()) ? $groups->count() : app('api_offset_value'); $limit = app('api_limit_value'); - $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; - $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; - $groups->orderBy($sort, $order); + + switch ($request->input('sort')) { + case 'created_by': + $groups = $groups->OrderByCreatedBy($order); + break; + default: + // This array is what determines which fields should be allowed to be sorted on ON the table itself. + // These must match a column on the consumables table directly. + $allowed_columns = [ + 'id', + 'name', + 'created_at', + 'users_count', + ]; + + $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; + $groups = $groups->orderBy($sort, $order); + break; + } $total = $groups->count(); $groups = $groups->skip($offset)->take($limit)->get(); diff --git a/app/Http/Controllers/Api/ReportsController.php b/app/Http/Controllers/Api/ReportsController.php index 62d700799e..53e1805a2a 100644 --- a/app/Http/Controllers/Api/ReportsController.php +++ b/app/Http/Controllers/Api/ReportsController.php @@ -87,7 +87,7 @@ class ReportsController extends Controller switch ($request->input('sort')) { case 'created_by': - $actionlogs->OrderAdmin($order); + $actionlogs->OrderByCreatedBy($order); break; default: $sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at'; diff --git a/app/Http/Transformers/GroupsTransformer.php b/app/Http/Transformers/GroupsTransformer.php index bf7e2bfd70..03e96d5622 100644 --- a/app/Http/Transformers/GroupsTransformer.php +++ b/app/Http/Transformers/GroupsTransformer.php @@ -26,7 +26,10 @@ class GroupsTransformer 'name' => e($group->name), 'permissions' => json_decode($group->permissions), 'users_count' => (int) $group->users_count, - 'created_by' => ($group->admin) ? e($group->admin->present()->fullName) : null, + 'created_by' => ($group->adminuser) ? [ + 'id' => (int) $group->adminuser->id, + 'name'=> e($group->adminuser->present()->fullName()), + ] : null, 'created_at' => Helper::getFormattedDateObject($group->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($group->updated_at, 'datetime'), ]; diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index 93c64f6e1c..0831352b87 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -374,7 +374,7 @@ class Actionlog extends SnipeModel $this->source = $source; } - public function scopeOrderAdmin($query, $order) + public function scopeOrderByCreatedBy($query, $order) { return $query->leftJoin('users as admin_sort', 'action_logs.created_by', '=', 'admin_sort.id')->select('action_logs.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order); } diff --git a/app/Models/AssetMaintenance.php b/app/Models/AssetMaintenance.php index ecd05176f3..f629b03dfb 100644 --- a/app/Models/AssetMaintenance.php +++ b/app/Models/AssetMaintenance.php @@ -207,20 +207,6 @@ class AssetMaintenance extends Model implements ICompanyableChild } - /** - * Query builder scope to order on admin user - * - * @param \Illuminate\Database\Query\Builder $query Query builder instance - * @param string $order Order - * - * @return \Illuminate\Database\Query\Builder Modified query builder - */ - public function scopeOrderAdmin($query, $order) - { - return $query->leftJoin('users', 'asset_maintenances.user_id', '=', 'users.id') - ->orderBy('users.first_name', $order) - ->orderBy('users.last_name', $order); - } /** * Query builder scope to order on asset tag diff --git a/app/Models/Group.php b/app/Models/Group.php index c6e6e56039..b41670f2f5 100755 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -65,7 +65,7 @@ class Group extends SnipeModel * @since [v6.3.0] * @return \Illuminate\Database\Eloquent\Relations\Relation */ - public function admin() + public function adminuser() { return $this->belongsTo(\App\Models\User::class, 'created_by'); } @@ -81,4 +81,9 @@ class Group extends SnipeModel { return json_decode($this->permissions, true); } + + public function scopeOrderByCreatedBy($query, $order) + { + return $query->leftJoin('users as admin_sort', 'permission_groups.created_by', '=', 'admin_sort.id')->select('permission_groups.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order); + } } diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index 1b36e72e69..249afc00a8 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -232,7 +232,7 @@ trait Loggable $params = [ 'item' => $log->item, 'filename' => $log->filename, - 'admin' => $log->admin, + 'admin' => $log->adminuser, 'location' => ($location) ? $location->name : '', 'note' => $note, ]; diff --git a/app/Presenters/ActionlogPresenter.php b/app/Presenters/ActionlogPresenter.php index ebbe3d7823..37a1adbc28 100644 --- a/app/Presenters/ActionlogPresenter.php +++ b/app/Presenters/ActionlogPresenter.php @@ -7,7 +7,7 @@ namespace App\Presenters; */ class ActionlogPresenter extends Presenter { - public function admin() + public function adminuser() { if ($user = $this->model->user) { if (empty($user->deleted_at)) { diff --git a/resources/views/groups/index.blade.php b/resources/views/groups/index.blade.php index 5f96551d31..11c16a8b09 100755 --- a/resources/views/groups/index.blade.php +++ b/resources/views/groups/index.blade.php @@ -45,7 +45,7 @@ {{ trans('admin/groups/table.name') }} {{ trans('admin/groups/table.users') }} {{ trans('general.created_at') }} - {{ trans('general.created_by') }} + {{ trans('general.created_by') }} {{ trans('table.actions') }}