Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| GlobalController | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
156 | |
0.00% |
0 / 1 |
| DataTableSearch | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
156 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Admin; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Models\admin\Product; |
| 7 | use App\Models\Order; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Support\Facades\Schema; |
| 10 | |
| 11 | class GlobalController extends Controller |
| 12 | { |
| 13 | /** |
| 14 | * SECURITY FIX: the model class used to be built directly from the URL's |
| 15 | * ?model= value (e.g. "App\Models\" . $request->model), with no check on |
| 16 | * what was allowed. That meant anyone who could reach this route (and before |
| 17 | * the admin-role fix, that was any logged-in user) could pass ?model=admin\Employee |
| 18 | * or ?model=User and browse data — including things like employee salary or |
| 19 | * user accounts — through tables never meant to be searchable here. |
| 20 | * |
| 21 | * Only the two datatables this endpoint actually serves are allowed now. |
| 22 | * If you add a new admin datatable later, add its key here too. |
| 23 | */ |
| 24 | protected array $allowedModels = [ |
| 25 | 'Order' => Order::class, |
| 26 | 'admin\Product' => Product::class, |
| 27 | ]; |
| 28 | |
| 29 | public function DataTableSearch(Request $request) |
| 30 | { |
| 31 | $request = request(); |
| 32 | |
| 33 | if (! array_key_exists($request->model, $this->allowedModels)) { |
| 34 | abort(403, 'This data table is not allowed to be searched.'); |
| 35 | } |
| 36 | |
| 37 | $model = $this->allowedModels[$request->model]; |
| 38 | $query = $model::query(); |
| 39 | |
| 40 | // ডেট ফিল্টার |
| 41 | if ($request->has('date_from') && $request->has('date_to') && $request->date_from != '' && $request->date_to != '') { |
| 42 | $query->whereBetween('created_at', [$request->date_from, $request->date_to]); |
| 43 | } |
| 44 | |
| 45 | // check search value if found then search all column |
| 46 | if ($request->has('search') && $request->search != '') { |
| 47 | $table = (new $model)->getTable(); |
| 48 | $columns = Schema::getColumnListing($table); |
| 49 | $term = $request->search; |
| 50 | $query->where(function ($q) use ($columns, $term, $model) { |
| 51 | foreach ($columns as $column) { |
| 52 | $q->orWhere($column, 'LIKE', '%' . $term . '%'); |
| 53 | } |
| 54 | // also match the related customer name / phone where available |
| 55 | if (method_exists(new $model, 'customer')) { |
| 56 | $q->orWhereHas('customer', function ($c) use ($term) { |
| 57 | $c->where('name', 'LIKE', '%' . $term . '%') |
| 58 | ->orWhere('phone', 'LIKE', '%' . $term . '%'); |
| 59 | }); |
| 60 | } |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | if ($request->has('status') && $request->status != '') { |
| 65 | $query->where('status', $request->status); |
| 66 | } |
| 67 | // if ($request->has('order_by') && $request->order_by != '') { |
| 68 | // $query->orderBy($request->order_by, 'asc'); |
| 69 | // } |
| 70 | $data = $query->paginate($request->per_page ?? 3); |
| 71 | $viewName = str_replace('\\', '.', $request->model); |
| 72 | $html = view('admin.global.datatable.' . $viewName, compact('data'))->render(); |
| 73 | |
| 74 | return response()->json(['data' => $html]); |
| 75 | } |
| 76 | } |