Working on custom route model binding

This commit is contained in:
Hillel Coren 2016-04-28 15:16:33 +03:00
parent 38b6c94fe7
commit 9979eba5d9
18 changed files with 98 additions and 48 deletions

View File

@ -3,6 +3,7 @@
use App\Http\Middleware\PermissionsRequired; use App\Http\Middleware\PermissionsRequired;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Input;
use Auth; use Auth;
use Utils; use Utils;
@ -10,7 +11,7 @@ class BaseController extends Controller
{ {
use DispatchesJobs, AuthorizesRequests; use DispatchesJobs, AuthorizesRequests;
protected $entity; protected $entityType;
/** /**
* Setup the layout used by the controller. * Setup the layout used by the controller.
@ -25,17 +26,23 @@ class BaseController extends Controller
} }
protected function authorizeCreate() { protected function authorizeCreate() {
$this->authorize('create', $this->entity); $this->authorize('create', $this->entityType);
} }
/*
protected function authorizeUpdate($entity) {
$this->authorize('edit', $entity);
}
*/
protected function authorizeUpdate($input){ protected function authorizeUpdate($input){
$creating = empty($input['public_id']) || $input['public_id'] == '-1'; $creating = empty($input['public_id']) || $input['public_id'] == '-1';
if($creating){ if($creating){
$this->authorize('create', $this->entity); $this->authorize('create', $this->entityType);
} }
else{ else{
$className = Utils::getEntityName($this->entity); $className = Utils::getEntityName($this->entityType);
$object = call_user_func(array("App\\Models\\{$className}", 'scope'), $input['public_id'])->firstOrFail(); $object = call_user_func(array("App\\Models\\{$className}", 'scope'), $input['public_id'])->firstOrFail();
$this->authorize('edit', $object); $this->authorize('edit', $object);

View File

@ -28,6 +28,7 @@ use App\Models\Task;
use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\ClientRepository;
use App\Services\ClientService; use App\Services\ClientService;
use App\Http\Requests\ClientRequest;
use App\Http\Requests\CreateClientRequest; use App\Http\Requests\CreateClientRequest;
use App\Http\Requests\UpdateClientRequest; use App\Http\Requests\UpdateClientRequest;
@ -35,7 +36,7 @@ class ClientController extends BaseController
{ {
protected $clientService; protected $clientService;
protected $clientRepo; protected $clientRepo;
protected $entity = ENTITY_CLIENT; protected $entityType = ENTITY_CLIENT;
public function __construct(ClientRepository $clientRepo, ClientService $clientService) public function __construct(ClientRepository $clientRepo, ClientService $clientService)
{ {
@ -81,11 +82,7 @@ class ClientController extends BaseController
*/ */
public function store(CreateClientRequest $request) public function store(CreateClientRequest $request)
{ {
$data = $request->input(); $client = $this->clientService->save($request->input());
$this->authorizeUpdate($data);
$client = $this->clientService->save($data);
Session::flash('message', trans('texts.created_client')); Session::flash('message', trans('texts.created_client'));
@ -100,6 +97,7 @@ class ClientController extends BaseController
*/ */
public function show($publicId) public function show($publicId)
{ {
//$client = $request->entity()->load('conacts');
$client = Client::withTrashed()->scope($publicId)->with('contacts', 'size', 'industry')->firstOrFail(); $client = Client::withTrashed()->scope($publicId)->with('contacts', 'size', 'industry')->firstOrFail();
$this->authorize('view', $client); $this->authorize('view', $client);
@ -177,9 +175,9 @@ class ClientController extends BaseController
* @param int $id * @param int $id
* @return Response * @return Response
*/ */
public function edit($publicId) public function edit(ClientRequest $request)
{ {
$client = Client::scope($publicId)->with('contacts')->firstOrFail(); $client = $request->entity();
$this->authorize('edit', $client); $this->authorize('edit', $client);
@ -225,11 +223,7 @@ class ClientController extends BaseController
*/ */
public function update(UpdateClientRequest $request) public function update(UpdateClientRequest $request)
{ {
$data = $request->input(); $client = $this->clientService->save($request->input(), $request->entity());
$this->authorizeUpdate($data);
$client = $this->clientService->save($data);
Session::flash('message', trans('texts.updated_client')); Session::flash('message', trans('texts.updated_client'));

View File

@ -17,7 +17,7 @@ class CreditController extends BaseController
{ {
protected $creditRepo; protected $creditRepo;
protected $creditService; protected $creditService;
protected $entity = ENTITY_CREDIT; protected $entityType = ENTITY_CREDIT;
public function __construct(CreditRepository $creditRepo, CreditService $creditService) public function __construct(CreditRepository $creditRepo, CreditService $creditService)
{ {

View File

@ -15,7 +15,7 @@ use App\Ninja\Repositories\DocumentRepository;
class DocumentController extends BaseController class DocumentController extends BaseController
{ {
protected $documentRepo; protected $documentRepo;
protected $entity = ENTITY_DOCUMENT; protected $entityType = ENTITY_DOCUMENT;
public function __construct(DocumentRepository $documentRepo) public function __construct(DocumentRepository $documentRepo)
{ {

View File

@ -25,7 +25,7 @@ class ExpenseController extends BaseController
// Expenses // Expenses
protected $expenseRepo; protected $expenseRepo;
protected $expenseService; protected $expenseService;
protected $entity = ENTITY_EXPENSE; protected $entityType = ENTITY_EXPENSE;
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService) public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
{ {

View File

@ -37,7 +37,7 @@ class InvoiceController extends BaseController
protected $documentRepo; protected $documentRepo;
protected $invoiceService; protected $invoiceService;
protected $recurringInvoiceService; protected $recurringInvoiceService;
protected $entity = ENTITY_INVOICE; protected $entityType = ENTITY_INVOICE;
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService, DocumentRepository $documentRepo, RecurringInvoiceService $recurringInvoiceService) public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService, DocumentRepository $documentRepo, RecurringInvoiceService $recurringInvoiceService)
{ {

View File

@ -30,7 +30,7 @@ use App\Http\Requests\UpdatePaymentRequest;
class PaymentController extends BaseController class PaymentController extends BaseController
{ {
protected $entity = ENTITY_PAYMENT; protected $entityType = ENTITY_PAYMENT;
public function __construct(PaymentRepository $paymentRepo, InvoiceRepository $invoiceRepo, AccountRepository $accountRepo, ContactMailer $contactMailer, PaymentService $paymentService) public function __construct(PaymentRepository $paymentRepo, InvoiceRepository $invoiceRepo, AccountRepository $accountRepo, ContactMailer $contactMailer, PaymentService $paymentService)
{ {

View File

@ -33,7 +33,7 @@ class QuoteController extends BaseController
protected $invoiceRepo; protected $invoiceRepo;
protected $clientRepo; protected $clientRepo;
protected $invoiceService; protected $invoiceService;
protected $entity = ENTITY_INVOICE; protected $entityType = ENTITY_INVOICE;
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService) public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService)
{ {

View File

@ -22,7 +22,7 @@ class TaskController extends BaseController
{ {
protected $taskRepo; protected $taskRepo;
protected $taskService; protected $taskService;
protected $entity = ENTITY_TASK; protected $entityType = ENTITY_TASK;
public function __construct(TaskRepository $taskRepo, InvoiceRepository $invoiceRepo, TaskService $taskService) public function __construct(TaskRepository $taskRepo, InvoiceRepository $invoiceRepo, TaskService $taskService)
{ {

View File

@ -25,12 +25,12 @@ use App\Services\VendorService;
use App\Http\Requests\CreateVendorRequest; use App\Http\Requests\CreateVendorRequest;
use App\Http\Requests\UpdateVendorRequest; use App\Http\Requests\UpdateVendorRequest;
// vendor
class VendorController extends BaseController class VendorController extends BaseController
{ {
protected $vendorService; protected $vendorService;
protected $vendorRepo; protected $vendorRepo;
protected $entity = ENTITY_VENDOR; protected $entityType = ENTITY_VENDOR;
public function __construct(VendorRepository $vendorRepo, VendorService $vendorService) public function __construct(VendorRepository $vendorRepo, VendorService $vendorService)
{ {

View File

@ -0,0 +1,30 @@
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
use Input;
use Utils;
class BaseRequest extends Request {
protected $entityType;
private $entity;
public function entity()
{
if ($this->entity) {
return $this->entity;
}
//dd($this->clients);
$publicId = Input::get('public_id') ?: Input::get('id');
if ( ! $publicId) {
return null;
}
$class = Utils::getEntityClass($this->entityType);
$this->entity = $class::scope($publicId)->withTrashed()->firstOrFail();
return $this->entity;
}
}

View File

@ -0,0 +1,21 @@
<?php namespace App\Http\Requests;
class ClientRequest extends BaseRequest {
protected $entityType = ENTITY_CLIENT;
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [];
}
}

View File

@ -1,9 +1,6 @@
<?php namespace app\Http\Requests; <?php namespace App\Http\Requests;
use App\Http\Requests\Request; class CreateClientRequest extends ClientRequest
use Illuminate\Validation\Factory;
class CreateClientRequest extends Request
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.
@ -12,7 +9,7 @@ class CreateClientRequest extends Request
*/ */
public function authorize() public function authorize()
{ {
return true; return $this->user()->can('create', ENTITY_CLIENT);
} }
/** /**

View File

@ -1,9 +1,6 @@
<?php namespace app\Http\Requests; <?php namespace App\Http\Requests;
use App\Http\Requests\Request; class CreateCreditRequest extends BaseRequest
use Illuminate\Validation\Factory;
class CreateCreditRequest extends Request
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.
@ -12,7 +9,7 @@ class CreateCreditRequest extends Request
*/ */
public function authorize() public function authorize()
{ {
return true; return $this->user()->can('create', ENTITY_CREDIT);
} }
/** /**

View File

@ -1,9 +1,6 @@
<?php namespace app\Http\Requests; <?php namespace App\Http\Requests;
use App\Http\Requests\Request; class UpdateClientRequest extends ClientRequest
use Illuminate\Validation\Factory;
class UpdateClientRequest extends Request
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.
@ -12,7 +9,7 @@ class UpdateClientRequest extends Request
*/ */
public function authorize() public function authorize()
{ {
return true; return $this->user()->can('edit', $this->entity());
} }
/** /**

View File

@ -669,6 +669,11 @@ class Utils
return $year + $offset; return $year + $offset;
} }
public static function getEntityClass($entityType)
{
return 'App\\Models\\' . static::getEntityName($entityType);
}
public static function getEntityName($entityType) public static function getEntityName($entityType)
{ {
return ucwords(str_replace('_', ' ', $entityType)); return ucwords(str_replace('_', ' ', $entityType));

View File

@ -66,11 +66,13 @@ class ClientRepository extends BaseRepository
return $query; return $query;
} }
public function save($data) public function save($data, $client = null)
{ {
$publicId = isset($data['public_id']) ? $data['public_id'] : false; $publicId = isset($data['public_id']) ? $data['public_id'] : false;
if (!$publicId || $publicId == '-1') { if ($client) {
// do nothing
} if (!$publicId || $publicId == '-1') {
$client = Client::createNew(); $client = Client::createNew();
} else { } else {
$client = Client::scope($publicId)->with('contacts')->firstOrFail(); $client = Client::scope($publicId)->with('contacts')->firstOrFail();

View File

@ -30,13 +30,13 @@ class ClientService extends BaseService
return $this->clientRepo; return $this->clientRepo;
} }
public function save($data) public function save($data, $client = null)
{ {
if (Auth::user()->account->isNinjaAccount() && isset($data['plan'])) { if (Auth::user()->account->isNinjaAccount() && isset($data['plan'])) {
$this->ninjaRepo->updatePlanDetails($data['public_id'], $data); $this->ninjaRepo->updatePlanDetails($data['public_id'], $data);
} }
return $this->clientRepo->save($data); return $this->clientRepo->save($data, $client);
} }
public function getDatatable($search) public function getDatatable($search)