Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 48399e4

Browse files
committed
ACL: Setup ACL
1 parent a7968f2 commit 48399e4

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Manage;
4+
5+
use Illuminate\Http\Request;
6+
use App\Http\Controllers\Controller;
7+
8+
class AclController extends Controller
9+
{
10+
public function __invoke(Request $request)
11+
{
12+
switch ($request->type) {
13+
case 'create':
14+
$permissions = [
15+
$request->permission . '_create',
16+
$request->permission . '_store',
17+
];
18+
break;
19+
case 'view':
20+
$permissions = [
21+
$request->permission . '_index',
22+
$request->permission . '_show',
23+
];
24+
break;
25+
case 'update':
26+
$permissions = [
27+
$request->permission . '_edit',
28+
$request->permission . '_update',
29+
];
30+
break;
31+
case 'destroy':
32+
$permissions = [
33+
$request->permission . '_destroy',
34+
];
35+
break;
36+
}
37+
38+
$role = role($request->role);
39+
40+
foreach ($permissions as $permission) {
41+
if($request->revoke) {
42+
$role->revokePermissionTo($permission);
43+
} else {
44+
$role->givePermissionTo($permission);
45+
}
46+
}
47+
}
48+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Manage;
4+
5+
use Illuminate\Http\Request;
6+
use App\Http\Controllers\Controller;
7+
8+
class AclController extends Controller
9+
{
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
16+
{
17+
return view('manage.acl.index');
18+
}
19+
20+
/**
21+
* Show the form for creating a new resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function create()
26+
{
27+
//
28+
}
29+
30+
/**
31+
* Store a newly created resource in storage.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @return \Illuminate\Http\Response
35+
*/
36+
public function store(Request $request)
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Display the specified resource.
43+
*
44+
* @param int $id
45+
* @return \Illuminate\Http\Response
46+
*/
47+
public function show($id)
48+
{
49+
//
50+
}
51+
52+
/**
53+
* Show the form for editing the specified resource.
54+
*
55+
* @param int $id
56+
* @return \Illuminate\Http\Response
57+
*/
58+
public function edit($id)
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Update the specified resource in storage.
65+
*
66+
* @param \Illuminate\Http\Request $request
67+
* @param int $id
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function update(Request $request, $id)
71+
{
72+
//
73+
}
74+
75+
/**
76+
* Remove the specified resource from storage.
77+
*
78+
* @param int $id
79+
* @return \Illuminate\Http\Response
80+
*/
81+
public function destroy($id)
82+
{
83+
//
84+
}
85+
}

resources/views/components/navigations/nav-menus.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
@can('user_index')
2222
<a href="{{ route('manage.users.index') }}" class="dropdown-item ">Users</a>
2323
@endcan
24+
25+
@can('acl_index')
26+
<a href="{{ route('manage.acl.index') }}" class="dropdown-item ">ACL</a>
27+
@endcan
2428
</div>
2529
</li>
2630
<li class="nav-item dropdown">
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
@extends('layouts.admin')
2+
3+
@push('scripts')
4+
<script>
5+
jQuery(document).ready(function($) {
6+
$(document).on('change', '.acl-action-create', function(event) {
7+
event.preventDefault();
8+
var data = $(this).data();
9+
data.revoke = !this.checked;
10+
axios.put(route('api.manage.acl.update'), data)
11+
.then(function(response) {
12+
console.log(response);
13+
});
14+
});
15+
16+
$(document).on('change', '.acl-action-view', function(event) {
17+
event.preventDefault();
18+
var data = $(this).data();
19+
data.revoke = !this.checked;
20+
axios.put(route('api.manage.acl.update'), data)
21+
.then(function(response) {
22+
console.log(response);
23+
});
24+
});
25+
26+
$(document).on('change', '.acl-action-update', function(event) {
27+
event.preventDefault();
28+
var data = $(this).data();
29+
data.revoke = !this.checked;
30+
axios.put(route('api.manage.acl.update'), data)
31+
.then(function(response) {
32+
console.log(response);
33+
});
34+
});
35+
36+
$(document).on('change', '.acl-action-destroy', function(event) {
37+
event.preventDefault();
38+
var data = $(this).data();
39+
data.revoke = !this.checked;
40+
axios.put(route('api.manage.acl.update'), data)
41+
.then(function(response) {
42+
console.log(response);
43+
});
44+
});
45+
});
46+
</script>
47+
@endpush
48+
49+
@section('content')
50+
<div class="row justify-content-center">
51+
<div class="col">
52+
@component('components.card')
53+
@slot('card_title')
54+
Manage Access Control Level
55+
@endslot
56+
@slot('card_body')
57+
@component('components.table', ['table_id' => 'acl-table'])
58+
@slot('thead')
59+
<tr>
60+
<th>Permission / Role</th>
61+
@foreach(config('acl.roles') as $role)
62+
<th>
63+
{{ title_case($role) }}
64+
</th>
65+
@endforeach
66+
</tr>
67+
@endslot
68+
69+
@slot('tbody')
70+
@foreach(config('acl.permissions') as $permission => $roles)
71+
<tr>
72+
<td>{{ title_case($permission) }}</td>
73+
@foreach(config('acl.roles') as $role)
74+
<td>
75+
@include('components.forms.checkbox', [
76+
'name' => 'acl['.$role.']['.$permission.'_create]',
77+
'id' => 'acl-'.$role.'-'.$permission.'-create',
78+
'label' => 'Create',
79+
'class' => 'acl-action-create',
80+
'data' => [
81+
'role' => $role,
82+
'permission' => $permission,
83+
'type' => 'create',
84+
],
85+
'checked' => role($role)->hasPermissionTo($permission.'_create') ? true : false,
86+
])
87+
88+
@include('components.forms.checkbox', [
89+
'name' => 'acl['.$role.']['.$permission.'_view]',
90+
'id' => 'acl-'.$role.'-'.$permission.'-view',
91+
'label' => 'View',
92+
'class' => 'acl-action-view',
93+
'data' => [
94+
'role' => $role,
95+
'permission' => $permission,
96+
'type' => 'view',
97+
],
98+
'checked' => role($role)->hasPermissionTo($permission.'_index') ? true : false,
99+
])
100+
101+
@include('components.forms.checkbox', [
102+
'name' => 'acl['.$role.']['.$permission.'_update]',
103+
'id' => 'acl-'.$role.'-'.$permission.'-update',
104+
'label' => 'Update',
105+
'class' => 'acl-action-update',
106+
'data' => [
107+
'role' => $role,
108+
'permission' => $permission,
109+
'type' => 'update',
110+
],
111+
'checked' => role($role)->hasPermissionTo($permission.'_update') ? true : false,
112+
])
113+
114+
@include('components.forms.checkbox', [
115+
'name' => 'acl['.$role.']['.$permission.'_destroy]',
116+
'id' => 'acl-'.$role.'-'.$permission.'-destroy',
117+
'label' => 'Destroy',
118+
'class' => 'acl-action-destroy',
119+
'data' => [
120+
'role' => $role,
121+
'permission' => $permission,
122+
'type' => 'destroy',
123+
],
124+
'checked' => role($role)->hasPermissionTo($permission.'_destroy') ? true : false,
125+
])
126+
</td>
127+
@endforeach
128+
</tr>
129+
@endforeach
130+
@endslot
131+
@endcomponent
132+
@endslot
133+
@endcomponent
134+
</div>
135+
</div>
136+
@endsection

routes/api/manage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
'as' => 'manage.',
1111
], function () {
1212
Route::resource('users', 'UserController')->except('create', 'edit');
13+
Route::put('acl', 'AclController')->name('acl.update');
1314
});

routes/breadcrumbs/__.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
// Home
44
Breadcrumbs::register('home', function ($breadcrumbs) {
55
$breadcrumbs->push(__('Home'), route('home'));
6+
});
7+
8+
// Home > Manage ACL
9+
Breadcrumbs::register('manage.acl.index', function ($breadcrumbs) {
10+
$breadcrumbs->parent('home');
11+
$breadcrumbs->push(__('Manage ACL'), route('manage.acl.index'));
612
});

routes/web/manage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
'as' => 'manage.',
88
], function () {
99
Route::resource('users', 'UserController')->except('store', 'update', 'destroy');
10+
Route::resource('acl', 'AclController')->except('store', 'update', 'destroy');
1011
});

0 commit comments

Comments
 (0)