2019-01-16 10:28:06 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-01-16 10:28:06 +01:00
|
|
|
|
|
|
|
namespace App\Policies;
|
|
|
|
|
|
|
|
use App\Models\User;
|
2019-04-03 03:17:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-01-16 10:28:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class EntityPolicy
|
|
|
|
* @package App\Policies
|
|
|
|
*/
|
|
|
|
class EntityPolicy
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Fires before any of the custom policy methods
|
2019-01-19 11:35:21 +01:00
|
|
|
*
|
|
|
|
* Only fires if true, if false we continue.....
|
2019-01-20 06:00:50 +01:00
|
|
|
*
|
|
|
|
* Do not use this function!!!! We MUST also check company_id,
|
2019-01-16 10:28:06 +01:00
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param $ability
|
2019-01-19 11:35:21 +01:00
|
|
|
* @return bool/void
|
2019-01-16 10:28:06 +01:00
|
|
|
*/
|
2019-01-19 11:35:21 +01:00
|
|
|
public function before($user, $ability)
|
2019-01-16 10:28:06 +01:00
|
|
|
{
|
2019-01-20 06:00:50 +01:00
|
|
|
//if($user->isAdmin())
|
|
|
|
// return true;
|
2019-01-16 10:28:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-20 06:00:50 +01: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!!!!!!
|
|
|
|
*
|
2019-01-16 10:28:06 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param $entity
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function edit(User $user, $entity) : bool
|
|
|
|
{
|
2019-04-03 03:17:21 +02:00
|
|
|
|
2019-01-25 11:47:23 +01:00
|
|
|
return ($user->isAdmin() && $entity->company_id == $user->companyId())
|
|
|
|
|| ($user->hasPermission('edit_' . strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|
2019-01-20 06:00:50 +01:00
|
|
|
|| $user->owns($entity);
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-01-16 10:28:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the user has view permissions
|
2019-01-20 06:00:50 +01:00
|
|
|
*
|
|
|
|
* We MUST also check that the user can both view a entity and also check the entity belongs to the users company!!!!!!
|
2019-01-16 10:28:06 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param $entity
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function view(User $user, $entity) : bool
|
|
|
|
{
|
2019-06-12 01:15:17 +02:00
|
|
|
|
2019-01-25 11:47:23 +01:00
|
|
|
return ($user->isAdmin() && $entity->company_id == $user->companyId())
|
|
|
|
|| ($user->hasPermission('view_' . strtolower(class_basename($entity))) && $entity->company_id == $user->companyId())
|
2019-01-20 06:00:50 +01:00
|
|
|
|| $user->owns($entity);
|
2019-01-16 10:28:06 +01:00
|
|
|
}
|
2019-01-20 06:00:50 +01:00
|
|
|
|
|
|
|
|
2019-01-16 10:28:06 +01:00
|
|
|
}
|