Giter Club home page Giter Club logo

hexa-docs's Introduction

Filament Hexa

Filament Hexa is an effortless role & permission plugin created for Filament, inspired by the concept of hexters/ladmin. This concept provides ease in managing each role and permission inline with code and offers an easy-to-understand interface.

This plugin is intended only for Administrators, as it has a separate admin table from the user table provided by Laravel. Additionally, this plugin will replace the auth.php configuration file.

About Filament

FilamentPHP is a lightweight and flexible PHP framework designed for building web applications. It aims to simplify application development by providing a clear structure and high modularity. The framework emphasizes speed, efficiency, and comes with many built-in features that facilitate effective web application development.

Demo

If you want to try it, you can install the lite version available at Filament Hexa Lite.

Installation

Note
You need to install the filament package first. You can refer to the official site at FilamentPHP.

To install Filament Hexa, you must add the repository package to the composer.json file in the root of your project. Copy the command below and run it:

composer config repositories.filamenthexa.ppmarket.org \
    '{"type": "composer", "url": "https://filamenthexa.ppmarket.org"}' \
        --file composer.json

Once the repository is added to your composer.json file, you can install Filament Hexa like any other composer package by using the composer require command:

composer require hexters/hexa

Then, proceed with the installation of the hexa plugin:

php artisan hexa:install

Install database migrations:

php artisan migrate

Create a superadmin account for admin login:

php artisan hexa:account --create

Plugin Setup

Add the Filament Hexa plugin to the created panel. If you haven't created one yet, see how to do it here Creating a new panel.

use Filament\Panel;
use Hexters\Hexa\Hexa;
 
public function panel(Panel $panel): Panel
{
    return $panel
        ->plugins([
            Hexa::make(),
        ]);
}

Declaring Access Permissions

Resource, Page, & Cluster

To declare access permissions for Resources, Pages, and Clusters, see the example below:

use Filament\Resources\Resource;
use Hexters\Hexa\Traits\HexAccess;

. . .

use HexAccess;

protected static ?string $permissionId = 'access.user';

protected static ?string $descriptionPermission = 'Admin can manage User accounts';

/**
 * Additional permission (optional)
 * You can add it or not depending on the needs of your application.
 */
protected static ?array $subPermissions = [
    'access.user.create' => 'Can Create',
    'access.user.edit' => 'Can Edit',
    'access.user.delete' => 'Can Delete',
];

public static function canAccess(): bool
{
    return hexa()->can(static::$permissionId);
}

. . .

Widget

To declare access permissions for Widgets, there is a difference in the access method as it uses the canView() method.

use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Hexters\Hexa\Traits\HexAccess;

class StatsOverview extends BaseWidget
{
    use HexAccess;

    protected static ?string $permissionId = 'widget.overview';

    protected static ?string $descriptionPermission = 'Admin can view report overview';

    public static function canView(): bool
    {
        return hexa()->can(static::$permissionId);
    }

    . . .
}

This way, the sidebar menu for resources and pages will appear for roles that have access to them.

Additional Access

Apart from the methods above, you can add additional permissions for other needs outside of Page, Resource, Widget, and Cluster. You can add them in the file /config/other-permissions.php:

use Hexters\Hexa\Helpers\Can;

return [
    Can::make(id: 'receive.email.order')
        ->name(name: 'Allow receiving email orders')
        ->description(description: 'Admins with this role will receive emails from incoming customer orders.'),
];

Actions, etc.

You can use the visible() method on several Class Components. For example, let's try it on a button.

Tables\Actions\EditAction::make()
    ->visible(hexa()->can('access.user.edit')),

For giving access to classes extended to Filament\Resources\Pages\EditRecord, Filament\Resources\Pages\CreateRecord, Filament\Resources\Pages\ListRecords, you can use:

/**
 * @param  array<string, mixed>  $parameters
 */
public static function canAccess(array $parameters = []): bool
{
    return hexa()->can('access.user.edit');
}

Checking Access Permissions

Access can be granted to Resources, Pages, Widgets, Button Actions, etc. The access can be given as shown below.

Using the hexa utility function:

hexa()->can('hexa.admin')

Using Laravel's auth can function:

auth()->user()?->can('hexa.admin')

Using Laravel's Gate class:

use Illuminate\Support\Facades\Gate;

. . .

Gate::allows('hexa.admin')

In a blade template, you can use it as shown below.

<div>
    @can('hexa.admin')
        // Content here ...
    @endcan
</div>

Options Setting

This plugin comes with an easy-to-use cache system that stores various settings required by the application. Check the file app/Filament/Pages/Option.php. You can use the Form component to create various types of form inputs.

use Filament\Forms\Components\TextInput;

public function formOptions(Form $form): Form
{
    return $form
        ->schema([
            TextInput::make('referral-commision')
                ->default(hexa()->getOption('referral-commision', 10)) #<-- required to set the value
                ->required()
                ->suffix('%')
                ->numeric(),
        ]);
}

To call it, you can use the utility function provided by Hexa:

hexa()->getOption('referral-commision', 10)

If you want to save it manually, you can use the utility function below:

hexa()->setOption('key-option', 'The option value can be a string, array, number, etc.')

You can also retrieve the update date from this option by

hexa()->getOptionDate('key-option') // output : 2030-04-12 10:10:10

License

This plugin is not open source. You need a license to use this plugin. You can purchase it at Filament Hexa License or request it from the owner of this plugin.

Issue

If you encounter any issues with this plugin, you can submit them to the repository: Filament Hexa Issue

Thank you for using this plugin. We hope it speeds up your process in creating powerful applications.

Happy Coding ๐Ÿง‘โ€๐Ÿ’ป ๐Ÿง‘โ€๐Ÿ’ป ๐Ÿง‘โ€๐Ÿ’ป

hexa-docs's People

Contributors

hexters avatar

Watchers

 avatar

hexa-docs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.