mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-09-20 08:21:34 +02:00
Working on Products
This commit is contained in:
parent
0f19056b8f
commit
08c4579464
@ -73,17 +73,8 @@ class ClientController extends BaseController
|
||||
public function show(ShowClientRequest $request, Client $client)
|
||||
{
|
||||
|
||||
$data = [
|
||||
'client' => $client,
|
||||
'hashed_id' => $this->encodePrimarykey($client->id),
|
||||
'company' => $client->company(),
|
||||
'sizes' => Size::all(),
|
||||
];
|
||||
return $this->itemResponse($client);
|
||||
|
||||
return response()->json($data);
|
||||
|
||||
// return redirect()->route('api.clients.edit', ['id' => $this->encodePrimarykey($client->id)]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,14 +86,7 @@ class ClientController extends BaseController
|
||||
public function edit(EditClientRequest $request, Client $client)
|
||||
{
|
||||
|
||||
$data = [
|
||||
'client' => $client,
|
||||
'hashed_id' => $this->encodePrimarykey($client->id),
|
||||
'company' => $client->company(),
|
||||
'sizes' => Size::all(),
|
||||
];
|
||||
|
||||
return response()->json($data);
|
||||
return $this->itemResponse($client);
|
||||
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Filters\ProductFilters;
|
||||
use App\Http\Requests\Product\ShowProductRequest;
|
||||
use App\Http\Requests\Product\EditProductRequest;
|
||||
use App\Models\Product;
|
||||
use App\Transformers\ProductTransformer;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
@ -17,6 +19,16 @@ class ProductController extends BaseController
|
||||
|
||||
protected $entityTransformer = ProductTransformer::class;
|
||||
|
||||
/**
|
||||
* ProductController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
parent::__construct();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function index(ProductFilters $filters)
|
||||
@ -56,9 +68,9 @@ class ProductController extends BaseController
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
public function show(ShowProductRequest $request, Product $product)
|
||||
{
|
||||
//
|
||||
return $this->itemResponse($product);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,9 +79,9 @@ class ProductController extends BaseController
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
public function edit(EditProductRequest $request, Product $product)
|
||||
{
|
||||
//
|
||||
return $this->itemResponse($product);
|
||||
}
|
||||
|
||||
/**
|
||||
|
30
app/Http/Requests/Product/EditProductRequest.php
Normal file
30
app/Http/Requests/Product/EditProductRequest.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Product;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
|
||||
class EditProductRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->can('edit', $this->product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
31
app/Http/Requests/Product/ShowProductRequest.php
Normal file
31
app/Http/Requests/Product/ShowProductRequest.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Product;
|
||||
|
||||
//use Illuminate\Foundation\Http\FormRequest;
|
||||
use App\Http\Requests\Request;
|
||||
|
||||
class ShowProductRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->can('view', $this->product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class EntityPolicy
|
||||
@ -38,7 +39,8 @@ class EntityPolicy
|
||||
*/
|
||||
public function edit(User $user, $entity) : bool
|
||||
{
|
||||
|
||||
Log::error('trying to edit');
|
||||
|
||||
return ($user->isAdmin() && $entity->company_id == $user->companyId())
|
||||
|| ($user->hasPermission('edit_' . strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|
||||
|| $user->owns($entity);
|
||||
@ -56,7 +58,7 @@ class EntityPolicy
|
||||
*/
|
||||
public function view(User $user, $entity) : bool
|
||||
{
|
||||
|
||||
Log::error('trying to view');
|
||||
return ($user->isAdmin() && $entity->company_id == $user->companyId())
|
||||
|| ($user->hasPermission('view_' . strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|
||||
|| $user->owns($entity);
|
||||
|
33
app/Policies/ProductPolicy.php
Normal file
33
app/Policies/ProductPolicy.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class ProductPolicy extends EntityPolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
/**
|
||||
* Create a new policy instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has create permissions
|
||||
*
|
||||
* @param User $user
|
||||
* @return bool
|
||||
*/
|
||||
public function create(User $user) : bool
|
||||
{
|
||||
return $user->isAdmin() || $user->hasPermission('create_product');
|
||||
}
|
||||
|
||||
}
|
@ -3,7 +3,9 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Product;
|
||||
use App\Policies\ClientPolicy;
|
||||
use App\Policies\ProductPolicy;
|
||||
use Auth;
|
||||
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
@ -17,6 +19,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
*/
|
||||
protected $policies = [
|
||||
Client::class => ClientPolicy::class,
|
||||
Product::class => ProductPolicy::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -2,12 +2,13 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
use \App\Utils\Traits\MakesHash;
|
||||
use MakesHash;
|
||||
/**
|
||||
* This namespace is applied to your controller routes.
|
||||
*
|
||||
|
@ -5,6 +5,7 @@ namespace App\Transformers;
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\Client;
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
@ -13,7 +14,7 @@ use App\Utils\Traits\MakesHash;
|
||||
*/
|
||||
class CompanyTransformer extends EntityTransformer
|
||||
{
|
||||
trait MakesHash;
|
||||
use MakesHash;
|
||||
|
||||
/**
|
||||
* @SWG\Property(property="account_key", type="string", example="123456")
|
||||
@ -71,8 +72,8 @@ class CompanyTransformer extends EntityTransformer
|
||||
'size_id' => (int) $company->size_id,
|
||||
'industry_id' => (int) $company->industry_id,
|
||||
'settings' => $company->settings,
|
||||
'updated_at' => $user->updated_at,
|
||||
'deleted_at' => $user->deleted_at,
|
||||
'updated_at' => $company->updated_at,
|
||||
'deleted_at' => $company->deleted_at,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Transformers;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\Product;
|
||||
use App\Models\User;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
/**
|
||||
@ -11,6 +13,43 @@ use App\Utils\Traits\MakesHash;
|
||||
class ProductTransformer extends EntityTransformer
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
protected $defaultIncludes = [
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = [
|
||||
'company',
|
||||
'user'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
*
|
||||
* @return \League\Fractal\Resource\Collection
|
||||
*/
|
||||
public function includeUser(Product $product)
|
||||
{
|
||||
$transformer = new UserTransformer($this->serializer);
|
||||
|
||||
return $this->includeItem($product->user, $transformer, User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
*
|
||||
* @return \League\Fractal\Resource\Collection
|
||||
*/
|
||||
public function includeCompany(Product $product)
|
||||
{
|
||||
$transformer = new CompanyTransformer($this->serializer);
|
||||
|
||||
return $this->includeItem($product->company, $transformer, Company::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @SWG\Property(property="id", type="integer", example=1, readOnly=true)
|
||||
* @SWG\Property(property="product_key", type="string", example="Item")
|
||||
|
@ -50,10 +50,16 @@ trait MakesHash
|
||||
|
||||
public function decodePrimaryKey($value) : string
|
||||
{
|
||||
$hashids = new Hashids('', 10);
|
||||
try{
|
||||
$hashids = new Hashids('', 10);
|
||||
|
||||
$decoded_array = $hashids->decode($value);
|
||||
$decoded_array = $hashids->decode($value);
|
||||
|
||||
return $decoded_array[0];
|
||||
return $decoded_array[0];
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
return response()->json(['error'=>'Invalid primary key'],400);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user