forked from grokability/snipe-it
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredefinedFilter.php
More file actions
205 lines (169 loc) · 6.96 KB
/
PredefinedFilter.php
File metadata and controls
205 lines (169 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
class PredefinedFilter extends Model
{
use HasFactory;
use SoftDeletes;
use ValidatingTrait;
protected $casts = [
"filter_data" => "array",
"is_public" => "boolean"
];
protected $fillable = [
'name',
'created_by',
'filter_data',
'is_public',
'object_type',
];
protected $rules = [
'name' => ['required', 'string', 'max:191'],
'filter_data' => ['required', 'array'],
'permissions' => ['sometimes', 'array'],
'is_public' => 'sometimes|boolean'
];
public function permissionGroups()
{
return $this->belongsToMany(
PermissionGroup::class,
'predefined_filter_permissions',
'predefined_filter_id',
'permission_group_id'
);
}
public function createdBy()
{
return $this->belongsTo(User::class, 'created_by');
}
public function userHasPermission(User $user, string $action): bool
{
// Give the superuser all permissions no matter in which groups he is
if ($user->isSuperUser()) {
return true;
}
// If filter is private AND is_owner AND action != create he can do everything
// such as create private, edit and delete
// note the 'create' permission is only for creating public filters.
if ($user->id == $this->created_by && !$this->is_public && $action != 'create') {
return true;
}
switch ($action) {
case 'create':
return $user->hasAccess('predefinedFilter.create');
case 'view':
if ($this->checkPermissions($user, 'view')) {
return true;
}
//cascade for edit and view
return $this->userHasPermission($user, 'edit') || $this->userHasPermission($user, 'delete');
case 'edit':
case 'delete':
// If filter is private AND is_owner AND action != create he can do everything
// such as create private, edit and delete
// note the 'create' permission is only for creating public filters.
if ($user->id == $this->created_by && !$this->is_public && $action != 'create') {
return true;
}
return $this->checkPermissions($user, $action);
default:
return false;
} //end switch
}
private function checkPermissions(User $user, $action): bool
{
$userGroupIds = $user->groups()->pluck('id')->toArray();
if (!$user->relationLoaded('groups')) {
$user->load('groups');
}
foreach ($this->permissionGroups as $group) {
if (in_array($group->id, $userGroupIds)) {
$permissions = json_decode($group->permissions, true);
if (isset($permissions["predefinedFilter.$action"]) && $permissions["predefinedFilter.$action"] == '1') {
return true;
}
}
}
return false;
}
protected function applyArrayOrScalarFilter(Builder $assets, array $filter, string $key, string $column): void
{
if (!empty($filter[$key])) {
$values = is_array($filter[$key]) ? $filter[$key] : [$filter[$key]];
$assets->whereIn($column, $values);
}
}
protected function applyLikeFilter(Builder $assets, array $filter, string $key, string $column): void
{
if (!empty($filter[$key])) {
$assets->where($column, 'LIKE', '%' . $filter[$key] . '%');
}
}
protected function applyDateRangeFilter(Builder $assets, array $filter, string $field): void
{
$startKey = $field . '_start';
$endKey = $field . '_end';
$start = $filter[$startKey] ?? null;
$end = $filter[$endKey] ?? null;
if (!$start && !$end) {
return;
}
$table = $assets->getModel()->getTable();
$column = $table . '.' . $field;
if ($start) {
$assets->whereDate($column, '>=', $start);
}
if ($end) {
$assets->whereDate($column, '<=', $end);
}
}
public function filterAssets(Builder $assets)
{
$filter = $this->filter_data ?? [];
$this->applyArrayOrScalarFilter($assets, $filter, 'company_id', 'assets.company_id');
$this->applyArrayOrScalarFilter($assets, $filter, 'location_id', 'location_id');
$this->applyArrayOrScalarFilter($assets, $filter, 'rtd_location_id', 'rtd_location_id');
$this->applyArrayOrScalarFilter($assets, $filter, 'supplier_id', 'supplier_id');
$this->applyArrayOrScalarFilter($assets, $filter, 'model_id', 'model_id');
$this->applyArrayOrScalarFilter($assets, $filter, 'status_id', 'status_id');
if (!empty($filter['category_id']) || !empty($filter['manufacturer_id'])) {
$assets->leftJoin('models', 'assets.model_id', '=', 'models.id');
$this->applyArrayOrScalarFilter($assets, $filter, 'category_id', 'models.category_id');
$this->applyArrayOrScalarFilter($assets, $filter, 'manufacturer_id', 'models.manufacturer_id');
}
$this->applyDateRangeFilter($assets, $filter, 'created_at');
$this->applyDateRangeFilter($assets, $filter, 'purchase_date');
$this->applyDateRangeFilter($assets, $filter, 'last_checkout');
$this->applyDateRangeFilter($assets, $filter, 'last_checkin');
$this->applyDateRangeFilter($assets, $filter, 'expected_checkin');
$this->applyDateRangeFilter($assets, $filter, 'asset_eol_date');
$this->applyDateRangeFilter($assets, $filter, 'last_audit_date');
$this->applyDateRangeFilter($assets, $filter, 'next_audit_date');
$this->applyDateRangeFilter($assets, $filter, 'updated_at');
$this->applyLikeFilter($assets, $filter, 'name', 'assets.name');
$this->applyLikeFilter($assets, $filter, 'asset_tag', 'assets.asset_tag');
$this->applyLikeFilter($assets, $filter, 'serial', 'assets.serial');
// Custom fields
if (!empty($filter['custom_fields']) && is_array($filter['custom_fields'])) {
foreach ($filter['custom_fields'] as $key => $value) {
$assets->where("assets.$key", '=', $value);
}
}
return $assets;
}
public function checkIfNameAlreadyExists(string $name, int $id=null): bool
{
if ($id === null) {
$query = $this->where('name', '=', $name);
return $query->exists();
}
$query = $this->where('name', '=', $name);
$query->where('id', '<>', $id);
return sizeof($query->get()->toArray()) > 1;
}
}