Skip to content

Commit 12f59c0

Browse files
committed
Final edits, ready for release.
1 parent 7b71a0b commit 12f59c0

File tree

15 files changed

+544
-21
lines changed

15 files changed

+544
-21
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Frontend Boilerplate for spatie permissions package
22

3-
Frontend bolierplate for spaties permissions package. This package gives you a start on managing roles and permissions.
3+
Frontend boilerplate for [spaties permissions package](https://github.com/spatie/laravel-permission). This package gives you a start on managing roles and permissions. This is a basic package and does not employ any design patterns nor does the package do any opinionated structure.
4+
5+
You are free edit the code as you wish once the package has been published.
46

57
## Installation
68

@@ -27,6 +29,9 @@ php artisan laravel-spatie-permissions-frontend tailwind
2729

2830
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
2931

32+
## Todo
33+
- Add tests
34+
3035
## Credits
3136

3237
- [James](https://github.com/webdevhayes)

src/LaravelSpatiePermissionsFrontendCommand.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class LaravelSpatiePermissionsFrontendCommand extends Command
1919
'roles/create.stub' => 'roles/create.blade.php',
2020
'roles/edit.stub' => 'roles/edit.blade.php',
2121
'roles/index.stub' => 'roles/index.blade.php',
22+
'permissions/create.stub' => 'permissions/create.blade.php',
23+
'permissions/edit.stub' => 'permissions/edit.blade.php',
24+
'permissions/index.stub' => 'permissions/index.blade.php',
2225
];
2326

2427
/**
@@ -54,6 +57,9 @@ protected function ensureDirectoriesExist()
5457
if (! is_dir($directory = $this->getViewPath('roles'))) {
5558
mkdir($directory, 0755, true);
5659
}
60+
if (! is_dir($directory = $this->getViewPath('permissions'))) {
61+
mkdir($directory, 0755, true);
62+
}
5763
}
5864

5965
/**
@@ -86,14 +92,23 @@ protected function exportBackend()
8692
{
8793
$this->callSilent('laravel-spatie-permissions-frontend:controllers');
8894

89-
$controller = app_path('Http/Controllers/RoleController.php');
95+
$roleController = app_path('Http/Controllers/RoleController.php');
96+
$permissionController = app_path('Http/Controllers/PermissionController.php');
9097

91-
if (file_exists($controller)) {
98+
if (file_exists($roleController)) {
9299
if ($this->confirm("The [RoleController.php] file already exists. Do you want to replace it?")) {
93-
file_put_contents($controller, $this->compileControllerStub());
100+
file_put_contents($roleController, $this->compileRoleControllerStub());
94101
}
95102
} else {
96-
file_put_contents($controller, $this->compileControllerStub());
103+
file_put_contents($roleController, $this->compileRoleControllerStub());
104+
}
105+
106+
if (file_exists($permissionController)) {
107+
if ($this->confirm("The [PermissionController.php] file already exists. Do you want to replace it?")) {
108+
file_put_contents($permissionController, $this->compilePermissionControllerStub());
109+
}
110+
} else {
111+
file_put_contents($permissionController, $this->compilePermissionControllerStub());
97112
}
98113

99114
file_put_contents(
@@ -105,11 +120,11 @@ protected function exportBackend()
105120

106121

107122
/**
108-
* Compiles the "HomeController" stub.
123+
* Compiles the "RoleController" stub.
109124
*
110125
* @return string
111126
*/
112-
protected function compileControllerStub()
127+
protected function compileRoleControllerStub()
113128
{
114129
return str_replace(
115130
'{{namespace}}',
@@ -118,6 +133,20 @@ protected function compileControllerStub()
118133
);
119134
}
120135

136+
/**
137+
* Compiles the "PermissionController" stub.
138+
*
139+
* @return string
140+
*/
141+
protected function compilePermissionControllerStub()
142+
{
143+
return str_replace(
144+
'{{namespace}}',
145+
$this->laravel->getNamespace(),
146+
file_get_contents(__DIR__.'/stubs/controllers/PermissionController.stub')
147+
);
148+
}
149+
121150
/**
122151
* Get full view path relative to the application's configured view path.
123152
*
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Contracts\Foundation\Application;
6+
use Illuminate\Contracts\Support\Renderable;
7+
use Illuminate\Http\RedirectResponse;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Http\Response;
10+
use Illuminate\Routing\Redirector;
11+
use Spatie\Permission\Models\Permission;
12+
use Spatie\Permission\Models\Role;
13+
14+
class PermissionController extends Controller
15+
{
16+
17+
/**
18+
* Show the application dashboard.
19+
*
20+
* @return Renderable
21+
*/
22+
public function index()
23+
{
24+
$permissions = Permission::paginate(10);
25+
26+
return view('permissions.index', compact('permissions'));
27+
}
28+
29+
/**
30+
* Show the form for creating a new resource.
31+
*
32+
* @return Response
33+
*/
34+
public function create()
35+
{
36+
return view('permissions.create');
37+
}
38+
39+
/**
40+
* Store a newly created resource in storage.
41+
*
42+
* @param Request $request
43+
* @return RedirectResponse
44+
*/
45+
public function store(Request $request)
46+
{
47+
$this->validatePermissionsForm($request);
48+
49+
$permission = Permission::create([
50+
'name' => $request->permission_name,
51+
'guard_name' => 'web'
52+
]);
53+
54+
return redirect()->route('permissions.index');
55+
}
56+
57+
/**
58+
* Show the form for editing the specified resource.
59+
*
60+
* @param Permission $permission
61+
* @return Response
62+
*/
63+
public function edit(Permission $permission)
64+
{
65+
return view('permissions.edit', compact('permission'));
66+
}
67+
68+
/**
69+
* Update the specified resource in storage.
70+
*
71+
* @param Request $request
72+
* @param Permission $permission
73+
* @return Application|RedirectResponse|Redirector
74+
*/
75+
public function update(Request $request, Permission $permission)
76+
{
77+
$this->validatePermissionsForm($request, $permission);
78+
79+
$permission->update([
80+
'name' => $request->permission_name,
81+
'guard_name' => 'web'
82+
]);
83+
84+
return redirect()->route('permissions.index');
85+
}
86+
87+
private function validatePermissionsForm(Request $request, Permission $permission = null)
88+
{
89+
$uniquePermission = $permission === null ? '' : ',' . $permission->id;
90+
return $request->validate([
91+
'permission_name' => 'required|string|max:255|unique:permissions,name' . $uniquePermission,
92+
]);
93+
}
94+
}

