Skip to content

Commit 2e02534

Browse files
committed
Initial commit
0 parents  commit 2e02534

14 files changed

+457
-0
lines changed

composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "amir/laravel-permission",
3+
"description": "an easy and flexible way for permissions management",
4+
"authors": [
5+
{
6+
"name": "Amir Yousefi",
7+
"email": "amiryousefi.it@gmail.com"
8+
}
9+
],
10+
"require": {},
11+
"autoload": {
12+
"psr-4": {
13+
"amir\\laravelpermission\\": "src/"
14+
}
15+
}
16+
}

src/Commands/PermissionsClear.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace amir\laravelpermission\Commands;
4+
5+
use amir\laravelpermission\Models\Permission;
6+
use amir\laravelpermission\Models\Role;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\DB;
9+
10+
class PermissionsClear extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'permissions:clear {--roles=*} {--tables=*} {--all}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Clear permissions';
25+
26+
/**
27+
* Create a new command instance.
28+
*
29+
* @return void
30+
*/
31+
public function __construct()
32+
{
33+
parent::__construct();
34+
}
35+
36+
/**
37+
* Execute the console command.
38+
*
39+
* @return mixed
40+
*/
41+
public function handle()
42+
{
43+
$options = $this->options();
44+
45+
DB::transaction(function () use ($options) {
46+
$no_options = count($options['tables']) == 0 && !$options['all'];
47+
48+
$roles_options = $options['roles'];
49+
$tables_options = $options['tables'];
50+
51+
$old_roles = Role::whereIn('name', $roles_options)->get();
52+
53+
if (in_array('permission_role', $tables_options) || $no_options) {
54+
$query = DB::table('permission_role');
55+
56+
if (count($roles_options) > 0) {
57+
$old_roles_id = $old_roles->pluck('id');
58+
$query->whereIn('role_id', $old_roles_id)->delete();
59+
} else {
60+
$query->delete();
61+
}
62+
}
63+
64+
if (in_array('permissions', $tables_options) || $options['all'] || $no_options) {
65+
if (count($roles_options) > 0) {
66+
Permission::whereHas('roles', function ($query) use ($roles_options) {
67+
$query->whereIn('name', $roles_options);
68+
})->delete();
69+
} else {
70+
DB::table('permission_role')->delete();
71+
Permission::query()->delete();
72+
}
73+
}
74+
75+
if (in_array('roles', $tables_options) || $options['all'] || $no_options) {
76+
if (count($roles_options) > 0) {
77+
Role::whereIn('name', $roles_options)->delete();
78+
} else {
79+
DB::table('permission_role')->delete();
80+
Role::query()->delete();
81+
}
82+
}
83+
});
84+
}
85+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace amir\laravelpermission\Commands;
4+
5+
use amir\laravelpermission\Models\Permission;
6+
use amir\laravelpermission\Models\Role;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\Route;
9+
10+
class PermissionsGenerate extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'permissions:generate {--fresh}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Generates permissions based on your routes';
25+
26+
/**
27+
* Create a new command instance.
28+
*
29+
* @return void
30+
*/
31+
public function __construct()
32+
{
33+
parent::__construct();
34+
}
35+
36+
/**
37+
* Execute the console command.
38+
*
39+
* @return mixed
40+
*/
41+
public function handle()
42+
{
43+
$options = $this->options();
44+
if ($options['fresh']){
45+
Permission::query()->delete();
46+
Role::query()->delete();
47+
}
48+
$routes = Route::getRoutes()->getRoutes();
49+
50+
foreach ($routes as $route){
51+
$action = $route->getActionname();
52+
if($action == "Closure"){
53+
continue;
54+
}
55+
$name = $route->getName();
56+
57+
$permission = Permission::firstOrCreate(['action'=>$action, 'name'=>$name]);
58+
59+
if(key_exists('role', $route->action)){
60+
$role = $route->action['role'];
61+
$role = Role::firstOrCreate(['name'=> $role]);
62+
63+
$role->permissions()->syncWithoutDetaching($permission->id);
64+
}
65+
}
66+
}
67+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateRolesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('roles', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name')->unique();
19+
$table->string('description')->nullable();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('roles');
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddRoleIdToUsers extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('users', function (Blueprint $table) {
17+
$table->unsignedBigInteger('role_id')->nullable();
18+
19+
$table->foreign('role_id')
20+
->references('id')
21+
->on('roles');
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::table('users', function (Blueprint $table) {
33+
$table->dropColumn('role_id');
34+
});
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreatePermissionTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('permissions', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name')->nullable()->unique();
19+
$table->string('action')->nullable();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('permission');
31+
}
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreatePermissionRoleTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('permission_role', function (Blueprint $table) {
17+
$table->unsignedBigInteger('permission_id');
18+
$table->unsignedBigInteger('role_id');
19+
20+
$table->foreign('permission_id')
21+
->references('id')
22+
->on('permissions')
23+
->onDelete('cascade');
24+
25+
$table->foreign('role_id')
26+
->references('id')
27+
->on('roles')
28+
->onDelete('cascade');
29+
30+
$table->primary(['permission_id', 'role_id']);
31+
});
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::dropIfExists('permission_role');
42+
}
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace amir\laravelpermission\Exception;
4+
5+
use Symfony\Component\HttpKernel\Exception\HttpException;
6+
7+
class UnauthenticatedException extends HttpException
8+
{
9+
10+
public static function notLoggedIn()
11+
{
12+
return new static(401, 'User is not logged in', null, []);
13+
}
14+
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace amir\laravelpermission\Exception;
4+
5+
6+
use Symfony\Component\HttpKernel\Exception\HttpException;
7+
8+
class UnauthorizedException extends HttpException
9+
{
10+
11+
public static function noPermission()
12+
{
13+
return new static(403, 'User done\'t have permission', null, []);
14+
}
15+
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace amir\laravelpermission;
3+
use amir\laravelpermission\Middleware\AuthRoles;
4+
use Illuminate\Support\ServiceProvider;
5+
use Illuminate\Routing\Router;
6+
7+
class LaravelPermissionServiceProvider extends ServiceProvider {
8+
public function boot(Router $router)
9+
{
10+
$this->loadMigrationsFrom(__DIR__.'/Database/migrations');
11+
12+
if ($this->app->runningInConsole()) {
13+
$this->commands([
14+
Commands\PermissionsGenerate::class,
15+
Commands\PermissionsClear::class,
16+
]);
17+
}
18+
19+
$router->aliasMiddleware('auth.role', AuthRoles::class);
20+
}
21+
public function register()
22+
{
23+
}
24+
}
25+
?>

0 commit comments

Comments
 (0)