Skip to content

Commit

Permalink
Merge pull request #155 from inventree/search-permissions
Browse files Browse the repository at this point in the history
Wrap search queries in permission checks
  • Loading branch information
SchrodingersGat authored Jun 3, 2022
2 parents 62e6009 + 2a685a7 commit 51a90f5
Showing 1 changed file with 59 additions and 47 deletions.
106 changes: 59 additions & 47 deletions lib/widget/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "package:inventree/inventree/purchase_order.dart";
import "package:inventree/widget/part_list.dart";
import "package:inventree/widget/purchase_order_list.dart";
import "package:inventree/widget/refreshable_state.dart";
import "package:inventree/api.dart";
import "package:inventree/l10.dart";
import "package:inventree/inventree/part.dart";
import "package:inventree/inventree/stock.dart";
Expand Down Expand Up @@ -115,6 +116,8 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
*/
Future<void> search(String term) async {

var api = InvenTreeAPI();

setState(() {
// Do not search on an empty string
nPartResults = 0;
Expand All @@ -132,45 +135,53 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
}

// Search parts
InvenTreePart().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nPartResults = n;
nSearchResults++;
});
}
});
if (api.checkPermission("part", "view")) {
InvenTreePart().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nPartResults = n;
nSearchResults++;
});
}
});
}

// Search part categories
InvenTreePartCategory().count(searchQuery: term,).then((int n) {
if (term == searchController.text) {
setState(() {
nCategoryResults = n;
nSearchResults++;
});
}
});
if (api.checkPermission("part_category", "view")) {
InvenTreePartCategory().count(searchQuery: term,).then((int n) {
if (term == searchController.text) {
setState(() {
nCategoryResults = n;
nSearchResults++;
});
}
});
}

// Search stock items
InvenTreeStockItem().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nStockResults = n;
nSearchResults++;
});
}
});
if (api.checkPermission("stock", "view")) {
InvenTreeStockItem().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nStockResults = n;
nSearchResults++;
});
}
});
}

// Search stock locations
InvenTreeStockLocation().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nLocationResults = n;

nSearchResults++;
});
}
});
if (api.checkPermission("stock_location", "view")) {
InvenTreeStockLocation().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nLocationResults = n;

nSearchResults++;
});
}
});
}

// TDOO: Re-implement this once display for companies has been fixed
/*
Expand All @@ -188,20 +199,21 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
*/

// Search purchase orders
InvenTreePurchaseOrder().count(
searchQuery: term,
filters: {
"outstanding": "true"
}
).then((int n) {
if (term == searchController.text) {
setState(() {
nPurchaseOrderResults = n;
nSearchResults++;
});
}
});

if (api.checkPermission("purchase_order", "view")) {
InvenTreePurchaseOrder().count(
searchQuery: term,
filters: {
"outstanding": "true"
}
).then((int n) {
if (term == searchController.text) {
setState(() {
nPurchaseOrderResults = n;
nSearchResults++;
});
}
});
}
}

List<Widget> _tiles(BuildContext context) {
Expand Down

0 comments on commit 51a90f5

Please sign in to comment.