From 8399f8b95f79075df5790ccf1b51510cda851ff4 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 8 Dec 2016 20:00:13 +0200 Subject: [PATCH] Working on module CRUD --- .../{MakeRepository.php => MakeClass.php} | 17 ++--- app/Console/Commands/MakeDatatable.php | 68 ------------------- app/Console/Commands/MakeModule.php | 14 ++-- .../Commands/stubs/migration/create.stub | 7 +- app/Console/Commands/stubs/policy.stub | 8 +++ app/Console/Commands/stubs/repository.stub | 5 +- app/Console/Kernel.php | 3 +- app/Policies/EntityPolicy.php | 1 + app/Policies/GenericEntityPolicy.php | 55 +++++++++------ config/modules.php | 3 +- 10 files changed, 72 insertions(+), 109 deletions(-) rename app/Console/Commands/{MakeRepository.php => MakeClass.php} (73%) delete mode 100644 app/Console/Commands/MakeDatatable.php create mode 100644 app/Console/Commands/stubs/policy.stub diff --git a/app/Console/Commands/MakeRepository.php b/app/Console/Commands/MakeClass.php similarity index 73% rename from app/Console/Commands/MakeRepository.php rename to app/Console/Commands/MakeClass.php index d701a5cc00b6..f895728acb50 100644 --- a/app/Console/Commands/MakeRepository.php +++ b/app/Console/Commands/MakeClass.php @@ -11,7 +11,7 @@ use Nwidart\Modules\Commands\GeneratorCommand; use Nwidart\Modules\Support\Stub; use Nwidart\Modules\Traits\ModuleCommandTrait; -class MakeRepository extends GeneratorCommand +class MakeClass extends GeneratorCommand { use ModuleCommandTrait; @@ -22,20 +22,21 @@ class MakeRepository extends GeneratorCommand * * @var string */ - protected $name = 'ninja:make-repository'; + protected $name = 'ninja:make-class'; /** * The console command description. * * @var string */ - protected $description = 'Create repository stub'; + protected $description = 'Create class stub'; protected function getArguments() { return [ ['name', InputArgument::REQUIRED, 'The name of the datatable.'], - ['module', InputArgument::OPTIONAL, 'The name of module will be used.'], + ['module', InputArgument::REQUIRED, 'The name of module will be used.'], + ['class', InputArgument::REQUIRED, 'The name of the class.'], ]; } @@ -43,8 +44,8 @@ class MakeRepository extends GeneratorCommand { $module = $this->laravel['modules']->findOrFail($this->getModuleName()); - return (new Stub('/repository.stub', [ - 'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.repository'), + return (new Stub('/' . $this->argument('class') . '.stub', [ + 'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.' . $this->argument('class')), 'LOWER_NAME' => $module->getLowerName(), 'CLASS' => $this->getClass(), 'STUDLY_NAME' => Str::studly($module->getLowerName()), @@ -54,7 +55,7 @@ class MakeRepository extends GeneratorCommand public function getDestinationFilePath() { $path = $this->laravel['modules']->getModulePath($this->getModuleName()); - $seederPath = $this->laravel['modules']->config('paths.generator.repository'); + $seederPath = $this->laravel['modules']->config('paths.generator.' . $this->argument('class')); return $path . $seederPath . '/' . $this->getFileName() . '.php'; } @@ -64,7 +65,7 @@ class MakeRepository extends GeneratorCommand */ protected function getFileName() { - return studly_case($this->argument('name')) . 'Repository'; + return studly_case($this->argument('name')) . Str::studly($this->argument('class')); } } diff --git a/app/Console/Commands/MakeDatatable.php b/app/Console/Commands/MakeDatatable.php deleted file mode 100644 index 6687f383e8ac..000000000000 --- a/app/Console/Commands/MakeDatatable.php +++ /dev/null @@ -1,68 +0,0 @@ -laravel['modules']->findOrFail($this->getModuleName()); - - return (new Stub('/datatable.stub', [ - 'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.datatables'), - 'LOWER_NAME' => $module->getLowerName(), - 'CLASS' => $this->getClass(), - ]))->render(); - } - - public function getDestinationFilePath() - { - $path = $this->laravel['modules']->getModulePath($this->getModuleName()); - $seederPath = $this->laravel['modules']->config('paths.generator.datatables'); - - return $path . $seederPath . '/' . $this->getFileName() . '.php'; - } - - /** - * @return string - */ - protected function getFileName() - { - return studly_case($this->argument('name')) . 'Datatable'; - } - -} diff --git a/app/Console/Commands/MakeModule.php b/app/Console/Commands/MakeModule.php index 6bd91d6b492c..30ed6cb1e376 100644 --- a/app/Console/Commands/MakeModule.php +++ b/app/Console/Commands/MakeModule.php @@ -40,16 +40,22 @@ class MakeModule extends Command { $name = $this->argument('name'); $fields = $this->argument('fields'); + $lower = strtolower($name); $this->info("Creating module: {$name}"); - $this->info("Fields: {$fields}"); + //$this->info("Fields: {$fields}"); Artisan::call('module:make', ['name' => [$name]]); - Artisan::call('module:make-migration', ['name' => "create_{$name}_table", '--fields' => $fields, 'module' => $name]); + Artisan::call('module:make-migration', ['name' => "create_{$lower}_table", '--fields' => $fields, 'module' => $name]); + Artisan::call('module:migrate', ['module' => $name]); Artisan::call('module:make-model', ['model' => $name, 'module' => $name]); - Artisan::call('ninja:make-datatable', ['name' => $name, 'module' => $name]); - Artisan::call('ninja:make-repository', ['name' => $name, 'module' => $name]); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'datatable']); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'repository']); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'policy']); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'auth-provider']); + + Artisan::call('module:dump'); } protected function getArguments() diff --git a/app/Console/Commands/stubs/migration/create.stub b/app/Console/Commands/stubs/migration/create.stub index c86cb612efde..240cc52761ca 100755 --- a/app/Console/Commands/stubs/migration/create.stub +++ b/app/Console/Commands/stubs/migration/create.stub @@ -12,14 +12,15 @@ class $CLASS$ extends Migration */ public function up() { - Schema::create('$TABLE$', function (Blueprint $table) { + Schema::create(strtolower('$TABLE$'), function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('user_id')->index(); $table->unsignedInteger('account_id')->index(); $table->unsignedInteger('client_id')->index()->nullable(); - + $FIELDS$ $table->timestamps(); + $table->softDeletes(); $table->boolean('is_deleted')->default(false); $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); @@ -38,6 +39,6 @@ $FIELDS$ */ public function down() { - Schema::dropIfExists('$TABLE$'); + Schema::dropIfExists(strtolower('$TABLE$')); } } diff --git a/app/Console/Commands/stubs/policy.stub b/app/Console/Commands/stubs/policy.stub new file mode 100644 index 000000000000..f95ad1e87a9c --- /dev/null +++ b/app/Console/Commands/stubs/policy.stub @@ -0,0 +1,8 @@ +where('clients.user_id', '=', $userId); } */ - + return $query; } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index fd7e44246839..deab7b163c53 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -24,8 +24,7 @@ class Kernel extends ConsoleKernel 'App\Console\Commands\GenerateResources', 'App\Console\Commands\TestOFX', 'App\Console\Commands\MakeModule', - 'App\Console\Commands\MakeDatatable', - 'App\Console\Commands\MakeRepository', + 'App\Console\Commands\MakeClass', ]; /** diff --git a/app/Policies/EntityPolicy.php b/app/Policies/EntityPolicy.php index 9a5216ec87c8..e2fa9af8e6de 100644 --- a/app/Policies/EntityPolicy.php +++ b/app/Policies/EntityPolicy.php @@ -73,6 +73,7 @@ class EntityPolicy private static function checkModuleEnabled(User $user, $item) { $entityType = is_string($item) ? $item : $item->getEntityType(); + return $user->account->isModuleEnabled($entityType); } } diff --git a/app/Policies/GenericEntityPolicy.php b/app/Policies/GenericEntityPolicy.php index fc18c7c550c9..e2afc039e45c 100644 --- a/app/Policies/GenericEntityPolicy.php +++ b/app/Policies/GenericEntityPolicy.php @@ -3,8 +3,9 @@ namespace App\Policies; -use App\Models\User; use Utils; +use App\Models\User; +use Illuminate\Support\Str; use Illuminate\Auth\Access\HandlesAuthorization; /** @@ -16,14 +17,14 @@ class GenericEntityPolicy /** * @param User $user - * @param $itemType + * @param $entityType * @param $ownerUserId * @return bool|mixed */ - public static function editByOwner(User $user, $itemType, $ownerUserId) { - $itemType = Utils::getEntityName($itemType); - if (method_exists("App\\Policies\\{$itemType}Policy", 'editByOwner')) { - return call_user_func(["App\\Policies\\{$itemType}Policy", 'editByOwner'], $user, $ownerUserId); + 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; @@ -31,14 +32,14 @@ class GenericEntityPolicy /** * @param User $user - * @param $itemType + * @param $entityTypee * @param $ownerUserId * @return bool|mixed */ - public static function viewByOwner(User $user, $itemType, $ownerUserId) { - $itemType = Utils::getEntityName($itemType); - if (method_exists("App\\Policies\\{$itemType}Policy", 'viewByOwner')) { - return call_user_func(["App\\Policies\\{$itemType}Policy", 'viewByOwner'], $user, $ownerUserId); + 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; @@ -46,13 +47,13 @@ class GenericEntityPolicy /** * @param User $user - * @param $itemType + * @param $entityType * @return bool|mixed */ - public static function create(User $user, $itemType) { - $entityName = Utils::getEntityName($itemType); - if (method_exists("App\\Policies\\{$entityName}Policy", 'create')) { - return call_user_func(["App\\Policies\\{$entityName}Policy", 'create'], $user, $itemType); + 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; @@ -60,16 +61,28 @@ class GenericEntityPolicy /** * @param User $user - * @param $itemType + * @param $entityType * @return bool|mixed */ - public static function view(User $user, $itemType) { - $entityName = Utils::getEntityName($itemType); - if (method_exists("App\\Policies\\{$entityName}Policy", 'view')) { - return call_user_func(["App\\Policies\\{$entityName}Policy", 'view'], $user, $itemType); + 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"; + } + } diff --git a/config/modules.php b/config/modules.php index c442a7d45cf9..cc550da57df1 100644 --- a/config/modules.php +++ b/config/modules.php @@ -113,7 +113,8 @@ return [ 'jobs' => 'Jobs', 'emails' => 'Emails', 'notifications' => 'Notifications', - 'datatables' => 'Datatables', + 'datatable' => 'Datatables', + 'policy' => 'Policies', ], ], /*