1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00
invoiceninja/app/Services/RecurringExpenseService.php
David Bomba a9f2d0d855
This PR implements Create/View/Edit permissions based on ENTITY TYPE (ie invoice/expense/client). (#2150)
* migration for new permissions schema

* update permissions across data tables

* refactor migrations to prevent duplicate attribute

* update permissions in views

* Product Permissions

* permissions via controllers

* Refactor to use Laravel authorization gate

* Doc Blocks for EntityPolicy

* check permissions conditional on create new client

* Bug Fixes

* Data table permissions

* working on UI

* settings UI/UX finalised

* Datatable permissions

* remove legacy permissions

* permission fix for viewing client

* remove all instances of viewByOwner

* refactor after PR

* Bug fix for Functional test and implementation of Functional tests for Permissions

* fix for tests
2018-06-07 20:08:34 +10:00

83 lines
2.0 KiB
PHP

<?php
namespace App\Services;
use Utils;
use App\Models\Client;
use App\Models\Vendor;
use App\Ninja\Datatables\RecurringExpenseDatatable;
use App\Ninja\Repositories\RecurringExpenseRepository;
/**
* Class RecurringExpenseService.
*/
class RecurringExpenseService extends BaseService
{
/**
* @var RecurringExpenseRepository
*/
protected $recurringExpenseRepo;
/**
* @var DatatableService
*/
protected $datatableService;
/**
* CreditService constructor.
*
* @param RecurringExpenseRepository $creditRepo
* @param DatatableService $datatableService
*/
public function __construct(RecurringExpenseRepository $recurringExpenseRepo, DatatableService $datatableService)
{
$this->recurringExpenseRepo = $recurringExpenseRepo;
$this->datatableService = $datatableService;
}
/**
* @return CreditRepository
*/
protected function getRepo()
{
return $this->recurringExpenseRepo;
}
/**
* @param $data
* @param mixed $recurringExpense
*
* @return mixed|null
*/
public function save($data, $recurringExpense = false)
{
if (isset($data['client_id']) && $data['client_id']) {
$data['client_id'] = Client::getPrivateId($data['client_id']);
}
if (isset($data['vendor_id']) && $data['vendor_id']) {
$data['vendor_id'] = Vendor::getPrivateId($data['vendor_id']);
}
return $this->recurringExpenseRepo->save($data, $recurringExpense);
}
/**
* @param $clientPublicId
* @param $search
* @param mixed $userId
*
* @return \Illuminate\Http\JsonResponse
*/
public function getDatatable($search, $userId)
{
$query = $this->recurringExpenseRepo->find($search);
if (! Utils::hasPermission('view_expense')) {
$query->where('recurring_expenses.user_id', '=', Auth::user()->id);
}
return $this->datatableService->createDatatable(new RecurringExpenseDatatable(), $query);
}
}