Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Dec 22, 2015
0 parents commit 53728e3
Show file tree
Hide file tree
Showing 15 changed files with 575 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# laravolt/acl
40 changes: 40 additions & 0 deletions composer.json
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": "uyab.exe@gmail.com",
"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"
}
}
10 changes: 10 additions & 0 deletions config/config.php
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 database/migrations/2015_10_19_044502_create_acl_roles_table.php
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');
}
}
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');
}
}
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');
}
}
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');
}
}
75 changes: 75 additions & 0 deletions src/Commands/SyncPermission.php
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);

}
}
15 changes: 15 additions & 0 deletions src/Contracts/HasRoleAndPermission.php
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);
}
10 changes: 10 additions & 0 deletions src/Enum/Permission.php
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';
}
13 changes: 13 additions & 0 deletions src/Facade.php
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';
}
}
12 changes: 12 additions & 0 deletions src/Models/Permission.php
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'];
}
50 changes: 50 additions & 0 deletions src/Models/Role.php
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());
}

}
Loading

0 comments on commit 53728e3

Please sign in to comment.