-
Notifications
You must be signed in to change notification settings - Fork 4
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 53728e3
Showing
15 changed files
with
575 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 @@ | ||
# laravolt/acl |
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,40 @@ | ||
{ | ||
"name": "laravolt/acl", | ||
"description": "Basic Laravel ACL", | ||
"keywords": [ | ||
"laravolt", | ||
"acl" | ||
], | ||
"homepage": "https://github.com/laravolt/acl", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Bayu Hendra Winata", | ||
"email": "[email protected]", | ||
"homepage": "http://id-laravel.com", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php" : ">=5.5.0", | ||
"illuminate/support": "~5.1", | ||
"illuminate/database": "~5.1", | ||
"myclabs/php-enum": "^1.4" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit" : "4.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Laravolt\\Acl\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Laravolt\\Acl\\Test\\": "tests" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "phpunit" | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
/* | ||
* Set specific configuration variables here | ||
*/ | ||
return [ | ||
'permission_enum' => \Laravolt\Acl\Enum\Permission::class, | ||
'is_admin' => function ($user) { | ||
return false; | ||
}, | ||
]; |
31 changes: 31 additions & 0 deletions
31
database/migrations/2015_10_19_044502_create_acl_roles_table.php
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,31 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateAclRolesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('acl_roles', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('acl_roles'); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
database/migrations/2015_10_19_044508_create_acl_permissions_table.php
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,31 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateAclPermissionsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('acl_permissions', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('acl_permissions'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
database/migrations/2015_10_20_044426_create_acl_permission_role_table.php
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,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateAclPermissionRoleTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('acl_permission_role', function (Blueprint $table) { | ||
$table->unsignedInteger('permission_id'); | ||
$table->unsignedInteger('role_id'); | ||
|
||
$table->foreign('permission_id')->references('id')->on('acl_permissions')->onDelete('cascade'); | ||
$table->foreign('role_id')->references('id')->on('acl_roles')->onDelete('cascade'); | ||
|
||
$table->primary(['permission_id', 'role_id']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('acl_permission_role'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
database/migrations/2015_10_20_044443_create_acl_role_user_table.php
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,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateAclRoleUserTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('acl_role_user', function (Blueprint $table) { | ||
$table->unsignedInteger('role_id'); | ||
$table->unsignedInteger('user_id'); | ||
|
||
$table->foreign('role_id')->references('id')->on('acl_roles')->onDelete('cascade'); | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
|
||
$table->primary(['role_id', 'user_id']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('acl_role_user'); | ||
} | ||
} |
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,75 @@ | ||
<?php | ||
|
||
namespace Laravolt\Acl\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Support\Facades\DB; | ||
use Laravolt\Acl\Models\Permission; | ||
|
||
class SyncPermission extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'acl:sync-permission {--clear}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Synchronize permission table and config file'; | ||
|
||
protected $config; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @param Repository $config | ||
*/ | ||
public function __construct(Repository $config) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$this->info('Synchronize Permissions Entries'); | ||
|
||
if ($this->option('clear')) { | ||
DB::statement('SET FOREIGN_KEY_CHECKS = 0;'); | ||
DB::table(with(new Permission)->getTable())->truncate(); | ||
} | ||
|
||
$enumClass = $this->config->get('acl.permission_enum'); | ||
$permissions = $enumClass::toArray(); | ||
|
||
$items = collect(); | ||
foreach ($permissions as $name) { | ||
$permission = Permission::firstOrNew(['name' => $name]); | ||
$status = 'No Change'; | ||
|
||
if (!$permission->exists) { | ||
$permission->save(); | ||
$status = 'New'; | ||
} | ||
|
||
$items->push(['id' => $permission->getKey(), 'name' => $name, 'status' => $status]); | ||
} | ||
|
||
$items = $items->sortBy('id'); | ||
|
||
$this->table(['ID', 'Name', 'Status'], $items); | ||
|
||
} | ||
} |
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,15 @@ | ||
<?php | ||
namespace Laravolt\Acl\Contracts; | ||
|
||
interface HasRoleAndPermission | ||
{ | ||
public function roles(); | ||
|
||
public function hasRole($role); | ||
|
||
public function assignRole($role); | ||
|
||
public function revokeRole($role); | ||
|
||
public function hasPermission($permission); | ||
} |
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,10 @@ | ||
<?php | ||
namespace Laravolt\Acl\Enum; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
class Permission extends Enum | ||
{ | ||
//const MANAGE_USER = 'manage-user'; | ||
//const MANAGE_ROLE = 'manage-role'; | ||
} |
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,13 @@ | ||
<?php | ||
namespace Laravolt\Acl; | ||
|
||
class Facade extends \Illuminate\Support\Facades\Facade | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'acl'; | ||
} | ||
} |
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 | ||
|
||
namespace Laravolt\Acl\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Permission extends Model | ||
{ | ||
protected $table = 'acl_permissions'; | ||
|
||
protected $fillable = ['name']; | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace Laravolt\Acl\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Role extends Model | ||
{ | ||
protected $table = 'acl_roles'; | ||
|
||
protected $fillable = ['name', 'description']; | ||
|
||
public function permissions() | ||
{ | ||
return $this->belongsToMany(Permission::class, 'acl_permission_role'); | ||
} | ||
|
||
public function users() | ||
{ | ||
return $this->belongsToMany(config('auth.model'), 'acl_role_user'); | ||
} | ||
|
||
public function addPermission($permission) | ||
{ | ||
if (is_string($permission)) { | ||
$permission = Permission::where('name', $permission)->first(); | ||
} | ||
|
||
return $this->permissions()->attach($permission); | ||
} | ||
|
||
public function removePermission($permission) | ||
{ | ||
if (is_string($permission)) { | ||
$permission = Permission::where('name', $permission)->first(); | ||
} | ||
|
||
return $this->permissions()->detach($permission); | ||
} | ||
|
||
public function syncPermission(array $permissions) | ||
{ | ||
$ids = collect($permissions)->filter(function ($id) { | ||
return $id > 0; | ||
}); | ||
|
||
return $this->permissions()->sync($ids->toArray()); | ||
} | ||
|
||
} |
Oops, something went wrong.