A free open source IT asset/license management system
Allow non-report users to view assets, etc if they have permission
snipe8502a2291b5e
diff --git a/app/Http/Controllers/Api/ReportsController.php b/app/Http/Controllers/Api/ReportsController.phpindex 120b297cf7..2188cd848b 100644--- a/app/Http/Controllers/Api/ReportsController.php+++ b/app/Http/Controllers/Api/ReportsController.php@@ -2,11 +2,13 @@namespace App\Http\Controllers\Api;+use App\Helpers\Helper;use App\Http\Controllers\Controller;use App\Http\Requests\FilterRequest;use App\Http\Transformers\ActionlogsTransformer;use App\Models\Actionlog;use Illuminate\Http\JsonResponse;+use Illuminate\Support\Facades\Gate;class ReportsController extends Controller{@@ -19,31 +21,52 @@ class ReportsController extends Controller*/public function index(FilterRequest $request): JsonResponse|array{- $this->authorize('activity.view');- $actionlogs = Actionlog::with('item', 'user', 'adminuser', 'target', 'location');-- // This invokes the Searchable model trait scopeTextSearch and will handle input by search or by advanced search filter- if ($request->filled('filter') || $request->filled('search')) {- $actionlogs->TextSearch($request->input('filter') ? $request->input('filter') : $request->input('search'));+ // If the user doesn't have permission to view the item or the target,+ // then they shouldn't be able to see the activity log for that item or target,+ // but if they have the general activity view permission,+ // then they can see all activity logs regardless of the item or target.+ if ((! Gate::allows('activity.view')) && (($request->filled('target_type')) && ($request->filled('target_id'))) || (($request->filled('item_type')) && ($request->filled('item_id')))) {++ if (($request->filled('target_type')) && ($request->filled('target_id'))) {+ $target = Helper::normalizeFullModelName(request()->input('target_type'));+ $target::find(request()->input('target_id'))->withTrashed();+ $this->authorize('view', $target);+ }++ if (($request->filled('item_type')) && ($request->filled('item_id'))) {+ $item = Helper::normalizeFullModelName(request()->input('item_type'));+ $item::find(request()->input('item_id'))->withTrashed();+ $this->authorize('view', $item);+ }++ } else {+ $this->authorize('activity.view');}+ $actionlogs = Actionlog::with('item', 'user', 'adminuser', 'target', 'location');+if (($request->filled('target_type')) && ($request->filled('target_id'))) {$actionlogs = $actionlogs->where('target_id', '=', $request->input('target_id'))- ->where('target_type', '=', 'App\\Models\\'.ucwords($request->input('target_type')));+ ->where('target_type', '=', $target);}if (($request->filled('item_type')) && ($request->filled('item_id'))) {$actionlogs = $actionlogs->where(function ($query) use ($request) {$query->where('item_id', '=', $request->input('item_id'))- ->where('item_type', '=', 'App\\Models\\'.ucwords($request->input('item_type')))+ ->where('item_type', '=', Helper::normalizeFullModelName($request->input('item_type')))->orWhere(function ($query) use ($request) {$query->where('target_id', '=', $request->input('item_id'))- ->where('target_type', '=', 'App\\Models\\'.ucwords($request->input('item_type')));+ ->where('target_type', '=', Helper::normalizeFullModelName($request->input('item_type')));});});}+ // This invokes the Searchable model trait scopeTextSearch and will handle input by search or by advanced search filter+ if ($request->filled('filter') || $request->filled('search')) {+ $actionlogs->TextSearch($request->input('filter') ? $request->input('filter') : $request->input('search'));+ }+if ($request->filled('action_type')) {$actionlogs = $actionlogs->where('action_type', '=', $request->input('action_type'));}@@ -100,5 +123,6 @@ class ReportsController extends Controller$actionlogs = $actionlogs->skip($offset)->take($limit)->get();return response()->json((new ActionlogsTransformer)->transformActionlogs($actionlogs, $total), 200, ['Content-Type' => 'application/json;charset=utf8'], JSON_UNESCAPED_UNICODE);+}}