Download the PHP package sunergix/entrust-permissions without Composer

On this page you can find all versions of the php package sunergix/entrust-permissions. 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 entrust-permissions

ENTRUST (Laravel 12|13 Package)

Forked from zizaco/entrust

Entrust is a succinct and flexible way to add role-based permissions to Laravel 12|13.

If you are using an older version of laravel, use version ~3.0

Contents

Installation

1) Install the package with Composer:

2) Laravel package discovery registers the service provider and facade automatically. If your application disables package discovery, add the service provider manually:

3) If needed, add the facade alias manually:

4) Run the command below to publish the package config file config/entrust.php:

5) Open your config/auth.php and add the following to it:

6) If you want to use Middleware, register aliases in your bootstrap middleware configuration:

For Laravel 12 and 13, register aliases with bootstrap/app.php middleware configuration.

Configuration

Set the property values in the config/auth.php. These values will be used by entrust to refer to the correct user table and model.

To further customize table names and model namespaces, edit the config/entrust.php.

User relation to roles

Now generate the Entrust migration:

It will generate the <timestamp>_entrust_setup_tables.php migration. You may now run it with the artisan migrate command:

After the migration, four new tables will be present:

Models

Role

Create a Role model inside app/models/Role.php using the following example:

The Role model has three main attributes:

Both display_name and description are optional; their fields are nullable in the database.

Permission

Create a Permission model inside app/models/Permission.php using the following example:

The Permission model has the same three attributes as the Role:

In general, it may be helpful to think of the last two attributes in the form of a sentence: "The permission display_name allows a user to description."

User

Next, use the EntrustUserTrait trait in your existing User model. For example:

This will enable the relation with Role and add the following methods roles(), hasRole($name), withRole($name), hasPermission($permission), and ability($roles, $permissions, $options) within your User model.

Don't forget to dump composer autoload

And you are ready to go.

Soft Deleting

The default migration takes advantage of onDelete('cascade') clauses within the pivot tables to remove relations when a parent record is deleted. If for some reason you cannot use cascading deletes in your database, the EntrustRole and EntrustPermission classes, and the HasRole trait include event listeners to manually delete records in relevant pivot tables. In the interest of not accidentally deleting data, the event listeners will not delete pivot data if the model uses soft deleting. However, due to limitations in Laravel's event listeners, there is no way to distinguish between a call to delete() versus a call to forceDelete(). For this reason, before you force delete a model, you must manually delete any of the relationship data (unless your pivot tables uses cascading deletes). For example:

Usage

Concepts

Let's start by creating the following Roles and Permissions:

Next, with both roles created let's assign them to the users. Thanks to the HasRole trait this is as easy as:

Now we just need to add permissions to those Roles:

Checking for Roles & Permissions

Now we can check for roles and permissions simply by doing:

Both hasRole() and hasPermission() can receive an array of roles & permissions to check:

By default, if any of the roles or permissions are present for a user then the method will return true. Passing true as a second parameter instructs the method to require all of the items:

You can have as many Roles as you want for each User and vice versa.

The Entrust class has shortcuts to both can() and hasRole() for the currently logged in user. The user model method is named hasPermission() to avoid colliding with Laravel's Gate-aware can() method:

You can also use placeholders (wildcards) to check any matching permission by doing:

To filter users according a specific role, you may use withRole() scope, for example to retrieve all admins:

User ability

More advanced checking can be done using the awesome ability function. It takes in three parameters (roles, permissions, options):

Either of the roles or permissions variable can be a comma separated string or array:

This will check whether the user has any of the provided roles and permissions. In this case it will return true since the user is an admin and has the create-post permission.

The third parameter is an options array:

Here is an example output:

The Entrust class has a shortcut to ability() for the currently logged in user:

Blade templates

Three directives are available for use within your Blade templates. What you give as the directive arguments will be directly passed to the corresponding Entrust function.

Middleware

You can use a middleware to filter routes and route groups by permission or role

It is possible to use pipe symbol as OR operator:

To emulate AND functionality just use multiple instances of middleware

For more complex situations use ability middleware which accepts 3 parameters: roles, permissions, validate_all

As you can see, Entrust::hasRole() and Entrust::can() check if the user is logged in, and then if they have the role or permission. If the user is not logged the return will also be false.

Troubleshooting

If you encounter an error when doing the migration that looks like:

Then it's likely that the id column in your user table does not match the user_id column in role_user. Laravel's default user id is an unsigned big integer, and the generated migration now uses unsignedBigInteger for pivot foreign keys.

When trying to use the EntrustUserTrait methods, you encounter the error which looks like

Class name must be a valid object or a string

then probably you don't have published Entrust assets or something went wrong when you did it. First of all check that you have the entrust.php file in your config directory. If you don't, then try php artisan vendor:publish --tag=entrust-config and, if it does not appear, manually copy the /vendor/zizaco/entrust/src/config/config.php file in your config directory and rename it entrust.php.

If your app uses a custom namespace then you'll need to tell entrust where your permission and role models are, you can do this by editing the config file in config/entrust.php

License

Entrust is free software distributed under the terms of the MIT license.

Contribution guidelines

Support follows PSR-1 and PSR-4 PHP coding standards, and semantic versioning.

Please report any issue you find in the issues page. Pull requests are welcome.


All versions of entrust-permissions with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/console Version ^12.0 || ^13.0
illuminate/support Version ^12.0 || ^13.0
illuminate/cache Version ^12.0 || ^13.0
illuminate/database Version ^12.0 || ^13.0
illuminate/http Version ^12.0 || ^13.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 sunergix/entrust-permissions contains the following files

Loading the files please wait ...