6 changed files with 287 additions and 0 deletions
-
2src/CrudMake.php
-
54src/Generators/ModelPolicyGenerator.php
-
64src/stubs/model-policy.stub
-
4tests/CrudMakeCommandTest.php
-
162tests/Generators/ModelPolicyGeneratorTest.php
-
1tests/TestCase.php
@ -0,0 +1,54 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Luthfi\CrudGenerator\Generators; |
||||
|
|
||||
|
/** |
||||
|
* Model Policy Generator Class |
||||
|
*/ |
||||
|
class ModelPolicyGenerator extends BaseGenerator |
||||
|
{ |
||||
|
/** |
||||
|
* {@inheritDoc} |
||||
|
*/ |
||||
|
public function generate() |
||||
|
{ |
||||
|
$parentDirectory = ''; |
||||
|
if (! is_null($this->command->option('parent'))) { |
||||
|
$parentDirectory = '/'.$this->command->option('parent'); |
||||
|
} |
||||
|
$modelPolicyPath = $this->makeDirectory(app_path('Policies'.$parentDirectory)); |
||||
|
|
||||
|
$this->generateFile( |
||||
|
$modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php', |
||||
|
$this->getContent() |
||||
|
); |
||||
|
|
||||
|
$this->command->info($this->modelNames['model_name'].' model policy generated.'); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* {@inheritDoc} |
||||
|
*/ |
||||
|
protected function getContent() |
||||
|
{ |
||||
|
$stub = $this->files->get(__DIR__.'/../stubs/model-policy.stub'); |
||||
|
|
||||
|
$policyFileContent = $this->replaceStubString($stub); |
||||
|
|
||||
|
$userModel = config('auth.providers.users.model'); |
||||
|
|
||||
|
if ('App\User' !== $userModel) { |
||||
|
$policyFileContent = str_replace('App\User', $userModel, $policyFileContent); |
||||
|
} |
||||
|
|
||||
|
if (! is_null($parentName = $this->command->option('parent'))) { |
||||
|
$policyFileContent = str_replace( |
||||
|
'App\Policies;', |
||||
|
"App\Policies\\{$parentName};", |
||||
|
$policyFileContent |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
return $policyFileContent; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Policies; |
||||
|
|
||||
|
use App\User; |
||||
|
use fullMstr; |
||||
|
use Illuminate\Auth\Access\HandlesAuthorization; |
||||
|
|
||||
|
class MasterPolicy |
||||
|
{ |
||||
|
use HandlesAuthorization; |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can view the project. |
||||
|
* |
||||
|
* @param \App\User $user |
||||
|
* @param \fullMstr $singleMstr |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function view(User $user, Master $singleMstr) |
||||
|
{ |
||||
|
// Update $user authorization to view $singleMstr here. |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can create projects. |
||||
|
* |
||||
|
* @param \App\User $user |
||||
|
* @param \fullMstr $singleMstr |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function create(User $user, Master $singleMstr) |
||||
|
{ |
||||
|
// Update $user authorization to create $singleMstr here. |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can update the project. |
||||
|
* |
||||
|
* @param \App\User $user |
||||
|
* @param \fullMstr $singleMstr |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function update(User $user, Master $singleMstr) |
||||
|
{ |
||||
|
// Update $user authorization to update $singleMstr here. |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can delete the project. |
||||
|
* |
||||
|
* @param \App\User $user |
||||
|
* @param \fullMstr $singleMstr |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function delete(User $user, Master $singleMstr) |
||||
|
{ |
||||
|
// Update $user authorization to delete $singleMstr here. |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,162 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Tests\Generators; |
||||
|
|
||||
|
use Tests\TestCase; |
||||
|
|
||||
|
class ModelPolicyGeneratorTest extends TestCase |
||||
|
{ |
||||
|
/** @test */ |
||||
|
public function it_creates_correct_model_policy_content() |
||||
|
{ |
||||
|
$userModel = config('auth.providers.users.model'); |
||||
|
|
||||
|
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); |
||||
|
|
||||
|
$modelPolicyPath = app_path('Policies/'.$this->model_name.'Policy.php'); |
||||
|
$this->assertFileExists($modelPolicyPath); |
||||
|
$modelPolicyContent = "<?php
|
||||
|
|
||||
|
namespace App\Policies; |
||||
|
|
||||
|
use {$userModel}; |
||||
|
use {$this->full_model_name}; |
||||
|
use Illuminate\Auth\Access\HandlesAuthorization; |
||||
|
|
||||
|
class {$this->model_name}Policy |
||||
|
{ |
||||
|
use HandlesAuthorization; |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can view the project. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function view(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to view \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can create projects. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function create(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to create \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can update the project. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function update(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to update \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can delete the project. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function delete(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to delete \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
";
|
||||
|
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function it_creates_correct_model_policy_content_with_parent() |
||||
|
{ |
||||
|
$userModel = config('auth.providers.users.model'); |
||||
|
|
||||
|
$this->artisan('make:crud', ['name' => $this->model_name, '--parent' => 'Projects', '--no-interaction' => true]); |
||||
|
|
||||
|
$modelPolicyPath = app_path('Policies/Projects/'.$this->model_name.'Policy.php'); |
||||
|
$this->assertFileExists($modelPolicyPath); |
||||
|
$modelPolicyContent = "<?php
|
||||
|
|
||||
|
namespace App\Policies\Projects; |
||||
|
|
||||
|
use {$userModel}; |
||||
|
use {$this->full_model_name}; |
||||
|
use Illuminate\Auth\Access\HandlesAuthorization; |
||||
|
|
||||
|
class {$this->model_name}Policy |
||||
|
{ |
||||
|
use HandlesAuthorization; |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can view the project. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function view(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to view \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can create projects. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function create(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to create \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can update the project. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function update(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to update \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine whether the user can delete the project. |
||||
|
* |
||||
|
* @param \\{$userModel} \$user |
||||
|
* @param \\{$this->full_model_name} \${$this->single_model_var_name} |
||||
|
* @return mixed |
||||
|
*/ |
||||
|
public function delete(User \$user, {$this->model_name} \${$this->single_model_var_name}) |
||||
|
{ |
||||
|
// Update \$user authorization to delete \${$this->single_model_var_name} here.
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
";
|
||||
|
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue