1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Policies/EntityPolicy.php

89 lines
1.8 KiB
PHP
Raw Normal View History

2016-04-23 17:52:36 +02:00
<?php
namespace App\Policies;
use App\Models\User;
2016-04-23 17:52:36 +02:00
use Illuminate\Auth\Access\HandlesAuthorization;
/**
2017-01-30 20:40:43 +01:00
* Class EntityPolicy.
*/
2016-04-26 03:53:39 +02:00
class EntityPolicy
2016-04-23 17:52:36 +02:00
{
use HandlesAuthorization;
/**
2017-01-30 20:49:42 +01:00
* @param User $user
* @param mixed $item
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2017-01-30 17:05:31 +01:00
public static function create(User $user, $item)
{
if (! static::checkModuleEnabled($user, $item)) {
2016-10-27 10:57:51 +02:00
return false;
}
2016-04-26 03:53:39 +02:00
return $user->hasPermission('create_all');
2016-04-23 17:52:36 +02:00
}
/**
* @param User $user
* @param $item
*
* @return bool
*/
2017-01-30 17:05:31 +01:00
public static function edit(User $user, $item)
{
if (! static::checkModuleEnabled($user, $item)) {
2016-10-27 10:57:51 +02:00
return false;
}
2016-04-26 03:53:39 +02:00
return $user->hasPermission('edit_all') || $user->owns($item);
2016-04-23 17:52:36 +02:00
}
/**
* @param User $user
* @param $item
*
* @return bool
*/
2017-01-30 17:05:31 +01:00
public static function view(User $user, $item)
{
if (! static::checkModuleEnabled($user, $item)) {
2016-10-27 10:57:51 +02:00
return false;
}
2016-04-26 03:53:39 +02:00
return $user->hasPermission('view_all') || $user->owns($item);
}
/**
* @param User $user
* @param $ownerUserId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2017-01-30 17:05:31 +01:00
public static function viewByOwner(User $user, $ownerUserId)
{
2016-04-26 03:53:39 +02:00
return $user->hasPermission('view_all') || $user->id == $ownerUserId;
}
/**
* @param User $user
* @param $ownerUserId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2017-01-30 17:05:31 +01:00
public static function editByOwner(User $user, $ownerUserId)
{
2016-04-26 03:53:39 +02:00
return $user->hasPermission('edit_all') || $user->id == $ownerUserId;
2016-04-23 17:52:36 +02:00
}
2016-10-27 10:57:51 +02:00
private static function checkModuleEnabled(User $user, $item)
{
$entityType = is_string($item) ? $item : $item->getEntityType();
2016-12-08 19:00:13 +01:00
2016-10-27 10:57:51 +02:00
return $user->account->isModuleEnabled($entityType);
}
}