Skip to content

Instantly share code, notes, and snippets.

View programarivm's full-sized avatar

Jordi Bassagana programarivm

View GitHub Profile
@programarivm
programarivm / Restaurant.php
Last active October 23, 2019 12:28
Many-to-many relationship linked through an entity table
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Restaurant extends Model
{
public function reviews()
{
@programarivm
programarivm / ReviewController.php
Last active October 25, 2019 12:12
Example showing how the Review model can be used in a controller
<?php
namespace App\Http\Controllers;
use App\Review;
use Illuminate\Http\Request;
class ReviewController extends Controller
{
public function results()
@programarivm
programarivm / start.sh
Last active December 25, 2019 18:13
bash/dev/start.sh
#!/bin/bash
read -p "This will bootstrap the development environment. Are you sure to continue? (y|n) " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
# cd the app's root directory
@programarivm
programarivm / Acl.php
Last active December 28, 2019 12:19
Acl model
<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Acl extends Model
{
protected $fillable = ['resource', 'role'];
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAclsTable extends Migration
{
/**
* Run the migrations.
@programarivm
programarivm / AclSetup.php
Last active December 26, 2019 19:55
Artisan command to easily set up the ACL
<?php
namespace App\Console\Commands;
use App\Acl;
use Illuminate\Console\Command;
class AclSetup extends Command
{
/**
@programarivm
programarivm / gist:530e8f4f74c78d68a5e6c57df7cdc988
Created November 5, 2019 19:05
The acls table is seeded with the permissions data
mysql> select * from acls;
+----+-----------------------------+-------------+---------------------+---------------------+
| id | resource | role | created_at | updated_at |
+----+-----------------------------+-------------+---------------------+---------------------+
| 1 | ReviewController@store | ROLE_BASIC | 2019-11-04 21:53:18 | 2019-11-04 21:53:18 |
| 2 | RestaurantController@index | ROLE_EDITOR | 2019-11-04 21:53:18 | 2019-11-04 21:53:18 |
| 3 | RestaurantController@show | ROLE_EDITOR | 2019-11-04 21:53:18 | 2019-11-04 21:53:18 |
| 4 | RestaurantController@update | ROLE_EDITOR | 2019-11-04 21:53:18 | 2019-11-04 21:53:18 |
| 5 | RestaurantController@delete | ROLE_EDITOR | 2019-11-04 21:53:18 | 2019-11-04 21:53:18 |
| 6 | ReviewController@delete | ROLE_EDITOR | 2019-11-04 21:53:18 | 2019-11-04 21:53:18 |
@programarivm
programarivm / Acl.php
Last active December 28, 2019 12:20
ACL middleware
<?php
namespace App\Http\Middleware;
use Closure;
class Acl
{
/**
* Handle an incoming request.
@programarivm
programarivm / Kernel.php
Last active November 5, 2019 19:26
Adding the new middleware to the $routeMiddleware variable
<?php
// ...
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
@programarivm
programarivm / api.php
Created November 5, 2019 19:30
The ACL middleware is ready to be used in the API's routes.
<?php
use Illuminate\Http\Request;
Route::post('/auth/login', 'AuthController@login');
Route::post('/auth/logout', 'AuthController@logout')->middleware('jwt.authorizer');
Route::get('restaurants', 'RestaurantController@index')->middleware('jwt.authorizer', 'acl');
Route::get('restaurants/{restaurant}', 'RestaurantController@show')->middleware('jwt.authorizer', 'acl');
Route::post('restaurants', 'RestaurantController@store')->middleware('jwt.authorizer', 'acl');