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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-11-12 12:36:24 +01:00
|
|
|
|| ($user->hasPermission('edit_all') && $entity->company_id == $user->companyId())
|
2019-11-05 11:16:38 +01:00
|
|
|
|| $user->owns($entity)
|
|
|
|
|| $user->assigned($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-11-12 12:36:24 +01:00
|
|
|
|| ($user->hasPermission('view_all') && $entity->company_id == $user->companyId())
|
2019-11-05 11:16:38 +01:00
|
|
|
|| $user->owns($entity)
|
|
|
|
|| $user->assigned($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
|
|
|
}
|