Skip to content

Commit

Permalink
Ajout Config et migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
fnoual committed Nov 25, 2020
0 parents commit 6c15b3a
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 0 deletions.
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "fnoual/roles",
"description": "Laravel roles system",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Fawzi Noual",
"email": "[email protected]"
}
],
"require": {},
"autoload": {
"psr-4": {
"Fnoual\\Roles\\" : "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Fnoual\\Roles\\UseRoleServiceProvider"
]
}
}
}
42 changes: 42 additions & 0 deletions src/Console/InstallRolesPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Fnoual\Roles\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class InstallRolesPackage extends Command
{
protected $signature = 'roles:install';

protected $description = 'Installe le gestionnaire de roles';

public function handle()
{
$this->info('Installation ...');

$this->call('vendor:publish', [
'--provider' => "Fnoual\Roles\UseRoleServiceProvider"
]);

$this->call('migrate:fresh');

$this->info('Table créée avec succès.');

DB::table('roles')->insert([
'id' => 1,
'name' => 'admin',
'description' => 'Administrateur Système',
]);

DB::table('roles')->insert([
'id' => 2,
'name' => 'user',
'description' => 'Utilisateur',
]);

$this->info('Rôles admin et utilisateurs créés 👍');

$this->info('Package installé 🚀');
}
}
18 changes: 18 additions & 0 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


namespace Fnoual\Roles\Models;


use App\User;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
protected $table = 'roles';

public function users()
{
return $this->belongsToMany(User::class);
}
}
59 changes: 59 additions & 0 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Fnoual\Roles\Traits;

use Fnoual\Roles\Models\Role;

trait HasRoles
{
public function roles()
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
}

/**
* @param string|array $roles
* @return bool
*/
public function authorizeRoles($roles)
{
if (is_array($roles)) {
return $this->hasAnyRole($roles) ||
abort(401, 'Cette action n\'est pas autorisée.');
}
return $this->hasRole($roles) ||
abort(401, 'Cette action n\'est pas autorisée.');
}

/**
* Vérifier plusieurs rôles
* @param array $roles
* @return bool
*/
public function hasAnyRole($roles)
{
return null !== $this->roles()->whereIn('name', $roles)->first();
}

/**
* Vérifier un rôle
* @param string $role
* @return bool
*/
public function hasRole($role)
{
return null !== $this->roles()->where('name', $role)->first();
}

public function isAdmin()
{
return $this->roles->first()->id === config('roles.super_user_role_id');
}

public static function boot()
{
self::created(function ($model) {
$model->roles()->attach(Role::find(config('roles.user_role_id')));
});
}
}
42 changes: 42 additions & 0 deletions src/UseRoleServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Fnoual\Roles;

use Fnoual\Roles\Console\InstallRolesPackage;
use Illuminate\Support\ServiceProvider;

class UseRoleServiceProvider extends ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(__DIR__.'/config/roles.php', 'roles');
}

public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
InstallRolesPackage::class,
]);
if (! class_exists('CreateRolesTable')) {
$this->publishes([
__DIR__ . '/database/migrations/create_roles_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_roles_table.php'),
], 'migrations');
}
if (! class_exists('CreateRolesTable')) {
$this->publishes([
__DIR__ . '/database/migrations/create_role_user_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_role_user_table.php'),
], 'migrations');
}
if (! class_exists('RolesTableSeeder')) {
$this->publishes([
__DIR__ . '/database/seeders/RolesTableSeeder.php.stub' => database_path('seeds/RolesTableSeeder.php'),
], 'seeds');
}
$this->publishes([
__DIR__.'/config/roles.php' => config_path('roles.php'),
], 'config');
}

}
}
12 changes: 12 additions & 0 deletions src/config/roles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
/*
* Admin entity attributes
*/
'super_user_role_id' => 1,
/*
* ID par défaut du rôle utilisateur
*/
'user_role_id' => 2,
];
33 changes: 33 additions & 0 deletions src/database/migrations/create_role_user_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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()
{
Schema::create('role_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
34 changes: 34 additions & 0 deletions src/database/migrations/create_roles_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('icon')->default('user');
$table->string('description');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
26 changes: 26 additions & 0 deletions src/database/seeders/RolesTableSeeder.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Fnoual\Roles\Models\Role;
use Illuminate\Database\Seeder;

class RolesTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
Role::create([
'id' => 1,
'name' => 'admin',
'description' => 'Administrateur système'
]);
Role::create([
'id' => 2,
'name' => 'user',
'description' => 'Utilisateur'
]);
}
}

0 comments on commit 6c15b3a

Please sign in to comment.