invoiceninja/app/Policies/EntityPolicy.php

130 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2016-04-23 11:52:36 -04:00
<?php
namespace App\Policies;
use App\Models\User;
2016-04-23 11:52:36 -04:00
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Support\Facades\Log;
2016-04-23 11:52:36 -04:00
/**
2017-01-30 21:40:43 +02:00
* Class EntityPolicy.
*/
2016-04-25 21:53:39 -04:00
class EntityPolicy
2016-04-23 11:52:36 -04:00
{
use HandlesAuthorization;
/**
2017-01-30 21:49:42 +02:00
* @param User $user
* @param $item - entity name or object
2017-01-30 21:40:43 +02:00
*
* @return bool
*/
2019-01-30 22:00:26 +11:00
public function createPermission(User $user, $entityType)
2017-01-30 18:05:31 +02:00
{
2016-10-27 11:57:51 +03:00
2019-01-30 22:00:26 +11:00
if (! $this->checkModuleEnabled($user, $entityType))
return false;
return $user->hasPermission('create_' . $entityType);
2016-04-23 11:52:36 -04:00
}
/**
* @param User $user
* @param $item - entity name or object
*
* @return bool
*/
2019-01-30 22:00:26 +11:00
public function edit(User $user, $item)
2017-01-30 18:05:31 +02:00
{
2019-01-30 22:00:26 +11:00
if (! $this->checkModuleEnabled($user, $item))
2016-10-27 11:57:51 +03:00
return false;
$entityType = is_string($item) ? $item : $item->getEntityType();
return $user->hasPermission('edit_' . $entityType) || $user->owns($item);
2016-04-23 11:52:36 -04:00
}
/**
* @param User $user
* @param $item - entity name or object
*
* @return bool
*/
2019-01-30 22:00:26 +11:00
public function view(User $user, $item, $entityType = null)
2017-01-30 18:05:31 +02:00
{
2019-01-30 22:00:26 +11:00
if (! $this->checkModuleEnabled($user, $item))
2016-10-27 11:57:51 +03:00
return false;
$entityType = is_string($item) ? $item : $item->getEntityType();
return $user->hasPermission('view_' . $entityType) || $user->owns($item);
2016-04-25 21:53:39 -04:00
}
2019-01-30 22:00:26 +11:00
public function viewModel(User $user, $model)
{
if($user->hasPermission('view_'.$model->entityType))
return true;
elseif($model->user_id == $user->id)
return true;
elseif(isset($model->agent_id) && ($model->agent_id == $user->id))
return true;
else
return false;
}
/**
* @param User $user
* @param $ownerUserId
2017-01-30 21:40:43 +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
*
* @return bool
*/
2019-01-30 22:00:26 +11:00
public function viewByOwner(User $user, $ownerUserId)
2017-01-30 18:05:31 +02:00
{
return $user->id == $ownerUserId;
2016-04-25 21:53:39 -04:00
}
/**
* @param User $user
* @param $ownerUserId
2017-01-30 21:40:43 +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
*
* @return bool
*/
2019-01-30 22:00:26 +11:00
public function editByOwner(User $user, $ownerUserId)
2017-01-30 18:05:31 +02:00
{
return $user->id == $ownerUserId;
2016-04-23 11:52:36 -04:00
}
2016-10-27 11:57:51 +03:00
/**
* @param User $user
* @param $item - entity name or object
* @return bool
*/
2019-01-30 22:00:26 +11:00
public function checkModuleEnabled(User $user, $item)
2016-10-27 11:57:51 +03:00
{
$entityType = is_string($item) ? $item : $item->getEntityType();
return $user->account->isModuleEnabled($entityType);
2016-10-27 11:57:51 +03:00
}
2019-01-30 22:00:26 +11:00
public function createEntity(User $user, $entityType)
{
return $user->hasPermission('create_' . $entityType);
}
}