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

89 lines
2.2 KiB
PHP

<?php
namespace App\Policies;
use Utils;
use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Auth\Access\HandlesAuthorization;
/**
* Class GenericEntityPolicy
*/
class GenericEntityPolicy
{
use HandlesAuthorization;
/**
* @param User $user
* @param $entityType
* @param $ownerUserId
* @return bool|mixed
*/
public static function editByOwner(User $user, $entityType, $ownerUserId) {
$className = static::className($entityType);
if (method_exists($className, 'editByOwner')) {
return call_user_func([$className, 'editByOwner'], $user, $ownerUserId);
}
return false;
}
/**
* @param User $user
* @param $entityTypee
* @param $ownerUserId
* @return bool|mixed
*/
public static function viewByOwner(User $user, $entityType, $ownerUserId) {
$className = static::className($entityType);
if (method_exists($className, 'viewByOwner')) {
return call_user_func([$className, 'viewByOwner'], $user, $ownerUserId);
}
return false;
}
/**
* @param User $user
* @param $entityType
* @return bool|mixed
*/
public static function create(User $user, $entityType) {
$className = static::className($entityType);
if (method_exists($className, 'create')) {
return call_user_func([$className, 'create'], $user, $entityType);
}
return false;
}
/**
* @param User $user
* @param $entityType
* @return bool|mixed
*/
public static function view(User $user, $entityType) {
$className = static::className($entityType);
if (method_exists($className, 'view')) {
return call_user_func([$className, 'view'], $user, $entityType);
}
return false;
}
private static function className($entityType)
{
if ( ! Utils::isNinjaProd()) {
if ($module = \Module::find($entityType)) {
return "Modules\\{$module->getName()}\\Policies\\{$module->getName()}Policy";
}
}
$studly = Str::studly($entityType);
return "App\\Policies\\{$studly}Policy";
}
}