Skip to content

Commit

Permalink
PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Jun 8, 2022
1 parent 80b71cc commit 35b5560
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 302 deletions.
62 changes: 31 additions & 31 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
{
"name": "rodrigopedra/laravel-simple-acl",
"description": "Laravel Simple ACL",
"keywords": [
"Laravel",
"acl"
],
"license": "MIT",
"authors": [
{
"name": "Rodrigo Pedra Brum",
"email": "[email protected]"
"name": "rodrigopedra/laravel-simple-acl",
"description": "Laravel Simple ACL",
"keywords": [
"Laravel",
"acl"
],
"license": "MIT",
"authors": [
{
"name": "Rodrigo Pedra Brum",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"RodrigoPedra\\LaravelSimpleACL\\": "src/LaravelSimpleACL/"
}
},
"require": {
"php": "^7.2.5|^8.0",
"illuminate/support": "^6.20.12|^7.30.4|^8.22.1|^9.0",
"illuminate/config": "^6.20.12|^7.30.4|^8.22.1|^9.0",
"illuminate/database": "^6.20.12|^7.30.4|^8.22.1|^9.0",
"illuminate/http": "^6.20.12|^7.30.4|^8.22.1|^9.0"
},
"extra": {
"laravel": {
"providers": [
"RodrigoPedra\\LaravelSimpleACL\\SimpleACLServiceProvider"
]
}
}
],
"autoload": {
"psr-4": {
"RodrigoPedra\\LaravelSimpleACL\\": "src/LaravelSimpleACL/"
}
},
"require": {
"php": ">=5.5.0",
"illuminate/support": "^5.4|^6.0",
"illuminate/config": "^5.4|^6.0",
"illuminate/database": "^5.4|^6.0",
"illuminate/http": "^5.4|^6.0"
},
"extra": {
"laravel": {
"providers": [
"RodrigoPedra\\LaravelSimpleACL\\SimpleACLServiceProvider"
]
}
}
}
2 changes: 1 addition & 1 deletion src/config.php → config/simple-acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

return [
'db-connection' => null,
'user-class' => 'App\\User',
'user-class' => 'App\\User',
];
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class CreateRolesTable extends Migration
*/
public function up()
{
Schema::connection( 'simple-acl' )
->create( 'roles', function ( Blueprint $table ) {
$table->increments( 'id' );
Schema::connection('simple-acl')
->create('roles', function (Blueprint $table) {
$table->increments('id');

$table->unsignedSmallInteger( 'sort_index' )->nullable();
$table->unsignedSmallInteger('sort_index')->nullable();

$table->string( 'label' )->unique();
$table->string( 'description' );
$table->string('label')->unique();
$table->string('description');

$table->timestamps();
$table->softDeletes();
} );
});
}

/**
Expand All @@ -34,6 +34,6 @@ public function up()
*/
public function down()
{
Schema::connection( 'simple-acl' )->dropIfExists( 'roles' );
Schema::connection('simple-acl')->dropIfExists('roles');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class CreatePermissionsTable extends Migration
*/
public function up()
{
Schema::connection( 'simple-acl' )
->create( 'permissions', function ( Blueprint $table ) {
$table->increments( 'id' );
Schema::connection('simple-acl')
->create('permissions', function (Blueprint $table) {
$table->increments('id');

$table->unsignedSmallInteger( 'sort_index' )->nullable();
$table->unsignedSmallInteger('sort_index')->nullable();

$table->string( 'label' )->unique();
$table->string( 'description' );
$table->string('label')->unique();
$table->string('description');

$table->timestamps();
$table->softDeletes();
} );
});
}

/**
Expand All @@ -34,6 +34,6 @@ public function up()
*/
public function down()
{
Schema::connection( 'simple-acl' )->dropIfExists( 'permissions' );
Schema::connection('simple-acl')->dropIfExists('permissions');
}
}
49 changes: 49 additions & 0 deletions migrations/2017_09_17_100000_create_role_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$userClassName = config('simple-acl.user-class');

/** @var \Illuminate\Database\Eloquent\Model $userModel */
$userModel = new $userClassName();

Schema::connection('simple-acl')
->create('role_user', function (Blueprint $table) use ($userModel) {
$userForeignKey = $userModel->getForeignKey();

$table->unsignedInteger('role_id');
$table->unsignedInteger($userForeignKey);

$table->primary([$userForeignKey, 'role_id']);

$table->timestamps();

$table->foreign('role_id')->references('id')->on('roles');

$table->foreign($userForeignKey)
->references($userModel->getKeyName())
->on($userModel->getTable());
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('simple-acl')->dropIfExists('role_user');
}
}
39 changes: 39 additions & 0 deletions migrations/2017_09_17_100001_create_permission_role_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePermissionRoleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('simple-acl')
->create('permission_role', function (Blueprint $table) {
$table->unsignedInteger('permission_id');
$table->unsignedInteger('role_id');

$table->primary(['role_id', 'permission_id']);

$table->timestamps();

$table->foreign('permission_id')->references('id')->on('permissions');
$table->foreign('role_id')->references('id')->on('roles');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('simple-acl')->dropIfExists('permission_role');
}
}
49 changes: 49 additions & 0 deletions migrations/2017_09_17_100010_create_permission_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePermissionUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$userClassName = config('simple-acl.user-class');

/** @var \Illuminate\Database\Eloquent\Model $userModel */
$userModel = new $userClassName();

Schema::connection('simple-acl')
->create('permission_user', function (Blueprint $table) use ($userModel) {
$userForeignKey = $userModel->getForeignKey();

$table->unsignedInteger('permission_id');
$table->unsignedInteger($userForeignKey);

$table->primary([$userForeignKey, 'permission_id']);

$table->timestamps();

$table->foreign('permission_id')->references('id')->on('permissions');

$table->foreign($userForeignKey)
->references($userModel->getKeyName())
->on($userModel->getTable());
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('simple-acl')->dropIfExists('permission_user');
}
}
25 changes: 12 additions & 13 deletions src/LaravelSimpleACL/Http/Middleware/LoadSimpleACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@ class LoadSimpleACL
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle( $request, Closure $next )
public function handle($request, Closure $next)
{
$user = $request->user();

if (is_null( $user )) {
return $next( $request );
if (is_null($user)) {
return $next($request);
}

$user->load( [ 'roles', 'permissions' ] );
$user->load(['roles', 'permissions']);

$user->permissions->each( function ( Permission $permission ) use ( $user, $request ) {
Gate::define( $permission->label, function () use ( $permission, $user, $request ) {
return $user->hasPermission( $permission );
} );
} );
$user->permissions->each(function (Permission $permission) use ($user, $request) {
Gate::define($permission->label, function () use ($permission, $user, $request) {
return $user->hasPermission($permission);
});
});

return $next( $request );
return $next($request);
}
}
18 changes: 9 additions & 9 deletions src/LaravelSimpleACL/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Permission extends Model
use SoftDeletes;

protected $connection = 'simple-acl';
protected $table = 'permissions';
protected $table = 'permissions';

protected $fillable = [
'label',
Expand All @@ -20,32 +20,32 @@ class Permission extends Model
];

protected $casts = [
'id' => 'integer',
'id' => 'integer',
'sort_index' => 'integer',
];

protected $dates = [ 'deleted_at' ];
protected $dates = ['deleted_at'];

public function roles()
{
return $this->belongsToMany( Role::class, 'permission_role' )->withTimestamps();
return $this->belongsToMany(Role::class, 'permission_role')->withTimestamps();
}

public function users()
{
return $this->belongsToMany( config( 'simple-acl.user-class' ), 'permission_user' )->withTimestamps();
return $this->belongsToMany(config('simple-acl.user-class'), 'permission_user')->withTimestamps();
}

public function scopeHasLabel( Builder $builder, $label )
public function scopeHasLabel(Builder $builder, $label)
{
$builder->where( 'label', $label );
$builder->where('label', $label);

return $builder;
}

public function scopeOrdered( Builder $builder )
public function scopeOrdered(Builder $builder)
{
$builder->orderBy( 'sort_index' )->orderBy( 'description' );
$builder->orderBy('sort_index')->orderBy('description');

return $builder;
}
Expand Down
Loading

0 comments on commit 35b5560

Please sign in to comment.