Skip to content

Commit

Permalink
Update permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ajifatur committed Jul 21, 2024
1 parent 022687f commit 24f2609
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
18 changes: 17 additions & 1 deletion resources/views/admin/permission/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
<div class="row mb-3">
<label class="col-lg-2 col-md-3 col-form-label">Kode <span class="text-danger">*</span></label>
<div class="col-lg-10 col-md-9">
<input type="text" name="code" class="form-control form-control-sm {{ $errors->has('code') ? 'border-danger' : '' }}" value="{{ old('code') }}">
<select name="code" class="form-select form-select-sm {{ $errors->has('code') ? 'border-danger' : '' }}">
<option value="" disabled selected>--Pilih--</option>
@foreach($routes as $route)
@if($route != null)
<option value="{{ $route['actionName'] }}" {{ old('code') == $route['actionName'] ? 'selected' : '' }}>{{ $route['actionName'] }} - {{ $route['method'] }}</option>
@endif
@endforeach
</select>
@if($errors->has('code'))
<div class="small text-danger">{{ $errors->first('code') }}</div>
@endif
Expand All @@ -45,4 +52,13 @@
</div>
</div>

@endsection

@section('js')

<script>
// Select2
Spandiv.Select2("select[name=code]");
</script>

@endsection
18 changes: 17 additions & 1 deletion resources/views/admin/permission/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
<div class="row mb-3">
<label class="col-lg-2 col-md-3 col-form-label">Kode <span class="text-danger">*</span></label>
<div class="col-lg-10 col-md-9">
<input type="text" name="code" class="form-control form-control-sm {{ $errors->has('code') ? 'border-danger' : '' }}" value="{{ $permission->code }}">
<select name="code" class="form-select form-select-sm {{ $errors->has('code') ? 'border-danger' : '' }}">
<option value="" disabled selected>--Pilih--</option>
@foreach($routes as $route)
@if($route != null)
<option value="{{ $route['actionName'] }}" {{ $permission->code == $route['actionName'] ? 'selected' : '' }}>{{ $route['actionName'] }} - {{ $route['method'] }}</option>
@endif
@endforeach
</select>
@if($errors->has('code'))
<div class="small text-danger">{{ $errors->first('code') }}</div>
@endif
Expand All @@ -46,4 +53,13 @@
</div>
</div>

@endsection

@section('js')

<script>
// Select2
Spandiv.Select2("select[name=code]");
</script>

@endsection
2 changes: 2 additions & 0 deletions resources/views/admin/permission/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<tr>
<th width="30"><input type="checkbox" class="form-check-input checkbox-all"></th>
<th>Akses</th>
<th width="80">Status</th>
@foreach($roles as $role)
<th width="50" class="small">
{{ $role->name }}
Expand All @@ -52,6 +53,7 @@
<br>
<span class="small text-muted">{{ $permission->code }}</span>
</td>
<td><span class="badge {{ $permission->isAvailable ? 'bg-success' : 'bg-danger' }}">{{ $permission->isAvailable ? 'Tersedia' : 'Tidak Tersedia' }}</span></td>
@foreach($roles as $role)
<td align="center">
<span class="d-none">{{ $permission->num_order }}</span>
Expand Down
52 changes: 50 additions & 2 deletions src/Http/Controllers/PermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Ajifatur\FaturHelper\Models\Permission;
Expand All @@ -28,6 +29,30 @@ public function index(Request $request)
else
$permissions = Permission::where('default','=',0)->orderBy('num_order','asc')->get();

// Get routes
$routes = collect(Route::getRoutes())->map(function($route) use($request) {
if(is_int(strpos($route->getActionName(), ($request->query('default') == 1 ? 'Ajifatur\FaturHelper\Http\Controllers' : 'App\Http\Controllers')))) {
return [
'actionName' => str_replace('@','::',$route->getActionName()),
];
}
});

// Push to codes
$codes = [];
foreach($routes as $route) {
if($route != null) {
if($request->query('default') == 1)
array_push($codes, substr($route['actionName'],1,strlen($route['actionName'])-1));
else
array_push($codes, $route['actionName']);
}
}

foreach($permissions as $key=>$permission) {
$permissions[$key]->isAvailable = in_array($permission->code, $codes);
}

// Get roles
$roles = Role::orderBy('num_order','asc')->get();

Expand All @@ -48,8 +73,20 @@ public function create()
// Check the access
has_access(__METHOD__, Auth::user()->role_id);

// Get routes
$routes = collect(Route::getRoutes())->map(function($route) {
if(is_int(strpos($route->getActionName(), 'App\Http\Controllers'))) {
return [
'method' => $route->methods()[0],
'actionName' => str_replace('@','::',$route->getActionName()),
];
}
});

// View
return view('faturhelper::admin/permission/create');
return view('faturhelper::admin/permission/create', [
'routes' => $routes
]);
}

/**
Expand Down Expand Up @@ -102,6 +139,16 @@ public function edit($id)
// Get the permission
$permission = Permission::findOrFail($id);

// Get routes
$routes = collect(Route::getRoutes())->map(function($route) {
if(is_int(strpos($route->getActionName(), 'App\Http\Controllers'))) {
return [
'method' => $route->methods()[0],
'actionName' => str_replace('@','::',$route->getActionName()),
];
}
});

// Check permission whether is default
if($permission->default === 1) {
// Redirect
Expand All @@ -110,7 +157,8 @@ public function edit($id)

// View
return view('faturhelper::admin/permission/edit', [
'permission' => $permission
'permission' => $permission,
'routes' => $routes
]);
}

Expand Down

0 comments on commit 24f2609

Please sign in to comment.