Skip to content

feat: Add Trait-Based Policy Generation #538

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

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

zayedadel
Copy link

Introduces an optional trait based policy generation approach. This allows users to generate policies that utilize a common GeneralPoliciesTrait for defining standard CRUD permissions, reducing boilerplate in individual policy files. The feature is controlled by a new configuration option generator.generate_trait_policies in config/filament-shield.php. When enabled, the shield:generate command will use a new TraitPolicy.stub to create policies that leverage this trait.

Includes:

  • GeneralPoliciesTrait.php with common policy methods.
  • TraitPolicy.stub for generating trait-based policies.
  • Updates to GenerateCommand.php to handle the new stub and variables.
  • New generate_trait_policies config option.
  • Utils::isTraitGenerationEnabled() helper method.
  • Documentation in README.md for the new feature.

…based policy generation approach. This allows users to generate policies that utilize a common `GeneralPoliciesTrait` for defining standard CRUD permissions, reducing boilerplate in individual policy files. The feature is controlled by a new configuration option `generator.generate_trait_policies` in `config/filament-shield.php`. When enabled, the `shield:generate` command will use a new `TraitPolicy.stub` to create policies that leverage this trait.

Includes:
- `GeneralPoliciesTrait.php` with common policy methods.
- `TraitPolicy.stub` for generating trait-based policies.
- Updates to `GenerateCommand.php` to handle the new stub and variables.
- New `generate_trait_policies` config option.
- `Utils::isTraitGenerationEnabled()` helper method.
- Documentation in README.md for the new feature.
Copy link

what-the-diff bot commented May 10, 2025

PR Summary

Added Trait-Based Policy Generation

In this pull request, a feature is added for easier management of permissions in the application. It's described in a new section added to the README guide. It uses the idea of "traits" - a technical term for a set of predetermined attributes that can be easily used over and over again.

New Configuration Option

To enable this new feature, a switch is added in the configuration file called filament-shield.php. The option is called generate_trait_policies. When it's turned on, the system can use this new way of managing permissions.

Updated Policy Generation Logic

The underlying script used to generate policies, GenerateCommand.php, has been updated. Now, when the special switch mentioned above is activated, it uses "traits" for providing permissions.

Added GeneralPoliciesTrait

A new list of common permissions has been created in the form of a file called GeneralPoliciesTrait.php. It uses a suffix for permissions which makes them easier to use within the application.

New Policy Stub

A blueprint that defines how the permissions are created when using "traits" is created. It's like a skeleton that the application uses to breed specific permissions.

@bezhanSalleh
Copy link
Owner

Hey Thanks for the PR. since we don't have tests, and your pr have some issues. could you fix and push an update so i can test it out. to reproduce clone the Filament Demo and follow shields installation and your PR's instructions.

@bezhanSalleh
Copy link
Owner

Do pull the latest v3 cause i think you would need to handle the feature introduced in #547 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants