1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Policies/EntityPolicy.php

73 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Policies;
use App\Models\User;
/**
* Class EntityPolicy.
*/
class EntityPolicy
{
/**
* Fires before any of the custom policy methods.
*
* 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
* @param $ability
2020-10-28 11:10:49 +01:00
* @return void /void
*/
public function before($user, $ability)
{
//if($user->isAdmin())
// return true;
}
/**
* 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->company_id == $user->companyId())
|| ($user->hasPermission('edit_'.strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|| ($user->hasPermission('edit_all') && $entity->company_id == $user->companyId())
|| $user->owns($entity)
|| $user->assigned($entity);
}
/**
* 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->company_id == $user->companyId())
|| ($user->hasPermission('view_'.strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|| ($user->hasPermission('view_all') && $entity->company_id == $user->companyId())
|| $user->owns($entity)
|| $user->assigned($entity);
}
}