Download the PHP package gecche/laravel-policy-builder without Composer

On this page you can find all versions of the php package gecche/laravel-policy-builder. 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 laravel-policy-builder

License Laravel Laravel Laravel Laravel Laravel Laravel Laravel

laravel-policy-builder

A simple and convenient way to build allowed list of Eloquent models according to policies.

Description

In many apps you use Laravel's Policies for checking if an user is allowed to handle a resource. Usually, in those apps, you also have to get lists of allowed resources accordingly to policies.

By using this package you store the business logic of filtering lists of resources directly in the policies and you get such lists by simply calling the method acl when using an Eloquent Builder.

Documentation

Version Compatibility

Laravel PolicyBuilder
5.5.x 1.1.x
5.6.x 1.2.x
5.7.x 1.3.x
5.8.x 1.4.x
6.x 2.x
7.x 3.x
8.x 4.x
9.x 5.x
10.x 10.x
11.x 11.x

Installation

Add gecche/laravel-policy-builder as a requirement to composer.json:

This package makes use of the discovery feature.

Basic usage

Define the business logic of building allowed lists of models in the policies

Let us suppose to have an Author Model class and a standard AuthorPolicy class for defining ability methods as usual.

Simply add directly in the AuthorPolicy class the business logic for filtering lists of Author. E.g.:

Get the allowed list of models for an user

Now, to get the allowed list of authors for the currently authenticated user, simply do:

If you want the list for the user 3, simply do:

Now the lists returns only italian authors.

Default list

Let us consider another Book model for which either the acl method has not been defined in its BookPolicy or there is no BookPolicy at all.

If we do:

we get the empty list of models for any user.

Beyond the basics

Once installed, other than the acl Eloquent Builder macro, the package provides the PolicyBuilderServiceProvider (together with the PolicyBuilder facade) which performs the underlying machinery for linking the Eloquent Builder with the policies (by wrapping the Laravel's Gate provider) and it offers some useful methods.

Basic default builder methods: all and none

The PolicyBuilder has two public methods, namely all and none which basically, given an Eloquent Builder adn (optionally) the model class name, return respectively the list of all available models (no filters at all) and the empty list.

The return of above methods can be customized by using the setAllBuilder and setNoneBuilder methods.

In the following example we change the previous AuthorPolicy class with the
PolicyBuilder's all method, but we leave the same semantics as before.

As before for both user 1 and 2 the full list of authors is returned if we do:

However we can set globally a different semantics for the PolicyBuilder's all method, e.g.:

In the above example when the all method is called the list of authors lacks the author with id 1.

The same can be done with the PolicyBuilder's none method.

Changing the "context"

Usually, an user either can access or not a certain model. But there are some cases in which, under certain "context", we need to built a list of allowed models which is different than the standard one.

For example, an user can view the whole list of Author models in the library, but it cannot edit all of them. So we want to build also the list of books which the user can edit and we are changing to the editing "context" with a different business logic for building the list.

In that case, simply pass the "context" to the builder:

In the AuthorPolicy you have to define accordingly the aclEditing method as done before for the acl one.

beforeAcl PolicyBuilder and Policy methods

Like the Laravel's Gate before method, PolicyBuilder has a beforeAcl method for registering "beforeAcl" callbacks. If a registered callback returns an Eloquent Builder, further elaboration is not needed and thus no policy is needed at all. E.g.:

A very similar beforeAcl method can also be placed into a single policy and it will be handled by the PolicyBuilderServiceProvider before elaborating any other method in the policy.

In the above example, the guest user has no access at all to the authors.


All versions of laravel-policy-builder with dependencies

PHP Build Version
Package Version
Requires laravel/framework Version ^11.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 gecche/laravel-policy-builder contains the following files

Loading the files please wait ....