-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6c15b3a
Showing
9 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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é 🚀'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
]); | ||
} | ||
} |