Added Gate integration into API transformers

This commit is contained in:
snipe 2017-02-08 18:24:55 -08:00
parent 959fafe04b
commit eea90dda78
10 changed files with 102 additions and 14 deletions

View file

@ -2,7 +2,7 @@
namespace App\Http\Transformers; namespace App\Http\Transformers;
use App\Models\Accessory; use App\Models\Accessory;
use App\Models\User; use Gate;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
class AccessoriesTransformer class AccessoriesTransformer
@ -19,7 +19,7 @@ class AccessoriesTransformer
public function transformAccessory (Accessory $accessory) public function transformAccessory (Accessory $accessory)
{ {
$transformed = [ $array = [
'id' => $accessory->id, 'id' => $accessory->id,
'name' => e($accessory->name), 'name' => e($accessory->name),
'company' => ($accessory->company) ? $accessory->company : null, 'company' => ($accessory->company) ? $accessory->company : null,
@ -36,7 +36,17 @@ class AccessoriesTransformer
'remaining_qty' => $accessory->numRemaining(), '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;
} }

View file

@ -3,6 +3,7 @@ namespace App\Http\Transformers;
use App\Models\AssetModel; use App\Models\AssetModel;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Gate;
class AssetModelsTransformer class AssetModelsTransformer
{ {
@ -19,7 +20,7 @@ class AssetModelsTransformer
public function transformAssetModel (AssetModel $assetmodel) public function transformAssetModel (AssetModel $assetmodel)
{ {
$transformed = [ $array = [
'id' => $assetmodel->id, 'id' => $assetmodel->id,
'name' => e($assetmodel->name), 'name' => e($assetmodel->name),
'manufacturer' => ($assetmodel->manufacturer_id) ? $assetmodel->manufacturer : null, 'manufacturer' => ($assetmodel->manufacturer_id) ? $assetmodel->manufacturer : null,
@ -33,7 +34,14 @@ class AssetModelsTransformer
'notes' => e($assetmodel->notes) '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;
} }

View file

@ -4,6 +4,8 @@ namespace App\Http\Transformers;
use App\Models\Asset; use App\Models\Asset;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use App\Http\Transformers\UsersTransformer; use App\Http\Transformers\UsersTransformer;
use Gate;
class AssetsTransformer class AssetsTransformer
{ {
@ -17,6 +19,7 @@ class AssetsTransformer
return (new DatatablesTransformer)->transformDatatables($array, $total); return (new DatatablesTransformer)->transformDatatables($array, $total);
} }
public function transformAsset (Asset $asset) public function transformAsset (Asset $asset)
{ {
$array = [ $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) { if ($asset->model->fieldset) {
foreach($asset->model->fieldset->fields as $field) { foreach($asset->model->fieldset->fields as $field) {
$fields_array = [$field->name => $asset->{$field->convertUnicodeDbSlug()}]; $fields_array = [$field->name => $asset->{$field->convertUnicodeDbSlug()}];
$array += $fields_array; $array += $fields_array;
//array_push($array, $fields_array);
} }
} }
return $array; return $array;
} }

View file

@ -4,6 +4,7 @@ namespace App\Http\Transformers;
use App\Models\Component; use App\Models\Component;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use App\Helpers\Helper; use App\Helpers\Helper;
use Gate;
class ComponentsTransformer class ComponentsTransformer
{ {
@ -19,7 +20,7 @@ class ComponentsTransformer
public function transformComponent (Component $component) public function transformComponent (Component $component)
{ {
$transformed = [ $array = [
'id' => $component->id, 'id' => $component->id,
'name' => e($component->name), 'name' => e($component->name),
@ -47,7 +48,16 @@ class ComponentsTransformer
] : null, ] : 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;
} }

View file

@ -4,6 +4,7 @@ namespace App\Http\Transformers;
use App\Models\Consumable; use App\Models\Consumable;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use App\Helpers\Helper; use App\Helpers\Helper;
use Gate;
class ConsumablesTransformer class ConsumablesTransformer
{ {
@ -19,7 +20,7 @@ class ConsumablesTransformer
public function transformConsumable (Consumable $consumable) public function transformConsumable (Consumable $consumable)
{ {
$transformed = [ $array = [
'category' => ($consumable->category) ? ['id' => $consumable->category->id, 'name' => $consumable->category->name] : null, 'category' => ($consumable->category) ? ['id' => $consumable->category->id, 'name' => $consumable->category->name] : null,
'company' => ($consumable->company) ? ['id' => $consumable->company->id, 'name' => $consumable->company->name] : null, 'company' => ($consumable->company) ? ['id' => $consumable->company->id, 'name' => $consumable->company->name] : null,
'id' => $consumable->id, 'id' => $consumable->id,
@ -35,7 +36,15 @@ class ConsumablesTransformer
'purchase_date' => $consumable->purchase_date, 'purchase_date' => $consumable->purchase_date,
'qty' => $consumable->qty, '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;
} }

View file

@ -3,6 +3,7 @@ namespace App\Http\Transformers;
use App\Models\Group; use App\Models\Group;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Gate;
class GroupsTransformer class GroupsTransformer
{ {
@ -25,6 +26,13 @@ class GroupsTransformer
'users_count' => $group->users_count, '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; return $array;
} }

View file

@ -2,7 +2,7 @@
namespace App\Http\Transformers; namespace App\Http\Transformers;
use App\Models\License; use App\Models\License;
use App\Models\LicenseSeat; use Gate;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
class LicensesTransformer class LicensesTransformer
@ -40,6 +40,15 @@ class LicensesTransformer
'created_at' => $license->created_at, '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; return $array;
} }

View file

@ -3,6 +3,7 @@ namespace App\Http\Transformers;
use App\Models\Location; use App\Models\Location;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Gate;
class LocationsTransformer class LocationsTransformer
{ {
@ -25,7 +26,7 @@ class LocationsTransformer
$assets_arr = ['id' => $asset->id]; $assets_arr = ['id' => $asset->id];
} }
$transformed = [ $array = [
'id' => e($location->id), 'id' => e($location->id),
'name' => e($location->name), 'name' => e($location->name),
'address' => e($location->address), 'address' => e($location->address),
@ -37,10 +38,14 @@ class LocationsTransformer
'assets' => $assets_arr, 'assets' => $assets_arr,
]; ];
$permissions_array['available_actions'] = [
'update' => Gate::allows('admin') ? true : false,
'delete' => Gate::allows('admin') ? true : false,
];
$array += $permissions_array;
return $array;
return $transformed;
} }

View file

@ -3,6 +3,7 @@ namespace App\Http\Transformers;
use App\Models\Statuslabel; use App\Models\Statuslabel;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Gate;
class StatuslabelsTransformer class StatuslabelsTransformer
{ {
@ -29,6 +30,12 @@ class StatuslabelsTransformer
'updated_at' => $statuslabel->updated_at, '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; return $array;
} }

View file

@ -4,6 +4,7 @@ namespace App\Http\Transformers;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\Integer;
use Gate;
class UsersTransformer class UsersTransformer
{ {
@ -43,6 +44,12 @@ class UsersTransformer
'company' => ($user->company) ? ['id' => $user->company->id,'name'=> e($user->company->name)] : null, '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; return $array;
} }