Skip to content

Commit

Permalink
Merge pull request #7 from Althinect:refactor/Model-detection-improve…
Browse files Browse the repository at this point in the history
…ments

Refactor README and configuration for clarity; update model discovery logic
  • Loading branch information
tharindarodrigo authored Dec 26, 2024
2 parents c5b62da + 1342170 commit a769126
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Laravel Enum Permissions

A Laravel package that generates and manages permissions using PHP enums, making permission handling more type-safe and maintainable.
A Laravel package to easily generate enums using Models

## Requirements

Expand Down
49 changes: 47 additions & 2 deletions config/enum-permission.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
<?php

return [
'models_path' => 'Domains',
//---------------------------------------------------------------------------
// The path where the Models can be discovered within the "app" directory
//---------------------------------------------------------------------------

'models_app_path' => 'Models',

//---------------------------------------------------------------------------
// Enums will be generated in a similar path to the models
//
// Example:
// App\Models\User -> App\Permissions\UserPermission
// App\Domain\Shared\Models\User -> App\Domain\Shared\Permissions\UserPermission
//---------------------------------------------------------------------------

'enum_path_should_follow_models_path' => true,

//---------------------------------------------------------------------------
// The path where the Enum classes will be generated
//---------------------------------------------------------------------------

'user_model' => 'App\Models\User',

//---------------------------------------------------------------------------
// The classes that the models should extend. This helps in model discovery
//---------------------------------------------------------------------------

'model_super_classes' => [
'Illuminate\Database\Eloquent\Model',
'Illuminate\Foundation\Auth\User',
],

//---------------------------------------------------------------------------
// This is a template for the Policy classes that will be
// generated. Each permission will be a method in the policy
//
// method: The method name in the policy
// arguments: The arguments that the method will take
// enum_case: The case of the enum value
// enum_value: The value of the enum
//---------------------------------------------------------------------------
// WARNING: Do not change the {{modelName}} and {{userModelName}} placeholders
// as they will be replaced by the actual model and user model names. You can
// however change the {{modelName}}.{{method}} placeholders to match your
// Refer: Althinect\EnumPermission\EnumPermissionCommand.php
//---------------------------------------------------------------------------

'permissions' => [
[
'method' => 'viewAny',
Expand Down Expand Up @@ -48,9 +90,12 @@
'enum_case' => 'FORCE_DELETE',
'enum_value' => '{{modelName}}.forceDelete',
],

],

//---------------------------------------------------------------------------
// The guards that the permissions will be created for
//---------------------------------------------------------------------------

'guards' => [
'web',
'api',
Expand Down
6 changes: 2 additions & 4 deletions src/Commands/EnumPermissionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace Althinect\EnumPermission\Commands;

use Althinect\EnumPermission\Concerns\Helpers;
use Illuminate\Auth\Authenticatable;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use ReflectionClass;

Expand Down Expand Up @@ -222,8 +220,8 @@ protected function promptForMissingArgumentsUsing(): array

private function getAllModels(): array
{
$models = array_filter($this->getClassesInDirectory(app_path(config('enum-permission.models_path'))), function ($model) {
return $model->isSubclassOf(Model::class) || $model->isSubclassOf(Authenticatable::class);
$models = array_filter($this->getClassesInDirectory(app_path(config('enum-permission.models_app_path'))), function ($model) {
return collect(config('enum-permission.model_super_classes'))->contains(fn ($superClass): mixed => $model->isSubclassOf($superClass));
});

return $models = array_map(function ($model) {
Expand Down

0 comments on commit a769126

Please sign in to comment.