2016-04-23 17:52:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Policies;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
use App\Models\User;
|
2016-04-23 17:52:36 +02:00
|
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
2018-06-07 12:08:34 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2016-04-23 17:52:36 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class EntityPolicy.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-04-26 03:53:39 +02:00
|
|
|
class EntityPolicy
|
2016-04-23 17:52:36 +02:00
|
|
|
{
|
|
|
|
use HandlesAuthorization;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
2017-01-30 20:49:42 +01:00
|
|
|
* @param User $user
|
2018-06-07 12:08:34 +02:00
|
|
|
* @param $item - entity name or object
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-06-07 12:08:34 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
public static function create(User $user, $item)
|
|
|
|
{
|
2018-06-07 12:08:34 +02:00
|
|
|
if (! static::checkModuleEnabled($user, $item))
|
2016-10-27 10:57:51 +02:00
|
|
|
return false;
|
|
|
|
|
2018-06-07 12:08:34 +02:00
|
|
|
|
|
|
|
$entityType = is_string($item) ? $item : $item->getEntityType();
|
|
|
|
return $user->hasPermission('create_' . $entityType);
|
2016-04-23 17:52:36 +02:00
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
2018-06-07 12:08:34 +02:00
|
|
|
* @param $item - entity name or object
|
2016-07-03 18:11:58 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-06-07 12:08:34 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
public static function edit(User $user, $item)
|
|
|
|
{
|
2018-06-07 12:08:34 +02:00
|
|
|
if (! static::checkModuleEnabled($user, $item))
|
2016-10-27 10:57:51 +02:00
|
|
|
return false;
|
|
|
|
|
2018-06-07 12:08:34 +02:00
|
|
|
|
|
|
|
$entityType = is_string($item) ? $item : $item->getEntityType();
|
|
|
|
return $user->hasPermission('edit_' . $entityType) || $user->owns($item);
|
2016-04-23 17:52:36 +02:00
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
2018-06-07 12:08:34 +02:00
|
|
|
* @param $item - entity name or object
|
2016-07-03 18:11:58 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-06-07 12:08:34 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
public static function view(User $user, $item)
|
|
|
|
{
|
2018-06-07 12:08:34 +02:00
|
|
|
if (! static::checkModuleEnabled($user, $item))
|
2016-10-27 10:57:51 +02:00
|
|
|
return false;
|
|
|
|
|
2018-06-07 12:08:34 +02:00
|
|
|
$entityType = is_string($item) ? $item : $item->getEntityType();
|
|
|
|
return $user->hasPermission('view_' . $entityType) || $user->owns($item);
|
2016-04-26 03:53:39 +02:00
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param $ownerUserId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2018-06-07 12:08:34 +02:00
|
|
|
* Legacy permissions - retaining these for legacy code however new code
|
|
|
|
* should use auth()->user()->can('view', $ENTITY_TYPE)
|
|
|
|
*
|
|
|
|
* $ENTITY_TYPE can be either the constant ie ENTITY_INVOICE, or the entity $object
|
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-06-07 12:08:34 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
public static function viewByOwner(User $user, $ownerUserId)
|
|
|
|
{
|
2018-06-07 12:08:34 +02:00
|
|
|
return $user->id == $ownerUserId;
|
2016-04-26 03:53:39 +02:00
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param $ownerUserId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2018-06-07 12:08:34 +02:00
|
|
|
* Legacy permissions - retaining these for legacy code however new code
|
|
|
|
* should use auth()->user()->can('edit', $ENTITY_TYPE)
|
|
|
|
*
|
|
|
|
* $ENTITY_TYPE can be either the constant ie ENTITY_INVOICE, or the entity $object
|
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-06-07 12:08:34 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
public static function editByOwner(User $user, $ownerUserId)
|
|
|
|
{
|
2018-06-07 12:08:34 +02:00
|
|
|
return $user->id == $ownerUserId;
|
2016-04-23 17:52:36 +02:00
|
|
|
}
|
2016-10-27 10:57:51 +02:00
|
|
|
|
2018-06-07 12:08:34 +02:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param $item - entity name or object
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
|
2016-10-27 10:57:51 +02:00
|
|
|
private static function checkModuleEnabled(User $user, $item)
|
|
|
|
{
|
|
|
|
$entityType = is_string($item) ? $item : $item->getEntityType();
|
2018-06-07 12:08:34 +02:00
|
|
|
return $user->account->isModuleEnabled($entityType);
|
2016-10-27 10:57:51 +02:00
|
|
|
}
|
2016-07-07 10:44:15 +02:00
|
|
|
}
|