2019-01-16 10:28:06 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-01-16 10:28:06 +01:00
|
|
|
|
|
|
|
namespace App\Policies;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class EntityPolicy.
|
2019-01-16 10:28:06 +01:00
|
|
|
*/
|
|
|
|
class EntityPolicy
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Fires before any of the custom policy methods.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* Only fires if true, if false we continue.....
|
|
|
|
*
|
|
|
|
* Do not use this function!!!! We MUST also check company_id,
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param User $user
|
2019-12-30 22:59:12 +01:00
|
|
|
* @param $ability
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return void /void
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
|
|
|
public function before($user, $ability)
|
|
|
|
{
|
|
|
|
//if($user->isAdmin())
|
|
|
|
// return true;
|
|
|
|
}
|
2019-01-16 10:28:06 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Checks if the user has edit permissions.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* 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->company_id == $user->companyId())
|
2020-09-06 11:38:10 +02:00
|
|
|
|| ($user->hasPermission('edit_'.strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|
2019-12-30 22:59:12 +01:00
|
|
|
|| ($user->hasPermission('edit_all') && $entity->company_id == $user->companyId())
|
|
|
|
|| $user->owns($entity)
|
|
|
|
|| $user->assigned($entity);
|
|
|
|
}
|
2019-01-20 06:00:50 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Checks if the user has view permissions.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* 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->company_id == $user->companyId())
|
2020-09-06 11:38:10 +02:00
|
|
|
|| ($user->hasPermission('view_'.strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|
2019-12-30 22:59:12 +01:00
|
|
|
|| ($user->hasPermission('view_all') && $entity->company_id == $user->companyId())
|
|
|
|
|| $user->owns($entity)
|
|
|
|
|| $user->assigned($entity);
|
|
|
|
}
|
2019-01-16 10:28:06 +01:00
|
|
|
}
|