2019-06-25 23:55:04 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-06-25 23:55:04 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-06-25 23:55:04 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-06-25 23:55:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Policies;
|
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class CompanyPolicy.
|
2019-06-25 23:55:04 +02:00
|
|
|
*/
|
|
|
|
class CompanyPolicy extends EntityPolicy
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Checks if the user has create permissions.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function create(User $user): bool
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
return $user->isAdmin() || $user->hasPermission('create_company') || $user->hasPermission('create_all');
|
|
|
|
}
|
2019-06-25 23:55:04 +02: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
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function view(User $user, $entity): bool
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
return ($user->isAdmin() && $entity->id == $user->companyId())
|
2020-09-06 11:38:10 +02:00
|
|
|
|| ($user->hasPermission('view_'.strtolower(class_basename($entity))) && $entity->id == $user->companyId())
|
2023-01-31 12:21:23 +01:00
|
|
|
// || ($user->hasPermission('view_all') && $entity->id == $user->companyId())
|
2023-08-07 15:00:19 +02:00
|
|
|
|| $user->owns($entity)
|
|
|
|
|| $user->companyId() == $entity->id;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-06-25 23:55:04 +02: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
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function edit(User $user, $entity): bool
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
return ($user->isAdmin() && $entity->id == $user->companyId())
|
2020-09-06 11:38:10 +02:00
|
|
|
|| ($user->hasPermission('edit_'.strtolower(class_basename($entity))) && $entity->id == $user->companyId())
|
2023-01-31 12:21:23 +01:00
|
|
|
// || ($user->hasPermission('edit_all') && $entity->id == $user->companyId())
|
2019-12-30 22:59:12 +01:00
|
|
|
|| $user->owns($entity);
|
|
|
|
}
|
2019-06-25 23:55:04 +02:00
|
|
|
}
|