check permission before searching

This commit is contained in:
Denis Duliçi
2021-02-24 19:23:56 +03:00
parent 0d3f34ae8f
commit 24428977ef

View File

@ -19,123 +19,141 @@ class Search extends Controller
*/ */
public function index() public function index()
{ {
$user = user();
$search = new \stdClass(); $search = new \stdClass();
$search->results = []; $search->results = [];
$search->keyword = request('keyword'); $search->keyword = request('keyword');
$accounts = Account::enabled()->usingSearchString($search->keyword)->get(); if ($user->can('read-banking-accounts')) {
$accounts = Account::enabled()->usingSearchString($search->keyword)->get();
if ($accounts->count()) { if ($accounts->count()) {
foreach ($accounts as $account) { foreach ($accounts as $account) {
$search->results[] = (object) [ $search->results[] = (object) [
'id' => $account->id, 'id' => $account->id,
'name' => $account->name, 'name' => $account->name,
'type' => trans_choice('general.accounts', 1), 'type' => trans_choice('general.accounts', 1),
'color' => '#55588b', 'color' => '#55588b',
'href' => route('accounts.edit', $account->id), 'href' => route('accounts.edit', $account->id),
]; ];
}
} }
} }
$items = Item::enabled()->usingSearchString($search->keyword)->get(); if ($user->can('read-common-items')) {
$items = Item::enabled()->usingSearchString($search->keyword)->get();
if ($items->count()) { if ($items->count()) {
foreach ($items as $item) { foreach ($items as $item) {
$search->results[] = (object) [ $search->results[] = (object) [
'id' => $item->id, 'id' => $item->id,
'name' => $item->name, 'name' => $item->name,
'type' => trans_choice('general.items', 1), 'type' => trans_choice('general.items', 1),
'color' => '#efad32', 'color' => '#efad32',
'href' => route('items.edit', $item->id), 'href' => route('items.edit', $item->id),
]; ];
}
} }
} }
$invoices = Document::invoice()->usingSearchString($search->keyword)->get(); if ($user->can('read-sales-invoices')) {
$invoices = Document::invoice()->usingSearchString($search->keyword)->get();
if ($invoices->count()) { if ($invoices->count()) {
foreach ($invoices as $invoice) { foreach ($invoices as $invoice) {
$search->results[] = (object) [ $search->results[] = (object) [
'id' => $invoice->id, 'id' => $invoice->id,
'name' => $invoice->document_number . ' - ' . $invoice->contact_name, 'name' => $invoice->document_number . ' - ' . $invoice->contact_name,
'type' => trans_choice('general.invoices', 1), 'type' => trans_choice('general.invoices', 1),
'color' => '#6da252', 'color' => '#6da252',
'href' => route('invoices.show', $invoice->id), 'href' => route('invoices.show', $invoice->id),
]; ];
}
} }
} }
/* /*
$income_transactions = Transaction::income()->usingSearchString($keyword)->get(); if ($user->can('read-sales-revenues')) {
$income_transactions = Transaction::income()->usingSearchString($keyword)->get();
if ($income_transactions->count()) { if ($income_transactions->count()) {
foreach ($income_transactions as $transaction) { foreach ($income_transactions as $transaction) {
$results[] = (object)[ $results[] = (object)[
'id' => $transaction->id, 'id' => $transaction->id,
'name' => $transaction->contact_name, 'name' => $transaction->contact_name,
'type' => trans_choice('general.revenues', 1), 'type' => trans_choice('general.revenues', 1),
'color' => '#00c0ef', 'color' => '#00c0ef',
'href' => url('sales/revenues/' . $transaction->id), 'href' => url('sales/revenues/' . $transaction->id),
]; ];
}
} }
} }
*/ */
$customers = Contact::customer()->enabled()->usingSearchString($search->keyword)->get(); if ($user->can('read-sales-customers')) {
$customers = Contact::customer()->enabled()->usingSearchString($search->keyword)->get();
if ($customers->count()) { if ($customers->count()) {
foreach ($customers as $customer) { foreach ($customers as $customer) {
$search->results[] = (object) [ $search->results[] = (object) [
'id' => $customer->id, 'id' => $customer->id,
'name' => $customer->name, 'name' => $customer->name,
'type' => trans_choice('general.customers', 1), 'type' => trans_choice('general.customers', 1),
'color' => '#328aef', 'color' => '#328aef',
'href' => route('customers.show', $customer->id), 'href' => route('customers.show', $customer->id),
]; ];
}
} }
} }
$bills = Document::bill()->usingSearchString($search->keyword)->get(); if ($user->can('read-purchases-bills')) {
$bills = Document::bill()->usingSearchString($search->keyword)->get();
if ($bills->count()) { if ($bills->count()) {
foreach ($bills as $bill) { foreach ($bills as $bill) {
$search->results[] = (object) [ $search->results[] = (object) [
'id' => $bill->id, 'id' => $bill->id,
'name' => $bill->document_number . ' - ' . $bill->contact_name, 'name' => $bill->document_number . ' - ' . $bill->contact_name,
'type' => trans_choice('general.bills', 1), 'type' => trans_choice('general.bills', 1),
'color' => '#ef3232', 'color' => '#ef3232',
'href' => route('bills.show', $bill->id), 'href' => route('bills.show', $bill->id),
]; ];
}
} }
} }
/* /*
$payments = Transaction::expense()->usingSearchString($keyword)->get(); if ($user->can('read-purchases-payments')) {
$payments = Transaction::expense()->usingSearchString($keyword)->get();
if ($revenues->count()) { if ($revenues->count()) {
foreach ($revenues as $revenue) { foreach ($revenues as $revenue) {
$results[] = (object)[ $results[] = (object)[
'id' => $revenue->id, 'id' => $revenue->id,
'name' => $revenue->contact_name, 'name' => $revenue->contact_name,
'type' => trans_choice('general.revenues', 1), 'type' => trans_choice('general.revenues', 1),
'color' => '#00c0ef', 'color' => '#00c0ef',
'href' => url('sales/revenues/' . $revenue->id), 'href' => url('sales/revenues/' . $revenue->id),
]; ];
}
} }
} }
*/ */
$vendors = Contact::vendor()->enabled()->usingSearchString($search->keyword)->get(); if ($user->can('read-purchases-vendors')) {
$vendors = Contact::vendor()->enabled()->usingSearchString($search->keyword)->get();
if ($vendors->count()) { if ($vendors->count()) {
foreach ($vendors as $vendor) { foreach ($vendors as $vendor) {
$search->results[] = (object) [ $search->results[] = (object) [
'id' => $vendor->id, 'id' => $vendor->id,
'name' => $vendor->name, 'name' => $vendor->name,
'type' => trans_choice('general.vendors', 1), 'type' => trans_choice('general.vendors', 1),
'color' => '#efef32', 'color' => '#efef32',
'href' => route('vendors.show', $vendor->id), 'href' => route('vendors.show', $vendor->id),
]; ];
}
} }
} }