/laraGates

A project to explore laravel authorization gates, developed based on https://www.youtube.com/watch?v=q-Fl_Qcfy5o

Primary LanguagePHP

A project built using the Laravel Framework

Laravel Logo


About LaraGates

LaraAppoint is a youtube crash course project built based on this video posted by Great Adib.

It is built as a Posting Management System that intends to showcase Laravel Gates and Policies implementation.


LaraGates Use-Case

In this post, we’ll make a toy posts app to show how Gate gives you liberty and decoupling. The web app will have two user roles (authors and editors) with the following permissions:

  • Authors can view all post listings.
  • Authors can view their post content.
  • Authors can delete their post.

Within LaraGates

This project utilizes the initial Laravel v10 framework with the inclusions bootstrap UI framework to automate the creation of a complete authentication system. While using the framework, Vite.js is required to render its view.

  • composer require laravel/ui
  • php artisan ui bootstrap --auth
  • npm install
  • npm run dev

Gates are mainly created by defining user Roles inside the User model/table. Three types of user roles are created which are Admin, Editor, and Viewer. Then, these roles are again defined as Gate inside the 'app\Providers\AppServiceProvider.php' file.

Next, Policies are also created to define the permissions bundled with the user-post relationship using artisan 'php artisan make:policy PostPolicy --model=Post'. Inside this file, the permissiosn are defined explicitly in the action class generated by the artisan.

In summary, Route Guards are implemented using middleware('can:params') function inside the 'routes\web.php' file. Controller Guards are implemented using Gate::allows('params') function & authorize('params', $model) function inside Controller files. Page Element Guards are implemented using @can('params', $model) .. @endcan function inside the View files. These parameters are coming from two sources:

  • app\Providers\AppServiceProvider.php
  • app\Policies\PostPolicy.php

This project utilizes MySQL database. The SQL file pertaining to the database is included inside this repo named LaraGates.sql which has several test data. Similarly, this repo also encompasses the required factories, migrations, and seeders which can be executed as to showcase the same results of that of the included SQL file.

Last but not least, the project was build using these version of frameworks:

  • PHP 8.1.10
  • MySQL 8.0.30
  • Laravel 10.1.5

So with that, I humbly thank you.