src/stubs/routes.stub

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ Route::get('roles', 'RoleController@index')->name('roles.index');
22
Route::get('roles/create', 'RoleController@create')->name('role.create');
33
Route::post('roles/create', 'RoleController@store')->name('role.store');
44
Route::get('roles/edit/{role}', 'RoleController@edit')->name('role.edit');
5-
Route::put('roles/edit/{role}', 'RoleController@update')->name('role.update');
5+
Route::put('roles/edit/{role}', 'RoleController@update')->name('role.update');
6+
7+
Route::get('permissions', 'PermissionController@index')->name('permissions.index');
8+
Route::get('permissions/create', 'PermissionController@create')->name('permission.create');
9+
Route::post('permissions/create', 'PermissionController@store')->name('permission.store');
10+
Route::get('permissions/edit/{permission}', 'PermissionController@edit')->name('permission.edit');
11+
Route::put('permissions/edit/{permission}', 'PermissionController@update')->name('permission.update');
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<!-- Required meta tags -->
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
8+
<!-- Bootstrap CSS -->
9+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
10+
11+
<title>Create Permission</title>
12+
</head>
13+
<body>
14+
<div class="container">
15+
<div class="row">
16+
<div class="col-12 my-5">
17+
<form method="POST" action="{{ route('permission.create') }}">
18+
@csrf
19+
<div class="form-group">
20+
<label for="permission_name">Permission name</label>
21+
<input type="text" class="form-control @error('permission_name') is-invalid @enderror" name="permission_name" id="permission_name" aria-describedby="role_name" placeholder="Enter permission name" value="{{ old('permission_name') }}">
22+
@error('permission_name')
23+
<div class="invalid-feedback" role="alert">
24+
<strong>{{ $message }}</strong>
25+
</div>
26+
@enderror
27+
</div>
28+
<div class="row">
29+
<div class="col-12 my-3">
30+
<button type="submit" class="btn btn-primary">Submit</button>
31+
</div>
32+
</div>
33+
</form>
34+
</div>
35+
</div>
36+
</div>
37+
38+
<!-- Optional JavaScript -->
39+
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
40+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
41+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
42+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
43+
</body>
44+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<!-- Required meta tags -->
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
8+
<!-- Bootstrap CSS -->
9+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
10+
11+
<title>Edit Permission</title>
12+
</head>
13+
<body>
14+
<div class="container">
15+
<div class="row">
16+
<div class="col-12 my-5">
17+
<form method="POST" action="{{ route('permission.update', $permission) }}">
18+
@csrf
19+
@method('PUT')
20+
<div class="form-group">
21+
<label for="permission_name">Permission name</label>
22+
<input type="text" class="form-control @error('permission_name') is-invalid @enderror" name="permission_name" id="permission_name" aria-describedby="permission_name" placeholder="Enter permission name" value="{{ !empty(old('permission_name')) ? old('permission_name') : $permission->name }}">
23+
@error('permission_name')
24+
<span class="invalid-feedback" role="alert">
25+
<strong>{{ $errors->first('permission_name') }}</strong>
26+
</span>
27+
@endif
28+
</div>
29+
<div class="row">
30+
<div class="col-12 my-3">
31+
<button type="submit" class="btn btn-primary">Submit</button>
32+
</div>
33+
</div>
34+
</form>
35+
</div>
36+
</div>
37+
</div>
38+
39+
<!-- Optional JavaScript -->
40+
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
41+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
42+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
43+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
44+
</body>
45+
</html>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<!-- Required meta tags -->
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
8+
<!-- Bootstrap CSS -->
9+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
10+
11+
<title>Permissions</title>
12+
</head>
13+
<body>
14+
<div class="container">
15+
<div class="row">
16+
<div class="col-12 mt-3">
17+
<div class="text-right">
18+
<a href="{{ route('permission.create') }}" class="btn btn-success btn-sm">Add</a>
19+
</div>
20+
</div>
21+
</div>
22+
<div class="row">
23+
<div class="col-12 my-5">
24+
<table class="table">
25+
<thead>
26+
<tr>
27+
<th scope="col">Name</th>
28+
<th scope="col">Guard</th>
29+
<th scope="col">Actions</th>
30+
</tr>
31+
</thead>
32+
<tbody>
33+
@foreach($permissions as $permission)
34+
<tr>
35+
<th scope="row">{{ $permission->name }}</th>
36+
<th scope="row">{{ $permission->guard_name }}</th>
37+
<td>
38+
<a href="{{ route('permission.edit', $permission) }} " class="btn btn-warning text-white btn-sm">Edit</a>
39+
</td>
40+
</tr>
41+
@endforeach
42+
</tbody>
43+
</table>
44+
{{ $permissions->links() }}
45+
</div>
46+
</div>
47+
</div>
48+
49+
<!-- Optional JavaScript -->
50+
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
51+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
52+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
53+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
54+
</body>
55+
</html>

src/stubs/views/bootstrap/roles/edit.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- Bootstrap CSS -->
99
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
1010

11-
<title>Roles</title>
11+
<title>Edit Role</title>
1212
</head>
1313
<body>
1414
<div class="container">

src/stubs/views/bootstrap/roles/index.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<div class="row">
1616
<div class="col-12 mt-3">
1717
<div class="text-right">
18-
<a href="{{ route('role.create') }} " class="btn btn-success btn-sm">Add</a>
18+
<a href="{{ route('role.create') }}" class="btn btn-success btn-sm">Add</a>
1919
</div>
2020
</div>
2121
</div>

0 commit comments

Comments
 (0)