diff --git a/app/Http/Controllers/BaseController.php b/app/Http/Controllers/BaseController.php index 5124097636a9..66abc53e2459 100644 --- a/app/Http/Controllers/BaseController.php +++ b/app/Http/Controllers/BaseController.php @@ -2,13 +2,15 @@ use App\Http\Middleware\PermissionsRequired; use Illuminate\Foundation\Bus\DispatchesJobs; +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Auth; +use Utils; class BaseController extends Controller { - use DispatchesJobs; + use DispatchesJobs, AuthorizesRequests; - protected $model = 'App\Models\EntityModel'; + protected $entity; /** * Setup the layout used by the controller. @@ -22,39 +24,21 @@ class BaseController extends Controller } } - protected function checkViewPermission($object, &$response = null){ - if(!$object->canView()){ - $response = response('Unauthorized.', 401); - return false; - } - return true; + protected function authorizeCreate() { + $this->authorize('create', $this->entity); } - protected function checkEditPermission($object, &$response = null){ - if(!$object->canEdit()){ - $response = response('Unauthorized.', 401); - return false; - } - return true; - } - - protected function checkCreatePermission(&$response = null){ - if(!call_user_func(array($this->model, 'canCreate'))){ - $response = response('Unauthorized.', 401); - return false; - } - return true; - } - - protected function checkUpdatePermission($input, &$response = null){ + protected function authorizeUpdate($input){ $creating = empty($input['public_id']) || $input['public_id'] == '-1'; if($creating){ - return $this->checkCreatePermission($response); + $this->authorize('create', $this->entity); } else{ - $object = call_user_func(array($this->model, 'scope'), $input['public_id'])->firstOrFail(); - return $this->checkEditPermission($object, $response); + $className = Utils::getEntityName($this->entity); + + $object = call_user_func(array("App\\Models\\{$className}", 'scope'), $input['public_id'])->firstOrFail(); + $this->authorize('edit', $object); } } } diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 05226aecb084..f8690be44ea5 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -35,7 +35,7 @@ class ClientController extends BaseController { protected $clientService; protected $clientRepo; - protected $model = 'App\Models\Client'; + protected $entity = ENTITY_CLIENT; public function __construct(ClientRepository $clientRepo, ClientService $clientService) { @@ -83,9 +83,7 @@ class ClientController extends BaseController { $data = $request->input(); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $client = $this->clientService->save($data); @@ -104,17 +102,16 @@ class ClientController extends BaseController { $client = Client::withTrashed()->scope($publicId)->with('contacts', 'size', 'industry')->firstOrFail(); - if(!$this->checkViewPermission($client, $response)){ - return $response; - } + $this->authorize('view', $client); + $user = Auth::user(); Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT); $actionLinks = []; - if(Task::canCreate()){ + if($user->can('create', ENTITY_TASK)){ $actionLinks[] = ['label' => trans('texts.new_task'), 'url' => URL::to('/tasks/create/'.$client->public_id)]; } - if (Utils::hasFeature(FEATURE_QUOTES) && Invoice::canCreate()) { + if (Utils::hasFeature(FEATURE_QUOTES) && $user->can('create', ENTITY_INVOICE)) { $actionLinks[] = ['label' => trans('texts.new_quote'), 'url' => URL::to('/quotes/create/'.$client->public_id)]; } @@ -122,15 +119,15 @@ class ClientController extends BaseController $actionLinks[] = \DropdownButton::DIVIDER; } - if(Payment::canCreate()){ + if($user->can('create', ENTITY_PAYMENT)){ $actionLinks[] = ['label' => trans('texts.enter_payment'), 'url' => URL::to('/payments/create/'.$client->public_id)]; } - if(Credit::canCreate()){ + if($user->can('create', ENTITY_CREDIT)){ $actionLinks[] = ['label' => trans('texts.enter_credit'), 'url' => URL::to('/credits/create/'.$client->public_id)]; } - if(Expense::canCreate()){ + if($user->can('create', ENTITY_EXPENSE)){ $actionLinks[] = ['label' => trans('texts.enter_expense'), 'url' => URL::to('/expenses/create/0/'.$client->public_id)]; } @@ -157,9 +154,7 @@ class ClientController extends BaseController */ public function create() { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); if (Client::scope()->withTrashed()->count() > Auth::user()->getMaxNumClients()) { return View::make('error', ['hideHeader' => true, 'error' => "Sorry, you've exceeded the limit of ".Auth::user()->getMaxNumClients()." clients"]); @@ -187,9 +182,7 @@ class ClientController extends BaseController { $client = Client::scope($publicId)->with('contacts')->firstOrFail(); - if(!$this->checkEditPermission($client, $response)){ - return $response; - } + $this->authorize('edit', $client); $data = [ 'client' => $client, @@ -235,9 +228,7 @@ class ClientController extends BaseController { $data = $request->input(); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $client = $this->clientService->save($data); diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index 26085c3d6b32..23577f951389 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -17,7 +17,7 @@ class CreditController extends BaseController { protected $creditRepo; protected $creditService; - protected $model = 'App\Models\Credit'; + protected $entity = ENTITY_CREDIT; public function __construct(CreditRepository $creditRepo, CreditService $creditService) { @@ -57,9 +57,7 @@ class CreditController extends BaseController public function create($clientPublicId = 0) { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); $data = array( 'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId, @@ -78,9 +76,7 @@ class CreditController extends BaseController { $credit = Credit::scope($publicId)->firstOrFail(); - if(!$this->checkEditPermission($credit, $response)){ - return $response; - } + $this->authorize('edit', $credit); $credit->credit_date = Utils::fromSqlDate($credit->credit_date); diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php index 15f3ced57fd2..c0e25e773d11 100644 --- a/app/Http/Controllers/DocumentController.php +++ b/app/Http/Controllers/DocumentController.php @@ -15,7 +15,7 @@ use App\Ninja\Repositories\DocumentRepository; class DocumentController extends BaseController { protected $documentRepo; - protected $model = 'App\Models\Document'; + protected $entity = ENTITY_DOCUMENT; public function __construct(DocumentRepository $documentRepo) { @@ -29,9 +29,7 @@ class DocumentController extends BaseController $document = Document::scope($publicId) ->firstOrFail(); - if(!$this->checkViewPermission($document, $response)){ - return $response; - } + $this->authorize('view', $document); return static::getDownloadResponse($document); } @@ -67,9 +65,7 @@ class DocumentController extends BaseController $document = Document::scope($publicId) ->firstOrFail(); - if(!$this->checkViewPermission($document, $response)){ - return $response; - } + $this->authorize('view', $document); if(empty($document->preview)){ return Response::view('error', array('error'=>'Preview does not exist!'), 404); @@ -95,9 +91,7 @@ class DocumentController extends BaseController $name = substr($name, 0, -3); } - if(!$this->checkViewPermission($document, $response)){ - return $response; - } + $this->authorize('view', $document); if(!$document->isPDFEmbeddable()){ return Response::view('error', array('error'=>'Image does not exist!'), 404); @@ -118,9 +112,7 @@ class DocumentController extends BaseController return; } - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); $result = $this->documentRepo->upload(Input::all()['file'], $doc_array); diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index b8e82cddda48..e53122e46dcb 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -25,7 +25,7 @@ class ExpenseController extends BaseController // Expenses protected $expenseRepo; protected $expenseService; - protected $model = 'App\Models\Expense'; + protected $entity = ENTITY_EXPENSE; public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService) { @@ -71,9 +71,7 @@ class ExpenseController extends BaseController public function create($vendorPublicId = null, $clientPublicId = null) { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); if($vendorPublicId != 0) { $vendor = Vendor::scope($vendorPublicId)->with('vendorcontacts')->firstOrFail(); @@ -101,9 +99,7 @@ class ExpenseController extends BaseController { $expense = Expense::scope($publicId)->with('documents')->firstOrFail(); - if(!$this->checkEditPermission($expense, $response)){ - return $response; - } + $this->authorize('edit', $expense); $expense->expense_date = Utils::fromSqlDate($expense->expense_date); @@ -160,9 +156,7 @@ class ExpenseController extends BaseController $data = $request->input(); $data['documents'] = $request->file('documents'); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $expense = $this->expenseService->save($data, true); @@ -181,9 +175,7 @@ class ExpenseController extends BaseController $data = $request->input(); $data['documents'] = $request->file('documents'); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $expense = $this->expenseService->save($data); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 774ed7a7a70c..79ce2aee93b1 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -37,7 +37,7 @@ class InvoiceController extends BaseController protected $documentRepo; protected $invoiceService; protected $recurringInvoiceService; - protected $model = 'App\Models\Invoice'; + protected $entity = ENTITY_INVOICE; public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService, DocumentRepository $documentRepo, RecurringInvoiceService $recurringInvoiceService) { @@ -96,9 +96,7 @@ class InvoiceController extends BaseController ->withTrashed() ->firstOrFail(); - if(!$this->checkEditPermission($invoice, $response)){ - return $response; - } + $this->authorize('edit', $invoice); $entityType = $invoice->getEntityType(); @@ -233,9 +231,7 @@ class InvoiceController extends BaseController public function create($clientPublicId = 0, $isRecurring = false) { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); $account = Auth::user()->account; $entityType = $isRecurring ? ENTITY_RECURRING_INVOICE : ENTITY_INVOICE; @@ -404,9 +400,7 @@ class InvoiceController extends BaseController $data = $request->input(); $data['documents'] = $request->file('documents'); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $action = Input::get('action'); $entityType = Input::get('entityType'); @@ -445,9 +439,7 @@ class InvoiceController extends BaseController $data = $request->input(); $data['documents'] = $request->file('documents'); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $action = Input::get('action'); $entityType = Input::get('entityType'); diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index b339689d77d0..555f9965e382 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -32,7 +32,7 @@ use App\Http\Requests\UpdatePaymentRequest; class PaymentController extends BaseController { - protected $model = 'App\Models\Payment'; + protected $entity = ENTITY_PAYMENT; public function __construct(PaymentRepository $paymentRepo, InvoiceRepository $invoiceRepo, AccountRepository $accountRepo, ContactMailer $contactMailer, PaymentService $paymentService, UserMailer $userMailer) { @@ -74,9 +74,7 @@ class PaymentController extends BaseController public function create($clientPublicId = 0, $invoicePublicId = 0) { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); $invoices = Invoice::scope() ->where('is_recurring', '=', false) @@ -105,9 +103,7 @@ class PaymentController extends BaseController { $payment = Payment::scope($publicId)->firstOrFail(); - if(!$this->checkEditPermission($payment, $response)){ - return $response; - } + $this->authorize('edit', $payment); $payment->payment_date = Utils::fromSqlDate($payment->payment_date); @@ -647,9 +643,7 @@ class PaymentController extends BaseController { $input = $request->input(); - if(!$this->checkUpdatePermission($input, $response)){ - return $response; - } + $this->authorizeUpdate($data); $input['invoice_id'] = Invoice::getPrivateId($input['invoice']); $input['client_id'] = Client::getPrivateId($input['client']); @@ -669,9 +663,7 @@ class PaymentController extends BaseController { $input = $request->input(); - if(!$this->checkUpdatePermission($input, $response)){ - return $response; - } + $this->authorizeUpdate($data); $payment = $this->paymentRepo->save($input); diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 4aaa504bfd1b..e2228ab2a0c0 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -33,7 +33,7 @@ class QuoteController extends BaseController protected $invoiceRepo; protected $clientRepo; protected $invoiceService; - protected $model = 'App\Models\Invoice'; + protected $entity = ENTITY_INVOICE; public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService) { @@ -80,9 +80,7 @@ class QuoteController extends BaseController public function create($clientPublicId = 0) { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); if (!Utils::hasFeature(FEATURE_QUOTES)) { return Redirect::to('/invoices/create'); diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index 59f49da2514f..7ff19436ead2 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -22,7 +22,7 @@ class TaskController extends BaseController { protected $taskRepo; protected $taskService; - protected $model = 'App\Models\Task'; + protected $entity = ENTITY_TASK; public function __construct(TaskRepository $taskRepo, InvoiceRepository $invoiceRepo, TaskService $taskService) { @@ -85,9 +85,7 @@ class TaskController extends BaseController */ public function create($clientPublicId = 0) { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); $this->checkTimezone(); $data = [ @@ -117,9 +115,7 @@ class TaskController extends BaseController $task = Task::scope($publicId)->with('client', 'invoice')->withTrashed()->firstOrFail(); - if(!$this->checkEditPermission($task, $response)){ - return $response; - } + $this->authorize('edit', $task); $actions = []; if ($task->invoice) { @@ -184,9 +180,7 @@ class TaskController extends BaseController { $action = Input::get('action'); - if(!$this->checkUpdatePermission(array('public_id'=>$publicId)/* Hacky, but works */, $response)){ - return $response; - } + $this->authorizeUpdate(array('public_id'=>$publicId)/* Hacky, but works */); if (in_array($action, ['archive', 'delete', 'restore'])) { return self::bulk(); diff --git a/app/Http/Controllers/VendorController.php b/app/Http/Controllers/VendorController.php index b337c1df29e7..00f5bbe83e4d 100644 --- a/app/Http/Controllers/VendorController.php +++ b/app/Http/Controllers/VendorController.php @@ -30,7 +30,7 @@ class VendorController extends BaseController { protected $vendorService; protected $vendorRepo; - protected $model = 'App\Models\Vendor'; + protected $entity = ENTITY_VENDOR; public function __construct(VendorRepository $vendorRepo, VendorService $vendorService) { @@ -79,9 +79,7 @@ class VendorController extends BaseController { $data = $request->input(); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $vendor = $this->vendorService->save($data); @@ -100,9 +98,7 @@ class VendorController extends BaseController { $vendor = Vendor::withTrashed()->scope($publicId)->with('vendorcontacts', 'size', 'industry')->firstOrFail(); - if(!$this->checkViewPermission($vendor, $response)){ - return $response; - } + $this->authorize('view', $vendor); Utils::trackViewed($vendor->getDisplayName(), 'vendor'); @@ -131,9 +127,7 @@ class VendorController extends BaseController */ public function create() { - if(!$this->checkCreatePermission($response)){ - return $response; - } + $this->authorizeCreate(); if (Vendor::scope()->count() > Auth::user()->getMaxNumVendors()) { return View::make('error', ['hideHeader' => true, 'error' => "Sorry, you've exceeded the limit of ".Auth::user()->getMaxNumVendors()." vendors"]); @@ -161,9 +155,7 @@ class VendorController extends BaseController { $vendor = Vendor::scope($publicId)->with('vendorcontacts')->firstOrFail(); - if(!$this->checkEditPermission($vendor, $response)){ - return $response; - } + $this->authorize('edit', $vendor) $data = [ 'vendor' => $vendor, @@ -203,9 +195,7 @@ class VendorController extends BaseController { $data = $request->input(); - if(!$this->checkUpdatePermission($data, $response)){ - return $response; - } + $this->authorizeUpdate($data); $vendor = $this->vendorService->save($data); diff --git a/app/Models/Document.php b/app/Models/Document.php index 96051d9db1b2..0d7fc049aac8 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -222,20 +222,6 @@ class Document extends EntityModel return $document; } - - public static function canCreate(){ - return true; - } - - public static function canViewItem($document){ - if(Auth::user()->hasPermission('view_all'))return true; - if($document->expense){ - if($document->expense->invoice)return $document->expense->invoice->canView(); - return $document->expense->canView(); - } - if($document->invoice)return $document->invoice->canView(); - return Auth::user()->id == $item->user_id; - } } Document::deleted(function ($document) { diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index 53bb1d0d1a48..8d0da39d3fab 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -118,56 +118,4 @@ class EntityModel extends Eloquent $name = $parts[count($parts)-1]; return strtolower($name) . '_id'; } - - public static function canCreate() { - return Auth::user()->hasPermission('create_all'); - } - - public function canEdit() { - return static::canEditItem($this); - } - - public static function canEditItem($item) { - return Auth::user()->hasPermission('edit_all') || (isset($item->user_id) && Auth::user()->id == $item->user_id); - } - - public static function canEditItemById($item_id) { - if(Auth::user()->hasPermission('edit_all')) { - return true; - } - - return static::whereId($item_id)->first()->user_id == Auth::user()->id; - } - - public static function canEditItemByOwner($user_id) { - if(Auth::user()->hasPermission('edit_all')) { - return true; - } - - return Auth::user()->id == $user_id; - } - - public function canView() { - return static::canViewItem($this); - } - - public static function canViewItem($item) { - return Auth::user()->hasPermission('view_all') || (isset($item->user_id) && Auth::user()->id == $item->user_id); - } - - public static function canViewItemById($item_id) { - if(Auth::user()->hasPermission('view_all')) { - return true; - } - - return static::whereId($item_id)->first()->user_id == Auth::user()->id; - } - - public static function canViewItemByOwner($user_id) { - if(Auth::user()->hasPermission('view_all')) { - return true; - } - - return Auth::user()->id == $user_id; - } } diff --git a/app/Models/Product.php b/app/Models/Product.php index 4098c67063d2..8de7c7ac5b2c 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -22,8 +22,4 @@ class Product extends EntityModel { return $this->belongsTo('App\Models\TaxRate'); } - - public function canEdit() { - return Auth::user()->hasPermission('admin'); - } } diff --git a/app/Models/TaxRate.php b/app/Models/TaxRate.php index cf0a576a8f0d..72ad266b07d8 100644 --- a/app/Models/TaxRate.php +++ b/app/Models/TaxRate.php @@ -17,8 +17,4 @@ class TaxRate extends EntityModel { return ENTITY_TAX_RATE; } - - public function canEdit() { - return Auth::user()->hasPermission('admin'); - } } diff --git a/app/Models/User.php b/app/Models/User.php index 32a4960a8482..71069d25821c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,20 +7,22 @@ use App\Libraries\Utils; use App\Events\UserSettingsChanged; use App\Events\UserSignedUp; use Illuminate\Auth\Authenticatable; +use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; +use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Database\Eloquent\SoftDeletes; -class User extends Model implements AuthenticatableContract, CanResetPasswordContract { +class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { public static $all_permissions = array( 'create_all' => 0b0001, 'view_all' => 0b0010, 'edit_all' => 0b0100, ); - use Authenticatable, CanResetPassword; + use Authenticatable, Authorizable, CanResetPassword; /** * The database table used by the model. @@ -326,6 +328,10 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon return false; } + + public function owns($entity) { + return !empty($entity->user_id) && $entity->user_id == $this->id; + } } User::updating(function ($user) { diff --git a/app/Ninja/Repositories/ExpenseRepository.php b/app/Ninja/Repositories/ExpenseRepository.php index aae7729c474d..3492740d6a94 100644 --- a/app/Ninja/Repositories/ExpenseRepository.php +++ b/app/Ninja/Repositories/ExpenseRepository.php @@ -2,6 +2,7 @@ use DB; use Utils; +use Auth; use App\Models\Expense; use App\Models\Vendor; use App\Models\Document; @@ -159,14 +160,14 @@ class ExpenseRepository extends BaseRepository $document_ids = !empty($input['document_ids'])?array_map('intval', $input['document_ids']):array();; foreach ($document_ids as $document_id){ $document = Document::scope($document_id)->first(); - if($document && !$checkSubPermissions || $document->canEdit()){ + if($document && !$checkSubPermissions || Auth::user()->can('edit', $document)){ $document->invoice_id = null; $document->expense_id = $expense->id; $document->save(); } } - if(!empty($input['documents']) && Document::canCreate()){ + if(!empty($input['documents']) && Auth::user()->can('create', ENTITY_DOCUMENT)){ // Fallback upload $doc_errors = array(); foreach($input['documents'] as $upload){ diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index d57710d4689b..710b9dc44191 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -3,6 +3,7 @@ use DB; use Utils; use Session; +use Auth; use App\Models\Invoice; use App\Models\InvoiceItem; use App\Models\Invitation; @@ -475,7 +476,7 @@ class InvoiceRepository extends BaseRepository $document_ids = !empty($data['document_ids'])?array_map('intval', $data['document_ids']):array();; foreach ($document_ids as $document_id){ $document = Document::scope($document_id)->first(); - if($document && !$checkSubPermissions || $document->canEdit()){ + if($document && !$checkSubPermissions || Auth::user()->can('edit', $document)){ if($document->invoice_id && $document->invoice_id != $invoice->id){ // From a clone @@ -489,7 +490,7 @@ class InvoiceRepository extends BaseRepository } } - if(!empty($data['documents']) && Document::canCreate()){ + if(!empty($data['documents']) && Auth::user()->can('create', ENTITY_DOCUMENT)){ // Fallback upload $doc_errors = array(); foreach($data['documents'] as $upload){ @@ -528,7 +529,7 @@ class InvoiceRepository extends BaseRepository $task = false; if (isset($item['task_public_id']) && $item['task_public_id']) { $task = Task::scope($item['task_public_id'])->where('invoice_id', '=', null)->firstOrFail(); - if(!$checkSubPermissions || $task->canEdit()){ + if(!$checkSubPermissions || Auth::user()->can('edit', $task)){ $task->invoice_id = $invoice->id; $task->client_id = $invoice->client_id; $task->save(); @@ -538,7 +539,7 @@ class InvoiceRepository extends BaseRepository $expense = false; if (isset($item['expense_public_id']) && $item['expense_public_id']) { $expense = Expense::scope($item['expense_public_id'])->where('invoice_id', '=', null)->firstOrFail(); - if(!$checkSubPermissions || $expense->canEdit()){ + if(!$checkSubPermissions || Auth::user()->can('edit', $expense)){ $expense->invoice_id = $invoice->id; $expense->client_id = $invoice->client_id; $expense->save(); @@ -549,7 +550,7 @@ class InvoiceRepository extends BaseRepository if (\Auth::user()->account->update_products && ! strtotime($productKey)) { $product = Product::findProductByKey($productKey); if (!$product) { - if(!$checkSubPermissions || Product::canCreate()){ + if(!$checkSubPermissions || Auth::user()->can('create', ENTITY_PRODUCT)){ $product = Product::createNew(); $product->product_key = trim($item['product_key']); } @@ -557,7 +558,7 @@ class InvoiceRepository extends BaseRepository $product = null; } } - if($product && (!$checkSubPermissions || $product->canEdit())){ + if($product && (!$checkSubPermissions || Auth::user()->can('edit', $product))){ $product->notes = ($task || $expense) ? '' : $item['notes']; $product->cost = $expense ? 0 : $item['cost']; $product->save(); diff --git a/app/Policies/ClientPolicy.php b/app/Policies/ClientPolicy.php new file mode 100644 index 000000000000..4610c139fbfa --- /dev/null +++ b/app/Policies/ClientPolicy.php @@ -0,0 +1,5 @@ +hasPermission('view_all'))return true; + if($document->expense){ + if($document->expense->invoice)return $user->can('view', $document->expense->invoice); + return $user->can('view', $document->expense); + } + if($document->invoice)return $user->can('view', $document->invoice); + + return $user->owns($item); + } +} \ No newline at end of file diff --git a/app/Policies/EntityPolicy.php b/app/Policies/EntityPolicy.php new file mode 100644 index 000000000000..4c5e8ded9d07 --- /dev/null +++ b/app/Policies/EntityPolicy.php @@ -0,0 +1,33 @@ +hasPermission('create_all'); + } + + public static function edit($user, $item) { + return $user->hasPermission('edit_all') || $user->owns($item); + } + + public static function view($user, $item) { + return $user->hasPermission('view_all') || $user->owns($item); + } + + public static function viewByOwner($user, $ownerUserId) { + return $user->hasPermission('view_all') || $user->id == $ownerUserId; + } + + public static function editByOwner($user, $ownerUserId) { + return $user->hasPermission('edit_all') || $user->id == $ownerUserId; + } +} \ No newline at end of file diff --git a/app/Policies/ExpensePolicy.php b/app/Policies/ExpensePolicy.php new file mode 100644 index 000000000000..4fdac4d627aa --- /dev/null +++ b/app/Policies/ExpensePolicy.php @@ -0,0 +1,5 @@ +hasPermission('admin'); + } +} \ No newline at end of file diff --git a/app/Policies/TaskPolicy.php b/app/Policies/TaskPolicy.php new file mode 100644 index 000000000000..b1fbe2902974 --- /dev/null +++ b/app/Policies/TaskPolicy.php @@ -0,0 +1,5 @@ +hasPermission('admin'); + } +} \ No newline at end of file diff --git a/app/Policies/VendorPolicy.php b/app/Policies/VendorPolicy.php new file mode 100644 index 000000000000..681cdcb50240 --- /dev/null +++ b/app/Policies/VendorPolicy.php @@ -0,0 +1,5 @@ + '.trans("texts.$types").''; $items = []; - if(Auth::user()->hasPermission('create_all')){ - $items[] = '