Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor README and configuration for clarity; update model discovery logic #7

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading