From eea90dda788eb2fdf1e135e73ade8375d7e167c9 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 8 Feb 2017 18:24:55 -0800 Subject: [PATCH] Added Gate integration into API transformers --- .../Transformers/AccessoriesTransformer.php | 16 +++++++++++++--- .../Transformers/AssetModelsTransformer.php | 12 ++++++++++-- app/Http/Transformers/AssetsTransformer.php | 17 ++++++++++++++++- app/Http/Transformers/ComponentsTransformer.php | 14 ++++++++++++-- .../Transformers/ConsumablesTransformer.php | 13 +++++++++++-- app/Http/Transformers/GroupsTransformer.php | 8 ++++++++ app/Http/Transformers/LicensesTransformer.php | 11 ++++++++++- app/Http/Transformers/LocationsTransformer.php | 11 ++++++++--- .../Transformers/StatuslabelsTransformer.php | 7 +++++++ app/Http/Transformers/UsersTransformer.php | 7 +++++++ 10 files changed, 102 insertions(+), 14 deletions(-) diff --git a/app/Http/Transformers/AccessoriesTransformer.php b/app/Http/Transformers/AccessoriesTransformer.php index 95317a8b8..96963baff 100644 --- a/app/Http/Transformers/AccessoriesTransformer.php +++ b/app/Http/Transformers/AccessoriesTransformer.php @@ -2,7 +2,7 @@ namespace App\Http\Transformers; use App\Models\Accessory; -use App\Models\User; +use Gate; use Illuminate\Database\Eloquent\Collection; class AccessoriesTransformer @@ -19,7 +19,7 @@ class AccessoriesTransformer public function transformAccessory (Accessory $accessory) { - $transformed = [ + $array = [ 'id' => $accessory->id, 'name' => e($accessory->name), 'company' => ($accessory->company) ? $accessory->company : null, @@ -36,7 +36,17 @@ class AccessoriesTransformer 'remaining_qty' => $accessory->numRemaining(), ]; - return $transformed; + + $permissions_array['available_actions'] = [ + 'checkout' => Gate::allows('checkout', Accessory::class) ? true : false, + 'checkin' => Gate::allows('checkin', Accessory::class) ? true : false, + 'update' => Gate::allows('update', Accessory::class) ? true : false, + 'delete' => Gate::allows('delete', Accessory::class) ? true : false, + ]; + + $array += $permissions_array; + + return $array; } diff --git a/app/Http/Transformers/AssetModelsTransformer.php b/app/Http/Transformers/AssetModelsTransformer.php index 57c23a44d..dfebcf3e0 100644 --- a/app/Http/Transformers/AssetModelsTransformer.php +++ b/app/Http/Transformers/AssetModelsTransformer.php @@ -3,6 +3,7 @@ namespace App\Http\Transformers; use App\Models\AssetModel; use Illuminate\Database\Eloquent\Collection; +use Gate; class AssetModelsTransformer { @@ -19,7 +20,7 @@ class AssetModelsTransformer public function transformAssetModel (AssetModel $assetmodel) { - $transformed = [ + $array = [ 'id' => $assetmodel->id, 'name' => e($assetmodel->name), 'manufacturer' => ($assetmodel->manufacturer_id) ? $assetmodel->manufacturer : null, @@ -33,7 +34,14 @@ class AssetModelsTransformer 'notes' => e($assetmodel->notes) ]; - return $transformed; + + $permissions_array['available_actions'] = [ + 'update' => Gate::allows('admin') ? true : false, + 'delete' => Gate::allows('admin') ? true : false, + ]; + + $array += $permissions_array; + return $array; } diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 0144514e2..67442a0c1 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -4,6 +4,8 @@ namespace App\Http\Transformers; use App\Models\Asset; use Illuminate\Database\Eloquent\Collection; use App\Http\Transformers\UsersTransformer; +use Gate; + class AssetsTransformer { @@ -17,6 +19,7 @@ class AssetsTransformer return (new DatatablesTransformer)->transformDatatables($array, $total); } + public function transformAsset (Asset $asset) { $array = [ @@ -47,16 +50,28 @@ class AssetsTransformer ]; + + $permissions_array['available_actions'] = [ + 'checkout' => Gate::allows('checkout', Asset::class) ? true : false, + 'checkin' => Gate::allows('checkin', Asset::class) ? true : false, + 'update' => Gate::allows('update', Asset::class) ? true : false, + 'delete' => Gate::allows('delete', Asset::class) ? true : false, + ]; + + $array += $permissions_array; + + + if ($asset->model->fieldset) { foreach($asset->model->fieldset->fields as $field) { $fields_array = [$field->name => $asset->{$field->convertUnicodeDbSlug()}]; $array += $fields_array; - //array_push($array, $fields_array); } } + return $array; } diff --git a/app/Http/Transformers/ComponentsTransformer.php b/app/Http/Transformers/ComponentsTransformer.php index f1c4923d3..b164a0d21 100644 --- a/app/Http/Transformers/ComponentsTransformer.php +++ b/app/Http/Transformers/ComponentsTransformer.php @@ -4,6 +4,7 @@ namespace App\Http\Transformers; use App\Models\Component; use Illuminate\Database\Eloquent\Collection; use App\Helpers\Helper; +use Gate; class ComponentsTransformer { @@ -19,7 +20,7 @@ class ComponentsTransformer public function transformComponent (Component $component) { - $transformed = [ + $array = [ 'id' => $component->id, 'name' => e($component->name), @@ -47,7 +48,16 @@ class ComponentsTransformer ] : null, ]; - return $transformed; + + $permissions_array['available_actions'] = [ + 'checkout' => Gate::allows('checkout', Component::class) ? true : false, + 'checkin' => Gate::allows('checkin', Component::class) ? true : false, + 'update' => Gate::allows('update', Component::class) ? true : false, + 'delete' => Gate::allows('delete', Component::class) ? true : false, + ]; + $array += $permissions_array; + + return $array; } diff --git a/app/Http/Transformers/ConsumablesTransformer.php b/app/Http/Transformers/ConsumablesTransformer.php index bfbdd0dd4..b9ffba61f 100644 --- a/app/Http/Transformers/ConsumablesTransformer.php +++ b/app/Http/Transformers/ConsumablesTransformer.php @@ -4,6 +4,7 @@ namespace App\Http\Transformers; use App\Models\Consumable; use Illuminate\Database\Eloquent\Collection; use App\Helpers\Helper; +use Gate; class ConsumablesTransformer { @@ -19,7 +20,7 @@ class ConsumablesTransformer public function transformConsumable (Consumable $consumable) { - $transformed = [ + $array = [ 'category' => ($consumable->category) ? ['id' => $consumable->category->id, 'name' => $consumable->category->name] : null, 'company' => ($consumable->company) ? ['id' => $consumable->company->id, 'name' => $consumable->company->name] : null, 'id' => $consumable->id, @@ -35,7 +36,15 @@ class ConsumablesTransformer 'purchase_date' => $consumable->purchase_date, 'qty' => $consumable->qty, ]; - return $transformed; + + $permissions_array['available_actions'] = [ + 'checkout' => Gate::allows('checkout', Consumable::class) ? true : false, + 'checkin' => Gate::allows('checkin', Consumable::class) ? true : false, + 'update' => Gate::allows('update', Consumable::class) ? true : false, + 'delete' => Gate::allows('delete', Consumable::class) ? true : false, + ]; + $array += $permissions_array; + return $array; } diff --git a/app/Http/Transformers/GroupsTransformer.php b/app/Http/Transformers/GroupsTransformer.php index adf2d3d60..482efe8b1 100644 --- a/app/Http/Transformers/GroupsTransformer.php +++ b/app/Http/Transformers/GroupsTransformer.php @@ -3,6 +3,7 @@ namespace App\Http\Transformers; use App\Models\Group; use Illuminate\Database\Eloquent\Collection; +use Gate; class GroupsTransformer { @@ -25,6 +26,13 @@ class GroupsTransformer 'users_count' => $group->users_count, ]; + $permissions_array['available_actions'] = [ + 'update' => Gate::allows('superadmin') ? true : false, + 'delete' => Gate::allows('superadmin') ? true : false, + ]; + + $array += $permissions_array; + return $array; } diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.php index def34e7e7..55305709d 100644 --- a/app/Http/Transformers/LicensesTransformer.php +++ b/app/Http/Transformers/LicensesTransformer.php @@ -2,7 +2,7 @@ namespace App\Http\Transformers; use App\Models\License; -use App\Models\LicenseSeat; +use Gate; use Illuminate\Database\Eloquent\Collection; class LicensesTransformer @@ -40,6 +40,15 @@ class LicensesTransformer 'created_at' => $license->created_at, ]; + $permissions_array['available_actions'] = [ + 'checkout' => Gate::allows('checkout', License::class) ? true : false, + 'checkin' => Gate::allows('checkin', License::class) ? true : false, + 'update' => Gate::allows('update', License::class) ? true : false, + 'delete' => Gate::allows('delete', License::class) ? true : false, + ]; + + $array += $permissions_array; + return $array; } diff --git a/app/Http/Transformers/LocationsTransformer.php b/app/Http/Transformers/LocationsTransformer.php index 43e776e2f..c4c1b5e05 100644 --- a/app/Http/Transformers/LocationsTransformer.php +++ b/app/Http/Transformers/LocationsTransformer.php @@ -3,6 +3,7 @@ namespace App\Http\Transformers; use App\Models\Location; use Illuminate\Database\Eloquent\Collection; +use Gate; class LocationsTransformer { @@ -25,7 +26,7 @@ class LocationsTransformer $assets_arr = ['id' => $asset->id]; } - $transformed = [ + $array = [ 'id' => e($location->id), 'name' => e($location->name), 'address' => e($location->address), @@ -37,10 +38,14 @@ class LocationsTransformer 'assets' => $assets_arr, ]; + $permissions_array['available_actions'] = [ + 'update' => Gate::allows('admin') ? true : false, + 'delete' => Gate::allows('admin') ? true : false, + ]; + $array += $permissions_array; - - return $transformed; + return $array; } diff --git a/app/Http/Transformers/StatuslabelsTransformer.php b/app/Http/Transformers/StatuslabelsTransformer.php index 605dbb940..c513ba19d 100644 --- a/app/Http/Transformers/StatuslabelsTransformer.php +++ b/app/Http/Transformers/StatuslabelsTransformer.php @@ -3,6 +3,7 @@ namespace App\Http\Transformers; use App\Models\Statuslabel; use Illuminate\Database\Eloquent\Collection; +use Gate; class StatuslabelsTransformer { @@ -29,6 +30,12 @@ class StatuslabelsTransformer 'updated_at' => $statuslabel->updated_at, ]; + $permissions_array['available_actions'] = [ + 'edit' => Gate::allows('admin') ? true : false, + 'delete' => Gate::allows('admin') ? true : false, + ]; + $array += $permissions_array; + return $array; } diff --git a/app/Http/Transformers/UsersTransformer.php b/app/Http/Transformers/UsersTransformer.php index 2d8236ba4..6625e52ba 100644 --- a/app/Http/Transformers/UsersTransformer.php +++ b/app/Http/Transformers/UsersTransformer.php @@ -4,6 +4,7 @@ namespace App\Http\Transformers; use App\Models\User; use Illuminate\Database\Eloquent\Collection; use phpDocumentor\Reflection\Types\Integer; +use Gate; class UsersTransformer { @@ -43,6 +44,12 @@ class UsersTransformer 'company' => ($user->company) ? ['id' => $user->company->id,'name'=> e($user->company->name)] : null, ]; + $permissions_array['available_actions'] = [ + 'edit' => Gate::allows('edit', User::class) ? true : false, + 'delete' => Gate::allows('delete', User::class) ? true : false, + ]; + $array += $permissions_array; + return $array; }