1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Policies/EntityPolicy.php
2016-12-08 20:00:13 +02:00

80 lines
1.7 KiB
PHP

<?php
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
/**
* Class EntityPolicy
*/
class EntityPolicy
{
use HandlesAuthorization;
/**
* @param User $user
* @return bool
*/
public static function create(User $user, $item) {
if ( ! static::checkModuleEnabled($user, $item)) {
return false;
}
return $user->hasPermission('create_all');
}
/**
* @param User $user
* @param $item
*
* @return bool
*/
public static function edit(User $user, $item) {
if ( ! static::checkModuleEnabled($user, $item)) {
return false;
}
return $user->hasPermission('edit_all') || $user->owns($item);
}
/**
* @param User $user
* @param $item
*
* @return bool
*/
public static function view(User $user, $item) {
if ( ! static::checkModuleEnabled($user, $item)) {
return false;
}
return $user->hasPermission('view_all') || $user->owns($item);
}
/**
* @param User $user
* @param $ownerUserId
* @return bool
*/
public static function viewByOwner(User $user, $ownerUserId) {
return $user->hasPermission('view_all') || $user->id == $ownerUserId;
}
/**
* @param User $user
* @param $ownerUserId
* @return bool
*/
public static function editByOwner(User $user, $ownerUserId) {
return $user->hasPermission('edit_all') || $user->id == $ownerUserId;
}
private static function checkModuleEnabled(User $user, $item)
{
$entityType = is_string($item) ? $item : $item->getEntityType();
return $user->account->isModuleEnabled($entityType);
}
}