1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Policies/CompanyPolicy.php

67 lines
2.0 KiB
PHP
Raw Normal View History

2019-06-25 23:55:04 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-06-25 23:55:04 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-06-25 23:55:04 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Policies;
use App\Models\Company;
use App\Models\User;
use Illuminate\Support\Facades\Log;
/**
* Class CompanyPolicy.
2019-06-25 23:55:04 +02:00
*/
class CompanyPolicy extends EntityPolicy
{
/**
* Checks if the user has create permissions.
*
* @param User $user
* @return bool
*/
public function create(User $user) : bool
{
return $user->isAdmin() || $user->hasPermission('create_company') || $user->hasPermission('create_all');
}
2019-06-25 23:55:04 +02:00
/**
* Checks if the user has view permissions.
*
* We MUST also check that the user can both view a entity and also check the entity belongs to the users company!!!!!!
* @param User $user
* @param $entity
* @return bool
*/
public function view(User $user, $entity) : bool
{
return ($user->isAdmin() && $entity->id == $user->companyId())
|| ($user->hasPermission('view_'.strtolower(class_basename($entity))) && $entity->id == $user->companyId())
|| ($user->hasPermission('view_all') && $entity->id == $user->companyId())
|| $user->owns($entity);
}
2019-06-25 23:55:04 +02:00
/**
* Checks if the user has edit permissions.
*
* We MUST also check that the user can both edit a entity and also check the entity belongs to the users company!!!!!!
*
* @param User $user
* @param $entity
* @return bool
*/
public function edit(User $user, $entity) : bool
{
return ($user->isAdmin() && $entity->id == $user->companyId())
|| ($user->hasPermission('edit_'.strtolower(class_basename($entity))) && $entity->id == $user->companyId())
|| ($user->hasPermission('edit_all') && $entity->id == $user->companyId())
|| $user->owns($entity);
}
2019-06-25 23:55:04 +02:00
}