Follow the basic laravel ACL Go
Run the following command to create advanced permission table script
d/xampp/htdocs/projectname> php artisan make:migration create_advanced_permission_role_table
then go to the location database/migration/ cut file create_advanced_permission_role_table.php to migration/customacl/create_advanced_permission_role_table.php
then go to the location database/migration/customacl/ and edit create_advanced_permission_role_table.php file and replace all the following code
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAutheticationSetupTable extends Migration {
public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->string('controller_name')->nullable();
$table->string('method_type')->nullable();
$table->string('method_name')->nullable();
$table->string('relative_path')->nullable();
$table->string('route_path')->nullable();
$table->tinyInteger('active')->default(1);
$table->bigInteger('create_by')->unsigned()->nullable();
$table->bigInteger('update_by')->unsigned()->nullable();
$table->dateTime('create_date')->nullable();
$table->dateTime('update_date')->nullable();
$table->foreign('create_by')->references('id')->on('users')->onDelate('cascade');
$table->foreign('update_by')->references('id')->on('users')->onDelate('cascade');
});
Schema::create('permission_roles', function(Blueprint $table){
$table->bigInteger('permission_id')->unsigned();
$table->bigInteger('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelate('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelate('cascade');
$table->primary(['permission_id', 'role_id']);
});
}
public function down()
{
Schema::drop('permissions');
Schema::drop('permission_roles');
}
}
Then run the following command to create table in the database
d/xampp/htdocs/projectname> php artisan migrate d/xampp/htdocs/projectname> php artisan config:cache
To create model write the following command one after another
d/xampp/htdocs/projectname> php artisan make:model App\Models\Auth\Permission.php
Edit the model file app/Models/Auth/Permission.php and replace all the following code to maintain relation to other tables
<?php namespace App\Models\Auth;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model {
protected $table = 'permissions';
public $timestamps = false;
protected $guarded = ['id'];
public function roles()
{
return $this->belongsToMany('App\Models\Auth\Role', 'permission_roles', 'permission_id', 'role_id');
}
public function users()
{
return $this->belongsTo('App\User','create_by','id');
}
public function updateBy()
{
return $this->belongsTo('App\User','update_by','id');
}
}
Add PermissionController in the location App\Http\Controllers\Auth\ using the following command
d/xampp/htdocs/projectname> php artisan make:controller Auth\PermissionController
After creating file replace all the following code
<?php namespace App\Http\Controllers\Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Models\Auth\Role;
use App\Models\Auth\Permission;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Input;
class PermissionController extends Controller {
public function index(Request $request)
{
$ct;
$rt;
$permissions;
$totalCount;
$pageNo = 10;
if (Input::has('ct') && Input::has('rt')) {
//$ct = Input::get('ct');
$ct = explode('\\',Input::get('ct'))[1];
$rt =Input::get('rt');
$permissions = Permission::orderBy('id','DESC')
->where('controller_name','LIKE',"%$ct%")
->where('route_path','LIKE',"%$rt%")
->paginate($pageNo)
->setPath(route('perm/index',['ct'=>$ct,'rt'=>$rt]));
$totalCount = Permission::orderBy('id','DESC')
->where('controller_name','LIKE',"%$ct%")
->where('route_path','LIKE',"%$rt%")->count();
}else if(Input::has('ct')){
$ct = explode('\\',Input::get('ct'))[1];
$permissions = Permission::orderBy('id','DESC')
->where('controller_name','LIKE',"%$ct%")
->paginate($pageNo)
->setPath(route('perm/index',['ct'=>$ct]));
$totalCount = Permission::orderBy('id','DESC')
->where('controller_name','LIKE',"%$ct%")->count();
}else if(Input::has('rt')){
$rt = trim(Input::get('rt'));
$permissions = Permission::orderBy('id','DESC')
->where('route_path','LIKE',"%$rt%")
->paginate($pageNo)
->setPath(route('perm/index',['rt'=>$rt]));
$totalCount = Permission::orderBy('id','DESC')
->where('route_path','LIKE',"%$rt%")->count();
}else{
$permissions = Permission::orderBy('id','DESC')
->paginate($pageNo)
->setPath(route('perm/index'));
$totalCount = Permission::orderBy('id','DESC')->count();
}
//$controllerList =Permission::all()->lists('controller_name','id');
//$controllerList =DB::select("select distinct id,controller_name from permissions");
$controllerList = Permission::select('controller_name','controller_name')->groupBy('controller_name')->lists('controller_name','controller_name');
return view('auth.perm_index',compact('permissions','totalCount','controllerList')) ->with('i', ($request->input('page', 1) - 1) * 5);
}
public function create()
{
$roleList =Role::all()->lists('role_name','id');
$roleIdList = array();
return view('auth.perm_create',['roleList'=>$roleList,'permissionInstance'=>new Permission(),'roleIdList'=>$roleIdList]);
}
public function store(Request $request)
{
$insert =null;
$sysRole = null;
$permission = null;
$i=0;
while($request->input('perm_'.$i.'_controller_name') && $request->input('perm_'.$i.'_relative_path')){
$this->validate($request, [
'perm_'.$i.'_controller_name'=> 'regex:/^[A-Za-z-_\\\]{5,100}/',
'perm_'.$i.'_method_type'=> 'regex:/^[A-Za-z-_]{2,20}/',
'perm_'.$i.'_method_name'=> 'regex:/^[A-Za-z-_]{2,100}/',
'perm_'.$i.'_relative_path'=> 'regex:/^[A-Za-z-_\/\#]{1,50}/',
'perm_'.$i.'_route_path' => 'regex:/^[A-Za-z-_\/\#]{1,50}/',
'perm_'.$i.'_active'=> 'regex:/^[0-9]{0,2}/'
]);
if ($request->input('perm_'.$i.'_method_type') && $request->input('perm_'.$i.'_role_id') && $request->input('perm_'.$i.'_new') == "true" && $request->input('perm_'.$i.'_deleted') == "false") {
$permission = new Permission();
$permission->controller_name = $request->input('perm_'.$i.'_controller_name');
$permission->method_type= $request->input('perm_'.$i.'_method_type');
$permission->method_name= $request->input('perm_'.$i.'_method_name');
$permission->relative_path= $request->input('perm_'.$i.'_relative_path');
$permission->route_path= $request->input('perm_'.$i.'_route_path');
$permission->active = $request->input('perm_'.$i.'_active');
$permission->create_date = Carbon::now();
$permission->create_by = Auth::user()->id ;
$insert = $permission->save();
if($request->input('perm_'.$i.'_role_id')[0]){
$permission->roles()->sync($request->input('perm_'.$i.'_role_id'));
}
} // end if
$i++;
}
if($insert){
return redirect()->route('perm/index')->with('success','Data insert successfull');
}else{
//$permission::delete($insert->id);
return redirect()->route('perm/create')->with('success','Data insert fail');
}
}
public function show($id)
{
//
}
public function edit($id)
{
$roleIdList = array();
$permissions = Permission::find($id);
foreach($permissions->roles()->get() as $role){
array_push($roleIdList,$role->id);
}
$roleList =Role::all()->lists('role_name','id');
return view('auth.perm_edit', compact('permissions','id','roleList','roleIdList'));
}
public function update(Request $request,$id)
{
$insert =null;
$sysRole = null;
$permission = null;
$i=0;
while($request->input('perm_'.$i.'_controller_name') && $request->input('perm_'.$i.'_relative_path')){
$this->validate($request, [
'perm_'.$i.'_controller_name'=> 'regex:/^[A-Za-z-_\\\]{5,100}/',
'perm_'.$i.'_method_type'=> 'regex:/^[A-Za-z-_]{2,20}/',
'perm_'.$i.'_method_name'=> 'regex:/^[A-Za-z-_]{2,100}/',
'perm_'.$i.'_relative_path'=> 'regex:/^[A-Za-z-_\/\#]{1,50}/',
'perm_'.$i.'_route_path' => 'regex:/^[A-Za-z-_\/\#]{1,50}/',
'perm_'.$i.'_active'=> 'regex:/^[0-9]{0,2}/'
]);
if ($request->input('perm_'.$i.'_method_type') && $request->input('perm_'.$i.'_role_id') && $request->input('perm_'.$i.'_new') == "false" && $request->input('perm_'.$i.'_deleted') == "false") {
$permission = Permission::find($id);
$permission->controller_name = $request->input('perm_'.$i.'_controller_name');
$permission->method_type= $request->input('perm_'.$i.'_method_type');
$permission->method_name= $request->input('perm_'.$i.'_method_name');
$permission->relative_path= $request->input('perm_'.$i.'_relative_path');
$permission->route_path= $request->input('perm_'.$i.'_route_path');
$permission->active = $request->input('perm_'.$i.'_active');
$permission->create_date = Carbon::now();
$permission->create_by = Auth::user()->id ;
$insert = $permission->save();
if( $request->input('perm_'.$i.'_role_id')[0]){
$permission->roles()->sync($request->input('perm_'.$i.'_role_id'));
}else{
$permission->roles()->detach();
}
} // end if
$i++;
}
if($insert){
return redirect()->route('perm/index')->with('success','Data update successfull');
}else{
return redirect()->route('perm/create')->with('success','Data update fail');
}
}
public function destroy($id)
{
$permissions = Permission::find($id);
if($permissions->roles()->detach()){
$permissions->roles()->detach();
$permissions->delete();
return redirect()->route('perm/index')->with('success','Data deleted successfully');
}else{
$permissions->delete();
return redirect()->route('perm/index')->with('fail','Data delation fail');
}
}
}
Add the following code in the location resources/views/auth/user_create.blade.php
@extends('layouts.admin') @section('content') <div class="content contentPadding contentBg"> <div class="container-fluid"> <div class="row"> @if(session('success')) <div class="alert alert-success alert-dismissable"> <p>{{{ session('success') }}}</p> </div> @endif @if(session('fail')) <div class="alert alert-success alert-dismissable"> <p>{{{ session('fail') }}}</p> </div> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div class="panel panel-success"> <div class="panel-heading">New Permission</div> <div class="panel-body"> <form class="form-horizontal" role="form" method="POST" action="{{route('perm/store/ct/uk',['ct'=>csrf_token()])}}"> <input type="hidden" name="_token" value="{{ csrf_token()}}"> <table id="detailList" class="width100"> @include('auth.perm_dtl',['i'=>0,'hidden'=>false,'perm'=>$permissionInstance]) </table> <div class="form-group"> <div class="col-md-12" align="center"> <input type="button" class="ym-primary btn btn-warning btn-sm" style="margin: 5px;" value="Add More" onclick="addChild();"/> </div> </div> <div class="form-group"> <div class="col-md-12" align="center"> <button type="submit" class="btn btn-primary"> Submit </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection
Add the following code in the location resources/views/auth/perm_dtl.blade.php
<script type="text/javascript"> var childCount = 1; $(document).on('click',".delButton",function(){ var prnt = $(this).parents(".detail-div"); var delInput = prnt.find("input[id$=deleted]"); delInput.attr('value', 'true'); prnt.hide(); }); function addChild() { var clone = $("#detail0").clone(); var htmlId = 'perm_' + childCount + '_'; clone.find("input[id$=id]") .attr('id', htmlId + 'id') .attr('name', htmlId + 'id'); clone.find("input[id$=deleted]") .attr('id', htmlId + 'deleted') .attr('name', htmlId + 'deleted'); clone.find("input[id$=new]") .attr('id', htmlId + 'new') .attr('name', htmlId + 'new') .attr('value', 'true'); clone.find("input[id$=controller_name]").attr('id', htmlId + 'controller_name').attr('name', htmlId + 'controller_name'); clone.find("select[id$=method_type]").attr('id', htmlId + 'method_type').attr('name', htmlId + 'method_type'); clone.find("input[id$=method_name]").attr('id', htmlId + 'method_name').attr('name', htmlId + 'method_name'); clone.find("input[id$=relative_path]").attr('id', htmlId + 'method_name').attr('name', htmlId + 'relative_path'); clone.find("input[id$=route_path]").attr('id', htmlId + 'route_path').attr('name', htmlId + 'route_path'); clone.find("select[id$=active]").attr('id', htmlId + 'active').attr('name', htmlId + 'active'); clone.find("select[id$=role_id]").attr('id', htmlId + 'role_id').attr('name', htmlId + 'role_id[]'); clone.attr('id', 'detail' + childCount); clone.attr('name', 'tr[' + childCount+']'); $("#detailList").append(clone); clone.show(); childCount++; } </script> <tr id="detail{{$i}}" name="tr[{{$i}}]" class="detail-div" <?php if($hidden){ ?> style="display:none;"<?php }?>> <input type="hidden" id="perm_{{$i}}_id" name="perm_{{$i}}_id" value="{{$perm->id}}"/> <input type="hidden" id="perm_{{$i}}_deleted" name="perm_{{$i}}_deleted" value='false'/> <input type="hidden" id="perm_{{$i}}_new" name="perm_{{$i}}_new" value="{{$perm->id == null ? 'true' : 'false'}}"/> <td> <div class="form-group col-md-4"> <label>Controller Name</label> <div> <input type="text" pattern="^[A-Za-z_-\\]{5,100}" class="form-control" id="perm_{{$i}}_controller_name" name="perm_{{$i}}_controller_name" value="{{$perm->controller_name}}"> </div> </div> <div class="form-group col-md-4"> <label>Method Type</label> <div> {!! Form::select('perm_'.$i.'_method_type', array('get' => 'get','post' => 'post','put' => 'put','delete' => 'delete'),$perm->method_type,['class' => 'form-control margin','id'=>'perm_'.$i.'_method_type']);!!} </div> </div> <div class="form-group col-md-4"> <label>Method Name</label> <div> <input type="text" pattern="^[A-Za-z_-]{2,100}" class="form-control" id="perm_{{$i}}_method_name" name="perm_{{$i}}_method_name" value="{{ $perm->method_name }}"> </div> </div> <div class="form-group col-md-4"> <label>Relative Path</label> <div> <input type="text" pattern="^[A-Za-z_-\/\#]{1,50}" class="form-control" id="perm_{{$i}}_relative_path" name="perm_{{$i}}_relative_path" value="{{ $perm->relative_path }}"> </div> </div> <div class="form-group col-md-4"> <label>Route Path</label> <div> <input type="text" pattern="^[A-Za-z_-\/\#]{1,50}" class="form-control" id="perm_{{$i}}_route_path" name="perm_{{$i}}_route_path" value="{{ $perm->route_path }}"> </div> </div> <div class="form-group col-md-4"> <label>Is Publish</label> <div> {!! Form::select('perm_'.$i.'_active',array('1' => 'Publish','0' => 'Unpublish'),$perm->active,['class' => 'form-control margin','id'=>'perm_'.$i.'_active']);!!} </div> </div> <div class="form-group col-md-4 cls"> <label>Permission Role</label> <div> {!! Form::select("perm_".$i."_role_id[]",[null=>'Select One']+$roleList, (($roleIdList)? $roleIdList:null), ['multiple'=>true,'class' => 'form-control margin','id'=>"perm_".$i."_role_id"]) !!} </div> </div> </td> <td> <span class='delButton' id='delButton' name='results_{{$i}}_delButton'><img src="{{ asset('public/images/database_delete.png') }}"/></span> </td> </tr>
Add the following code in the location resources/views/auth/perm_index.blade.php
@extends('layouts.admin') @section('content') <div class="content contentPadding contentBg"> <div class="container-fluid"> <div class="row"> @if(session('success')) <div class="alert alert-success alert-dismissable"> <p>{{{ session('success') }}}</p> </div> @endif @if(session('fail')) <div class="alert alert-success alert-dismissable"> <p>{{{ session('fail') }}}</p> </div> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div class="panel panel-success"> <div class="panel-heading"> Permission List <a href="{{route('perm/create')}}" class="btn pull-right btn-xs btn-warning"> <span class="glyphicon glyphicon-plus-sign"></a> </div> <div class="panel-body"> <div class="search"> <div class="panel panel-info"> <div class="panel-body"> <form action="{{URL::to('perm/index')}}" method="GET"> <div class="form-group col-md-4"> <label>Controller Name</label> <div> {!! Form::select('ct',[null=>'Select One']+$controllerList,null,['class'=>'form-control']);!!} </div> </div> <div class="form-group col-md-4"> <label>Route Name</label> <div> <input type="text" class="form-control" name="rt" value="{{ old('rt') }}"> </div> </div> <div class="form-group"> <div class="col-md-12" align="center"> <button type="submit" class="btn btn-primary"> Search </button> </div> </div> </form> </div> </div> </div> <div class="table-responsive cls"> <table class="table table-hover table-bordered"> <thead> <tr> <th>Sl No.</th> <th>Action</th> <th>Relative Path</th> <th>Route Path</th> <th>Method Name</th> <th>Method Type</th> <th>Controller Name</th> <th>Permission Role</th> <th>Is Active</th> <th>Create By</th> <th>Update By</th> <th>Create Date</th> <th>Update Date</th> <th>Action</th> </tr> </thead> <tbody> <?php $i=0;?> @foreach ($permissions as $key => $post) <tr> <td>{{$i+1}}</td> <td> <ul class="list"> <li><a href="{{route('perm/edit/id',['id'=>$post->id])}}" class="btn btn-xs btn-primary"><span class="glyphicon glyphicon-edit"></span> </a></li> <li><a href="{{route('perm/delete/id',['id'=>$post->id])}}" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-remove-sign"></span></a></li> </ul> </td> <td >{{$post->relative_path}}</td> <td >{{$post->route_path}}</td> <td >{{$post->method_name}}</td> <td >{{$post->method_type}}</td> <td >{{$post->controller_name}}</td> <td > @foreach($post->roles()->get() as $role) {{$role->role_name}} @endforeach </td> <td >{{$post->active}}</td> <td > @if($post->create_by) {{$post->users->user_name}} @endif </td> <td > @if($post->update_by) {{$post->updateBy->user_name}} @endif </td> <td > @if($post->create_date) {{\Carbon\Carbon::parse($post->create_date)->format('d/m/Y h:i A')}} @endif </td> <td > @if($post->update_date) {{\Carbon\Carbon::parse($post->update_date)->format('d/m/Y h:i A')}} @endif </td> <td > <ul class="list"> <li><a href="{{route('perm/edit/id',['id'=>$post->id])}}" class="btn btn-xs btn-primary"><span class="glyphicon glyphicon-edit"></span> </a></li> <li><a href="{{route('perm/delete/id',['id'=>$post->id])}}" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-remove-sign"></span></a></li> </ul> </td> </tr> <?php $i++; ?> @endforeach </tbody> </table> </div> <div>show: {{$permissions->count()}} of total {{$totalCount}}</div> {!! $permissions->render() !!} </div> </div> </div> </div> </div> @endsection
Add the following code in the location resources/views/auth/perm_edit.blade.php
@extends('layouts.admin') @section('content') <div class="content contentPadding contentBg"> <div class="container-fluid"> <div class="row"> @if(session('success')) <div class="alert alert-success alert-dismissable"> <p>{{{ session('success') }}}</p> </div> @endif @if(session('fail')) <div class="alert alert-success alert-dismissable"> <p>{{{ session('fail') }}}</p> </div> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div class="panel panel-success"> <div class="panel-heading">Update Permission</div> <div class="panel-body"> <form class="form-horizontal" role="form" method="POST" action="{{route('perm/update/id',['id'=>$permissions->id])}}"> <input type="hidden" name="_token" value="{{ csrf_token()}}"> <table id="detailList" class="width100"> @include('auth.perm_dtl',['i'=>0,'hidden'=>false,'perm'=>$permissions]) </table> <div class="form-group"> <div class="col-md-12" align="center"> <input type="button" class="ym-primary btn btn-warning btn-sm" style="margin: 5px;" value="Add More" onclick="addChild();"/> </div> </div> <div class="form-group"> <div class="col-md-12" align="center"> <button type="submit" class="btn btn-primary"> Update </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection
Create a file RouteServices.php in the location app/Services/Menu/ and add the following code
<?php namespace App\Services\Menu; use Auth; use App\Models\Auth\Role; use App\Models\Auth\Permission; use DB; class RouteServices{ public static function makePermission(){ $permissions = DB::table('permissions') ->select(DB::raw('permissions.id,permissions.controller_name,permissions.method_type,permissions.method_name,permissions.relative_path,permissions.route_path,permissions.active')) ->whereNotIn('controller_name',array('Web\HomeController','Web\DashboardController','Auth\AuthController','Auth\PasswordController')) ->get(); return $permissions; } public static function getPermissionRole($permission_id){ $role_name_array = array(); $query ="select role_name from roles r,permission_roles pr \n"; $query .="where r.id=pr.role_id \n"; $query .="and pr.permission_id='".$permission_id."' \n"; $roles = DB::select($query); foreach($roles as $role){ array_push($role_name_array,$role->role_name); } return $role_name_array ; } } ?>
Add the following code in the file Http/routes.php
Route::get('perm/create',['uses' => 'Auth\PermissionController@create', 'as' => 'perm/create']); Route::post('perm/store/{ct}/{uk?}',['uses' => 'Auth\PermissionController@store', 'as' => 'perm/store/ct/uk']); Route::get('perm/index',['uses' => 'Auth\PermissionController@index', 'as' => 'perm/index']); Route::get('perm/edit/{id}',['uses' => 'Auth\PermissionController@edit', 'as' => 'perm/edit/id']); Route::post('perm/update/{id}',['uses' => 'Auth\PermissionController@update', 'as' => 'perm/update/id']); Route::get('perm/delete/{id}',['uses' => 'Auth\PermissionController@destroy', 'as' => 'perm/delete/id']);
Then brouse the url:http://localhost/perm/create input the route Route::get('perm/create',['uses' => 'Auth\PermissionController@create', 'as' => 'perm/create']); as follows fields
post type: get,controller:Auth\PermissionController,method:create,url path:perm/create,route path:perm/create,is published:published,role:admin,user input... sequentially
Now replace route code of step-7 as follows
$routeServices = new \App\Services\Menu\RouteServices(); foreach($routeServices::makePermission() as $perm){ if($perm->method_type=='get' && $routeServices::getPermissionRole($perm->id)){ Route::get($perm->relative_path,['middleware' => ['auth','roles'],'roles' => $routeServices::getPermissionRole($perm->id),'uses' =>$perm->controller_name.'@'.$perm->method_name, 'as' => $perm->route_path]); }else if($perm->method_type=='get' && ! $routeServices::getPermissionRole($perm->id)){ Route::get($perm->relative_path,['uses' =>$perm->controller_name.'@'.$perm->method_name, 'as' => $perm->route_path]); }else if($perm->method_type=='post' && $routeServices::getPermissionRole($perm->id)){ Route::post($perm->relative_path,['middleware' => ['auth','roles'],'roles' => $routeServices::getPermissionRole($perm->id),'uses' =>$perm->controller_name.'@'.$perm->method_name, 'as' => $perm->route_path]); }else if($perm->method_type=='post' && ! $routeServices::getPermissionRole($perm->id)){ Route::post($perm->relative_path,['uses' =>$perm->controller_name.'@'.$perm->method_name, 'as' => $perm->route_path]); } }
Again brouse the url:http://localhost/perm/create. if work fine, then acl is ok
Total : 26654
Today :3
Today Visit Country :