46
common/Generators/Policy/GeneratePolicy.php
Executable file
46
common/Generators/Policy/GeneratePolicy.php
Executable file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Generators\Policy;
|
||||
|
||||
use Illuminate\Foundation\Console\PolicyMakeCommand;
|
||||
|
||||
class GeneratePolicy extends PolicyMakeCommand
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
if (parent::handle() !== false) {
|
||||
// auto-register policy
|
||||
$path = app_path('Providers/AuthServiceProvider.php');
|
||||
$model = str_replace('/', '\\', $this->option('model'));
|
||||
$policy = $this->getNameInput();
|
||||
|
||||
// add policy to providers
|
||||
$marker = "'App\\Model' => 'App\\Policies\\ModelPolicy',";
|
||||
file_put_contents($path, str_replace(
|
||||
$marker,
|
||||
"$marker\n {$model}::class => $policy::class,",
|
||||
file_get_contents($path)
|
||||
));
|
||||
|
||||
// import policy and model
|
||||
$marker = 'use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;';
|
||||
$namespaceModel = $this->laravel->getNamespace().$model;
|
||||
$namespacePolicy = $this->qualifyClass($policy);
|
||||
|
||||
file_put_contents($path, str_replace(
|
||||
$marker,
|
||||
"use {$namespaceModel};\nuse {$namespacePolicy};\n$marker",
|
||||
file_get_contents($path)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
protected function getStub()
|
||||
{
|
||||
if ($this->option('model')) {
|
||||
return __DIR__.'/stubs/policy.model.stub';
|
||||
}
|
||||
|
||||
return parent::getStub();
|
||||
}
|
||||
}
|
||||
43
common/Generators/Policy/stubs/policy.model.stub
Executable file
43
common/Generators/Policy/stubs/policy.model.stub
Executable file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace DummyNamespace;
|
||||
|
||||
use Common\Auth\BaseUser;
|
||||
use NamespacedDummyModel;
|
||||
use Common\Core\Policies\BasePolicy;
|
||||
|
||||
class DummyClass extends BasePolicy
|
||||
{
|
||||
public function index(BaseUser $user, $userId = null)
|
||||
{
|
||||
return $user->hasPermission('dummyModel.view') || $user->id === (int) $userId;
|
||||
}
|
||||
|
||||
public function show(BaseUser $user, DummyModel $dummyModel)
|
||||
{
|
||||
return $user->hasPermission('dummyModel.view') || $dummyModel->user_id === $user->id;
|
||||
}
|
||||
|
||||
public function store(BaseUser $user)
|
||||
{
|
||||
return $user->hasPermission('dummyModel.create');
|
||||
}
|
||||
|
||||
public function update(BaseUser $user, DummyModel $dummyModel)
|
||||
{
|
||||
return $user->hasPermission('dummyModel.update') || $dummyModel->user_id === $user->id;
|
||||
}
|
||||
|
||||
public function destroy(BaseUser $user, $dummyModelIds)
|
||||
{
|
||||
if ($user->hasPermission('dummyModel.delete')) {
|
||||
return true;
|
||||
} else {
|
||||
$dbCount = app(DummyModel::class)
|
||||
->whereIn('id', $dummyModelIds)
|
||||
->where('user_id', $user->id)
|
||||
->count();
|
||||
return $dbCount === count($dummyModelIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user