From 46c56cc499f38a8cef54cba06aa84d7c09e44fc4 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Fri, 20 Oct 2017 09:57:22 +0800 Subject: [PATCH] Add model policy generator class --- src/CrudMake.php | 2 + src/Generators/ModelPolicyGenerator.php | 54 +++++++++ src/stubs/model-policy.stub | 64 ++++++++++ tests/CrudMakeCommandTest.php | 4 + tests/Generators/ModelPolicyGeneratorTest.php | 162 ++++++++++++++++++++++++++ tests/TestCase.php | 1 + 6 files changed, 287 insertions(+) create mode 100644 src/Generators/ModelPolicyGenerator.php create mode 100644 src/stubs/model-policy.stub create mode 100644 tests/Generators/ModelPolicyGeneratorTest.php diff --git a/src/CrudMake.php b/src/CrudMake.php index 89d44b5..e294007 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -12,6 +12,7 @@ use Luthfi\CrudGenerator\Generators\LangFileGenerator; use Luthfi\CrudGenerator\Generators\MigrationGenerator; use Luthfi\CrudGenerator\Generators\ModelFactoryGenerator; use Luthfi\CrudGenerator\Generators\ModelGenerator; +use Luthfi\CrudGenerator\Generators\ModelPolicyGenerator; use Luthfi\CrudGenerator\Generators\ModelTestGenerator; use Luthfi\CrudGenerator\Generators\WebRouteGenerator; @@ -93,6 +94,7 @@ class CrudMake extends Command app(FormViewGenerator::class, ['command' => $this])->generate(); app(LangFileGenerator::class, ['command' => $this])->generate(); app(ModelFactoryGenerator::class, ['command' => $this])->generate(); + app(ModelPolicyGenerator::class, ['command' => $this])->generate(); app(FeatureTestGenerator::class, ['command' => $this])->generate(); app(ModelTestGenerator::class, ['command' => $this])->generate(); diff --git a/src/Generators/ModelPolicyGenerator.php b/src/Generators/ModelPolicyGenerator.php new file mode 100644 index 0000000..2a60307 --- /dev/null +++ b/src/Generators/ModelPolicyGenerator.php @@ -0,0 +1,54 @@ +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; + } +} diff --git a/src/stubs/model-policy.stub b/src/stubs/model-policy.stub new file mode 100644 index 0000000..70d3735 --- /dev/null +++ b/src/stubs/model-policy.stub @@ -0,0 +1,64 @@ +assertFileExists(resource_path("lang/en/{$this->lang_name}.php")); $this->assertFileExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileExists(base_path("routes/web.php")); + $this->assertFileExists(app_path("Policies/{$this->model_name}Policy.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); } @@ -47,6 +48,7 @@ class CrudMakeCommandTest extends TestCase $this->assertFileNotExists(resource_path("lang/en/{$this->lang_name}.php")); $this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileNotExists(base_path("routes/web.php")); + $this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php")); $this->assertFileNotExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); $this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); } @@ -75,6 +77,7 @@ class CrudMakeCommandTest extends TestCase $this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php")); $this->assertFileExists(resource_path("lang/en/{$langName}.php")); $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(app_path("Policies/{$modelName}Policy.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); } @@ -104,6 +107,7 @@ class CrudMakeCommandTest extends TestCase $this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php")); $this->assertFileExists(resource_path("lang/en/{$langName}.php")); $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); } diff --git a/tests/Generators/ModelPolicyGeneratorTest.php b/tests/Generators/ModelPolicyGeneratorTest.php new file mode 100644 index 0000000..e4030d9 --- /dev/null +++ b/tests/Generators/ModelPolicyGeneratorTest.php @@ -0,0 +1,162 @@ +artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $modelPolicyPath = app_path('Policies/'.$this->model_name.'Policy.php'); + $this->assertFileExists($modelPolicyPath); + $modelPolicyContent = "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 = "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)); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 4879ae8..ae0603d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -45,6 +45,7 @@ abstract class TestCase extends BaseTestCase $this->removeFileOrDir(resource_path("lang/en/app.php")); $this->removeFileOrDir(resource_path("lang/en/{$this->lang_name}.php")); $this->removeFileOrDir(base_path('routes')); + $this->removeFileOrDir(app_path('Policies')); $this->removeFileOrDir(base_path('tests/BrowserKitTest.php')); $this->removeFileOrDir(base_path('tests/Feature')); $this->removeFileOrDir(base_path('tests/Unit'));