Skip to content

Commit 6aad647

Browse files
committed
init
0 parents  commit 6aad647

File tree

12 files changed

+711
-0
lines changed

12 files changed

+711
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/vendor
2+
/node_modules
3+
.env
4+
.env.*.local
5+
.idea/
6+
.vscode/
7+
.DS_Store
8+
/storage
9+
/tests/.env
10+
/tests/storage
11+
/phpunit.xml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 ootri
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Ootri Permission Manager
2+
3+
## Overview
4+
5+
Ootri Permission Manager is a very simple permission and role management dashboard for spatie/laravel-permission designed to work with the TALL stack, specifically Laravel Jetstream (optional). It leverages the Spatie Laravel Permission package to provide a user-friendly interface for managing users, roles, and permissions.
6+
7+
While it was designed to integrate with Jetstream, it can also be used standalone within any TALL stack application or a custom dashboards.
8+
9+
## Installation
10+
11+
Install via Composer:
12+
```bash
13+
composer require ootri/laravel-permission-manager
14+
```
15+
16+
## Usage
17+
18+
### Option 1: Permissions already set
19+
20+
Add this if you've already setup permissions using spatie/laravel-permission:
21+
```php
22+
Route::group(['middleware' => ['can:admin']], function () {
23+
Route::get('/permissions', [\Ootri\PermissionManager\Http\Controllers\PermissionManagementController::class, 'index'])->name('permissions.index');
24+
});
25+
```
26+
27+
### Option 2: Restrict to auth/user
28+
29+
If you haven't yet created any permissions/roles:
30+
```php
31+
// Restrict to any logged in user
32+
Route::middleware(['auth'])->group(function () {
33+
Route::get('/permissions', [\Ootri\PermissionManager\Http\Controllers\PermissionManagementController::class, 'index'])->name('permissions.index');
34+
});
35+
36+
// Or specify a user by email address
37+
Route::middleware(['auth'])->group(function () {
38+
Route::get('/permissions', function () {
39+
if (\Illuminate\Support\Facades\Auth::check() && \Illuminate\Support\Facades\Auth::user()->email === '[email protected]') {
40+
return app()->call([\Ootri\PermissionManager\Http\Controllers\PermissionManagementController::class, 'index']);
41+
}
42+
abort(403, 'Unauthorized');
43+
})->name('permissions.index');
44+
});
45+
```
46+
IMPORTANT: This is just temporary until admin permissions/roles are created, then change it to Option 1.
47+
48+
### Option 3: Using Livewire Components Directly
49+
50+
If you prefer to integrate the permission management tools directly into an existing dashboard or custom UI, you can include the Livewire components directly in your Blade templates:
51+
52+
```php
53+
@livewire('ootri-permission-management')
54+
@livewire('ootri-user-management')
55+
```
56+
57+
This approach removes the dependency on Jetstream, allowing flexibility to integrate into any other app based on the TALL stack.
58+
59+
## Features
60+
61+
- User Management: Add, view, and remove users.
62+
- Role Management: Create, assign, and revoke roles.
63+
- Permission Management: Create, assign, and revoke permissions.
64+
- Direct User Permissions: Assign/revoke permissions directly to/from users.
65+
- Clear Overview: Display current assignments for users, roles, and permissions.
66+
67+
This package is intended to be and remain simple.

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "ootri/laravel-permission-manager",
3+
"description": "A permissions management module for Laravel apps using Spatie Permissions, Livewire, and Tailwind CSS.",
4+
"type": "library",
5+
"keywords": [
6+
"laravel",
7+
"permissions",
8+
"spatie",
9+
"livewire",
10+
"tailwind",
11+
"admin"
12+
],
13+
"license": "MIT",
14+
"authors": [
15+
{
16+
"name": "Dustin",
17+
"email": "[email protected]"
18+
}
19+
],
20+
"require": {
21+
"php": "^8.2",
22+
"laravel/framework": "^11.0",
23+
"spatie/laravel-permission": "^6.0",
24+
"livewire/livewire": "^3.0"
25+
},
26+
"require-dev": {
27+
"orchestra/testbench": "^8.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Ootri\\PermissionManager\\": "src/"
32+
}
33+
},
34+
"extra": {
35+
"laravel": {
36+
"providers": [
37+
"Ootri\\PermissionManager\\PermissionManagerServiceProvider"
38+
]
39+
}
40+
}
41+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Ootri\PermissionManager\Http\Controllers;
4+
5+
class PermissionManagementController
6+
{
7+
public static function index()
8+
{
9+
return view('Ootri\PermissionManager::permissions.index');
10+
}
11+
}
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<?php
2+
3+
namespace Ootri\PermissionManager\Livewire;
4+
5+
use Livewire\Component;
6+
use Spatie\Permission\Models\Role;
7+
use Spatie\Permission\Models\Permission;
8+
use App\Models\User;
9+
10+
class PermissionManagement extends Component
11+
{
12+
public $roles, $permissions, $users;
13+
public $roleName, $permissionName;
14+
public $selectedUser, $selectedRole, $selectedPermission;
15+
16+
public function mount()
17+
{
18+
$this->loadData();
19+
}
20+
21+
public function loadData()
22+
{
23+
$this->roles = Role::with('permissions')->get(); // Include permissions in roles
24+
$this->permissions = Permission::all();
25+
$this->users = User::with('roles', 'permissions')->get(); // Include roles and permissions in users
26+
}
27+
28+
// Role CRUD
29+
public function createRole()
30+
{
31+
Role::create(['name' => $this->roleName]);
32+
$this->roleName = '';
33+
$this->loadData();
34+
}
35+
36+
public function deleteRole($id)
37+
{
38+
$role = Role::find($id);
39+
40+
if (!$role) {
41+
session()->flash('error', 'Role not found.');
42+
return;
43+
}
44+
45+
if ($role->users()->exists()) {
46+
session()->flash('error', 'Cannot delete role because it is assigned to users.');
47+
return;
48+
}
49+
50+
// Check if role has any permissions
51+
if ($role->permissions()->exists()) {
52+
session()->flash('error', 'Cannot delete role because it has permissions assigned.');
53+
return;
54+
}
55+
56+
$role->delete();
57+
$this->loadData();
58+
session()->flash('message', 'Role deleted successfully.');
59+
}
60+
61+
// Permission CRUD
62+
public function createPermission()
63+
{
64+
Permission::create(['name' => $this->permissionName]);
65+
$this->permissionName = '';
66+
$this->loadData();
67+
}
68+
69+
public function deletePermission($id)
70+
{
71+
$permission = Permission::find($id);
72+
73+
if (!$permission) {
74+
session()->flash('error', 'Permission not found.');
75+
return;
76+
}
77+
78+
// Check if permission is assigned to any roles
79+
if ($permission->roles()->exists()) {
80+
session()->flash('error', 'Cannot delete permission because it is assigned to roles.');
81+
return;
82+
}
83+
84+
// Check if permission is assigned directly to any users
85+
if ($permission->users()->exists()) {
86+
session()->flash('error', 'Cannot delete permission because it is assigned directly to users.');
87+
return;
88+
}
89+
90+
$permission->delete();
91+
$this->loadData();
92+
session()->flash('message', 'Permission deleted successfully.');
93+
}
94+
95+
// Assign Role to User
96+
public function assignRole()
97+
{
98+
$user = User::find($this->selectedUser);
99+
$role = Role::find($this->selectedRole);
100+
101+
if ($user && $role) {
102+
103+
// Check if role is already assigned to user
104+
if ($user->hasRole($role->name)) {
105+
session()->flash('error', 'Role is already assigned to this user.');
106+
return;
107+
}
108+
109+
$user->assignRole($role->name);
110+
$this->loadData();
111+
session()->flash('message', 'Role assigned successfully.');
112+
} else {
113+
session()->flash('error', 'Invalid user or role selected.');
114+
}
115+
}
116+
117+
public function revokeRole()
118+
{
119+
$user = User::find($this->selectedUser);
120+
$role = Role::find($this->selectedRole);
121+
122+
if ($user && $role) {
123+
124+
// Check if role is already assigned to user
125+
if (!$user->hasRole($role->name)) {
126+
session()->flash('error', 'Role is not assigned to this user.');
127+
return;
128+
}
129+
130+
$user->removeRole($role->name);
131+
$this->loadData();
132+
session()->flash('message', 'Role revoked successfully.');
133+
} else {
134+
session()->flash('error', 'Invalid user or role selected.');
135+
}
136+
}
137+
138+
// Assign Permission to Role
139+
public function assignPermission()
140+
{
141+
$role = Role::find($this->selectedRole);
142+
$permission = Permission::find($this->selectedPermission);
143+
144+
if ($role && $permission) {
145+
146+
// Check if permission is already assigned to role
147+
if ($role->hasPermissionTo($permission->name)) {
148+
session()->flash('error', 'Permission is already assigned to this role.');
149+
return;
150+
}
151+
152+
$role->givePermissionTo($permission->name);
153+
$this->loadData();
154+
session()->flash('message', 'Permission assigned successfully.');
155+
} else {
156+
session()->flash('error', 'Invalid role or permission selected.');
157+
}
158+
}
159+
160+
public function revokePermission()
161+
{
162+
$role = Role::find($this->selectedRole);
163+
$permission = Permission::find($this->selectedPermission);
164+
165+
if ($role && $permission) {
166+
167+
// Check if permission is already assigned to role
168+
if (!$role->hasPermissionTo($permission->name)) {
169+
session()->flash('error', 'Permission is not assigned to this role.');
170+
return;
171+
}
172+
173+
$role->revokePermissionTo($permission->name);
174+
$this->loadData();
175+
session()->flash('message', 'Permission revoked successfully.');
176+
} else {
177+
session()->flash('error', 'Invalid role or permission selected.');
178+
}
179+
}
180+
181+
public function getUserRoles($userId)
182+
{
183+
$user = $this->users->find($userId);
184+
return $user ? $user->roles->pluck('name')->toArray() : [];
185+
}
186+
187+
public function getRolePermissions($roleId)
188+
{
189+
$role = $this->roles->find($roleId);
190+
return $role ? $role->permissions->pluck('name')->toArray() : [];
191+
}
192+
193+
public function assignUserPermission()
194+
{
195+
$user = User::find($this->selectedUser);
196+
$permission = Permission::find($this->selectedPermission);
197+
198+
if ($user && $permission) {
199+
if ($user->hasPermissionTo($permission->name)) {
200+
session()->flash('error', 'Permission is already assigned to this user.');
201+
return;
202+
}
203+
204+
$user->givePermissionTo($permission->name);
205+
$this->loadData();
206+
session()->flash('message', 'Permission assigned to user successfully.');
207+
} else {
208+
session()->flash('error', 'Invalid user or permission selected.');
209+
}
210+
}
211+
212+
public function revokeUserPermission()
213+
{
214+
$user = User::find($this->selectedUser);
215+
$permission = Permission::find($this->selectedPermission);
216+
217+
if ($user && $permission) {
218+
if (!$user->hasPermissionTo($permission->name)) {
219+
session()->flash('error', 'Permission is not assigned to this user.');
220+
return;
221+
}
222+
223+
$user->revokePermissionTo($permission->name);
224+
$this->loadData();
225+
session()->flash('message', 'Permission revoked from user successfully.');
226+
} else {
227+
session()->flash('error', 'Invalid user or permission selected.');
228+
}
229+
}
230+
231+
public function render()
232+
{
233+
return view('Ootri\PermissionManager::livewire.permission-management');
234+
}
235+
}

0 commit comments

Comments
 (0)