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;
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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}