Download the PHP package bezhansalleh/filament-shield without Composer

On this page you can find all versions of the php package bezhansalleh/filament-shield. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package filament-shield

filament-shield-art

FILAMENT 4.x FILAMENT 5.x Packagist Tests Passing Code Style Passing Downloads

Shield

The easiest and most intuitive way to add access management to your Filament panels.

[!IMPORTANT] This iteration is a complete rewrite from versions 3.x and 4.x-beta and is not backward compatible. Please refer to the Upgrade section on how to proceed.

Features

## Compatibility | Package Version | Filament Version | |-----------------|------------------| | [2.x](https://github.com/bezhanSalleh/filament-shield/tree/2.x) | 2.x | | [3.x](https://github.com/bezhanSalleh/filament-shield/tree/3.x) | 3.x | | **4.x** | 4.x & 5.x |
Table of Contents - [Installation](#installation) - [1. Install Package](#1-install-package) - [2. Configure Auth Provider](#2-configure-auth-provider) - [3. Setup Shield](#3-setup-shield) - [Usage \& Configuration](#usage--configuration) - [Permissions](#permissions) - [Configuration](#configuration) - [Case](#case) - [Customize permission key composition](#customize-permission-key-composition) - [Policies](#policies) - [Configuration](#configuration-1) - [Methods](#methods) - [Merge](#merge) - [Single Parameter Methods](#single-parameter-methods) - [Policy Enforcement](#policy-enforcement) - [Resources](#resources) - [Configuration](#configuration-2) - [Subject](#subject) - [Manage](#manage) - [Exclude](#exclude) - [Pages \& Widgets](#pages--widgets) - [Configuration](#configuration-3) - [Options](#options) - [Permission Enforcement](#permission-enforcement) - [Custom Permissions](#custom-permissions) - [Users (Assigning Roles to Users)](#users-assigning-roles-to-users) - [Shield Plugin \& Resource](#shield-plugin--resource) - [Navigation](#navigation) - [Labels](#labels) - [Global Search](#global-search) - [Parent Resource](#parent-resource) - [Tenancy](#tenancy) - [Layout Customization](#layout-customization) - [Commands](#commands) - [Prohibited Commands](#prohibited-commands) - [Core Commands](#core-commands) - [Generate Command Options (recap)](#generate-command-options-recap) - [Seeder Command Options (recap)](#seeder-command-options-recap) - [Localization](#localization) - [Configuration](#configuration-4) - [How It Works](#how-it-works) - [Generating Translation Files](#generating-translation-files) - [Translation Keys](#translation-keys) - [Default Package Translations](#default-package-translations) - [Upgrade](#upgrade) - [Translations](#translations) - [Testing](#testing) - [Changelog](#changelog) - [Contributing](#contributing) - [Security Vulnerabilities](#security-vulnerabilities) - [Credits](#credits) - [License](#license)

Installation

1. Install Package

2. Configure Auth Provider

  1. Publish the config and set your auth provider model.

  2. Add the HasRoles trait to your auth provider model:

3. Setup Shield

Run the setup command (it is interactive and smart):

Usage & Configuration

The package comes with a sensible default configuration that should work for most applications. You can customize the configuration by modifying it to fit your needs. The following sections explain the various configuration options available.

Permissions

You can customize how permission keys are generated to match your preferred naming conventions and organizational standards. Shield uses these settings from the filament-shield.php config file when creating permission names from your {Resources|Pages|Widgets}.

Configuration

Case

Shield formats permission keys using the specified case style. The available options are:

Customize permission key composition

You can customize how permission keys are generated by providing your own callback to buildPermissionKeyUsing in your AppServiceProvider's boot() method. The callback receives the following parameters:

This approach ensures that each resource has a unique set of permission keys, preventing any conflicts and allowing for more granular access control. You can of course extract the logic to a separate class or function if it gets too complex, but this should give you a good starting point.

Policies

Shield automatically generates policies for your Resources' Models.

Configuration

Methods

Each policy includes methods defined in the policies.methods config. You can customize this list to fit your application's needs. Since Filament Resources typically use a standard set of methods, the default configuration should suffice for most applications. If you have specific resources that require additional methods, you can easily add them to the list. However, it would be best to only include methods that are commonly used across your resources and define any resource-specific methods in the resources.manage config section. This approach keeps your policies clean and relevant to your application's requirements.

Merge

When policies.merge is set to true, Shield will combine the global methods defined in policies.methods with any resource-specific methods you define in resources.manage. This ensures that each resource's policy includes both the standard methods and any additional ones you need for that particular resource.

Single Parameter Methods

Some policy methods only require the user instance as a parameter (e.g., viewAny, create). These are defined in policies.single_parameter_methods. Shield will generate these methods accordingly in the policies. When you add new methods or resource-specific methods, ensure to update this list if they also only require the user instance. This helps maintain consistency and clarity in your policy definitions.

Policy Enforcement

Laravel automatically resolves policies for models, but this is not always the case. For instance, if your models are not in the default App\Models namespace, are nested, or are from third-party plugins, you may need to manually register the policies. You can do this in a service provider's boot() method:

Tip For your in-app resources' models you can add the following method in the boot() method to automatically enforce policies, without the need to manually register each policy. This assumes your policies are in the App\Policies namespace and follow the naming convention of appending Policy to the model class name. Adjust the str_replace parameters if your structure differs:

Resources

Shield derives resource permission keys from configured policy methods. Since Filament Resources' authorization is handled via policies, generated permissions align with policy methods.

Configuration

Subject

You can customize the subject used for resource permissions by setting the subject key in the resources configuration. The subject can be set to either class or model (default is model).

Manage

You can define resource-specific policy methods in the resources.manage configuration. This allows you to tailor the permissions for individual resources (in-app or third-party) based on their unique requirements. When you specify methods here, Shield will generate permissions for these methods in addition to the global methods defined in policies.methods, provided that policies.merge is set to true. This ensures that each resource has a comprehensive set of permissions that reflect both standard and resource-specific actions.

Exclude

You can exclude specific resources from permission generation by listing them in the resources.exclude configuration. This is useful for resources that should always be accessible or do not require permission checks. When a resource is excluded, Shield will skip generating permissions and policy for it.

Pages & Widgets

Both pages and widgets in Filament follow a similar permission model. By default, they require view permissions. You can customize their behavior in the configuration, including subject, prefix, exclusions, and enforcement traits.

Configuration

Pages

Widgets

Options

Option Description
Subject Determines how the permission subject is generated.
class → Uses the class name (default).
model → Uses the model name (if the entity has a static getModel() method).
Prefix Prepended to permission keys for distinction.
• Example for Pages: Page:IconLibrary
• Example for Widgets: Widget:IncomeWidget.
Exclude Entities listed here will be skipped during permission generation.
Useful for always-accessible entities like dashboards, account widgets, or system info.

Permission Enforcement

Use the appropriate Shield trait to automatically enforce permissions. When applied, these traits ensure:

Pages

Widgets

Custom Permissions

Most of the time you will have some ad-hoc permissions that don't fit into the Resource, Page, or Widget categories, or you might not want a policy method for them. You can define these under custom_permissions in the config:

They appear in the Role Resource's Custom Permissions tab when enabled. To enable the tab, set shield_resource.tabs.custom_permissions to true in the config.

Users (Assigning Roles to Users)

Shield does not come with a way to assign roles to your users out of the box; however, you can easily assign roles to your users using Filament's Forms Select or CheckboxList component. Inside your users Resource's form, add one of these components and configure them as needed:

  1. Without Tenancy

  2. With Tenancy

You can find out more about these components in the Filament Docs

Shield Plugin & Resource

The plugin provides several methods to handle resource-related customizations and overrides without publishing the resource. You can use the plugin as follows:

Navigation

You may use the following methods to customize the navigation of the RoleResource:

Labels

You may use the following methods to customize the labels of the RoleResource:

Global Search

You may use the following methods to customize the global search related functionality of the RoleResource:

Parent Resource

You may use the following method to set a parent resource for the RoleResource:

Tenancy

You may use the following methods to customize the tenancy related functionality of the RoleResource:

Layout Customization

  1. You can easily customize the Grid, Section and CheckboxList's columns() and columnSpan() without publishing the resource.

    Screenshot 2023-09-24 at 10 34 31 PM
  2. You can also make the resource tab to have a simple view like the other tabs by using the following method:

  3. When you have localization enabled and setup and you want the permission labels to react to your application's chosen locale/language you can use the following method:

Commands

Prohibited Commands

Since almost all Shield commands are destructive and can cause data loss, they can be prohibited by calling the prohibit method of the command as follows in a service provider's boot() method:

Core Commands

Generate Command Options (recap)

Seeder Command Options (recap)

Localization

Shield supports multiple languages out of the box. When enabled, you can provide translated labels for permissions to create a more localized experience for your app's users.

Configuration

How It Works

Shield uses a fallback chain for resolving permission labels:

  1. User's translation file (when localization.enabled = true)
    • Checks lang/{locale}/{key}.php where {key} is your configured localization key
  2. Package's default translations
    • Falls back to resource_permission_prefixes_labels for standard affixes (view, create, update, etc.)
  3. Headline fallback
    • Converts the key to a readable format (e.g., force_delete_any → "Force Delete Any")

Generating Translation Files

The easiest way to create a translation file is using the shield:translation command:

This generates a file at lang/en/shield-permissions.php containing all permission labels:

Translation Keys

All translation keys are in snake_case format:

Permission Type Original Key Translation Key
Resource affix viewAny view_any
Resource affix forceDeleteAny force_delete_any
Page permission view:Dashboard view_dashboard
Widget permission view:StatsOverview view_stats_overview
Custom permission Approve:Posts approve_posts

Default Package Translations

Shield includes translations for standard resource affixes in 32 languages. When localization.enabled = false, the package automatically uses these translations for affixes like view, create, update, delete, etc.

For entity labels (Resources, Pages, Widgets), Filament's entity related methods are used (getModelLabel(), getTitle(), getHeading(), etc.).

Upgrade

Upgrading from 3.x|4.0.0-Beta* versions to 4.x requires careful consideration due to significant changes in the package's architecture and functionality. Here are the key steps and considerations for a successful upgrade:

  1. Backup Your Data: Before making any changes, ensure you have a complete backup of your database and application files. This is crucial in case you need to revert to the previous version.
  2. Remove Config and Resource: Delete the existing filament-shield.php config file and the published RoleResource if you have done so. This is important to avoid conflicts with the new configuration and resource structure.
  3. Update Composer: Run composer require bezhansalleh/filament-shield to update the package to the latest version.
  4. Publish New Config and Resource: Publish the new configuration file and the RoleResource using the following commands:

  5. Adjust Configuration: Review and adjust the new filament-shield.php configuration file to match your application's requirements. Pay special attention to the new options and defaults that may differ from the previous version.
  6. HasShieldPermissions Contract is Deprecated: If you have implemented the HasShieldPermissions contract in your resources, consult Resources sections on how to migrate. If you leave it as is, it will be ignored.
  7. Clean Slate or Perserve: Decide whether to start fresh with a clean slate or preserve existing roles and permissions.

    1. Clean Slate: If you choose to start fresh, you can run the following command to and follow along to set up the package from scratch.

    2. Preserve Existing Data: If you want to keep your existing roles and permissions intact, then follow these steps:

      1. Add the following code to your AppServiceProvider's boot() method to perserve the the previous versions(3.x|4.x-Beta*) permission pattern:

      2. If you have used the configurePermissionIdentifierUsing() method to customize the permission key composition, then adjust the logic for resources above to match your custom logic.
      3. Running the shield:generate command

        • If your policies are altered or customized, you may need to run the generate command carefully per resource or set of resources to avoid any unwanted side effects. Then manually review and adjust the customized policies as needed. :

        • If you haven't customized your policies then run the following command to ensure that your policies are up to date with the latest version of Shield:

        • If you have tenancy enabled in your panels and you want to generate the tenancy relationships, you can add the --relationships flag to the above commands.
      4. Review and adjust the generated policies and permissions as needed.
  8. Test Thoroughly: After completing the upgrade, thoroughly test your application to ensure that all functionalities related to roles, permissions, and access control are working as expected. Pay special attention to any custom implementations you may have had in place.

Translations

Publish the translations using:

Testing

Changelog

See CHANGELOG.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of filament-shield with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2|^8.3
bezhansalleh/filament-plugin-essentials Version ^1.0
filament/filament Version ^4.0|^5.0
illuminate/contracts Version ^11.28|^12.0|^13.0
illuminate/support Version ^11.28|^12.0|^13.0
spatie/laravel-package-tools Version ^1.0
spatie/laravel-permission Version ^6.0|^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bezhansalleh/filament-shield contains the following files

Loading the files please wait ...