Download the PHP package thepinecode/policy without Composer

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

Policy

Using Laravel's authorization on the front-end.

A nice tool for SPAs and front-end heavy applications.

If you want to see behind the package, we suggest to read this post: Implementing Laravel’s Authorization on the Front-End.

Table of contents

  1. Getting Started
  2. Publishing and setting up the JavaScript library
    • Setting up the Gate.js
    • Initializing a gate instance
    • Passing the user to the gate instance
    • Using it as a Vue service
    • The @currentUser blade directive
  3. Using the policies and the Gate.js
    • The available methods
    • Adding the UsesModelName trait to the models
    • Generating policies with artisan
    • Writing the policy rules
  4. Example
  5. Contribute

Getting started

You can install the package with composer, running the composer require thepinecode/policy command.

Since the package supports auto-discovery, Laravel will register the service provider automatically behind the scenes.

In some cases you may disable auto-discovery for this package. You can add the provider class to the dont-discover array to disable it. Then you need to register it manually again.

Publishing and setting up the JavaScript library

By default the package provides a Gate.js file, that will handle the policies. Use the php artisan vendor:publish command and choose the Pine\Policy\PolicyServiceProvider provider. After publishing you can find your fresh copy in the resources/js/policies folder if you are using Laravel 5.7+. If your application is lower than 5.7, the JS will be published in the resources/assets/js/policies.

Setting up the Gate.js

Then you can import the Gate class and assign it to the window object.

Initializing a gate instance

From this point you can initialize the translation service anywhere from your application.

Passing the user to the gate instance

The Gate object requires a passed user to work properly. This can be a string or an object. By default, it looks for the window['user'] object, however you may customize the key or the object itself.

Note, you can pass any object as a user. If you pass a team or a group object, it works as well. Since you define the logic behind the Gate, you can pass anything you wish.

Using it as a Vue service

If you want to use it from Vue templates directly you can extend Vue with this easily.

The @currentUser blade directive

To make it quicker, the package comes with a @currentUser blade directive. This does nothing more, but to print the currently authenticated user as JSON and assign it to the window object.

You may override the default key for the user. You can do that by passing a string to the blade directive.

If there is no authenticated user, the value will be null.

Using the policies and the Gate.js

The available methods

allow()

The allow() accepts two parameters. The first is the action to perform, the second is the model object or the model name, like in Laravel.

Note: model name should be a lower case version of the actual model name in Laravel: for example Comment becomes comment.

deny()

The deny() has the same signature like allow() but it will negate its return value.

before()

Like in Laravel, in the before() method you can provide a custom logic to check for special conditions. If the condition passes, the rest of the policy rules in the allow() or deny() won't run at all. However if the condition fails, the policy rules will get place. To use the before() method, you may extend the gate object and define your custom logic.

Please note, to use the this object correctly, use the traditional function signature instead of the arrow (() => {}) functions.

Adding the UsesModelName trait to the models

Since, the policies use real JSON shaped eloquent models, the models have to use the Pine\Policy\UsesModelName trait that generates the proper model name. This model name attribute is used for pairing the proper policy with the model by the Gate.js.

Please note, to be able to use this attribute on the front-end, the attribute has to be appended to the JSON form. You can read more about appending values to JSON in the docs.

Generating policies with artisan

The package comes with an artisan command by default, that helps you to generate your JavaScript policies easily. To make a policy, run the php artisan make:js-policy Model command, where the Model is the model's name.

This command will create the CommentPolicy.js file next to the Gate.js in the resources/js/policies directory. If you are using lower than Laravel 5.7, the policies will be generated in the resources/assets/js/policies directory.

Note, the command will append the Policy automatically in the file name. It means you may pass only the model name when running the command.

After you generated the policy files, use npm to compile all the JavaScript, including policies.


Important!

The policies are registered automatically. It means, no need for importing them manually. The gate instance will automatically populate the policies. Every policy will be used where it matches with the model's model_name attribute.

Based on Laravel's default app.js the Gate instance registers the policies automatically when calling npm run dev, npm run prod and so on.


Writing the policy rules

Policies – like in Laravel – have the following methods by default: viewAny, view, create, update, restore, delete and forceDelete. Of course, you can use custom methods as well, policies are fully customizables.

Example

Contribute

If you found a bug or you have an idea connecting the package, feel free to open an issue.


All versions of policy with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2.5 | ^8.0
laravel/framework Version ^6.0 || ^7.0 || ^8.22.1
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 thepinecode/policy contains the following files

Loading the files please wait ....