Skip to content

Commit

Permalink
Added edit/delete groups
Browse files Browse the repository at this point in the history
  • Loading branch information
hobsRKM committed May 3, 2024
1 parent bafe973 commit a276478
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
26 changes: 9 additions & 17 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public function getGroupsList(Request $request)
// Format each group record
$siteDir = env('VITE_SITE_DIR');
foreach ($groups as $group) {
$preLoadDefaultServerId = SaGroupsServers::where('group_id', $group->id)->first()->server_id;
$preLoadDefaultServerId = SaGroupsServers::where('group_id', $group->id)?->first()?->server_id;
$formattedData[] = [
"id" => $group->id,
"name" => "<span style='font-size: 12px;' class='badge badge-dark'>{$group->name}</span>",
Expand All @@ -508,39 +508,29 @@ public function groups()
return view('admin.groups.list');
}

public function editGroup(Request $request, $group_id, $server_id) {
$groupServer = SaGroupsServers::with('groupsFlags')
->where('server_id', $server_id)
->where('group_id', $group_id)
public function editGroup(Request $request, $group_id) {
$groupFlags = SaGroupsFlags::where('group_id', $group_id)
->get();

if ($groupServer->isEmpty()) {
return redirect()->route('groups.list')->with('error', 'Group does not exist for the selected server!. Add Group to the server!');
}
$groupPermissions = $groupServer->pluck('groupsFlags.*.flag')->flatten()->toArray();
$groupPermissions = $groupFlags->pluck('flag')->toArray();
$groupDetails = SaGroups::where('id', $group_id)->first();
$permissions = Permission::all();
$servers = SaServer::all();
return view('admin.groups.edit', compact('servers', 'permissions', 'groupDetails', 'groupPermissions', 'server_id'));
return view('admin.groups.edit', compact('permissions', 'groupDetails', 'groupPermissions'));
}

public function updateGroup(Request $request, $groupId) {
$validated = $request->validate([
'permissions' => 'required|array',
'permissions.*' => 'exists:permissions,permission',
'server_id' => 'exists:sa_servers,id',
'immunity' => 'required',
'name' => 'required',
]);

$submittedPermissions = $validated['permissions'];
$groupServer = SaGroupsServers::with('groupsFlags')
->where('server_id',$validated['server_id'])
->where('group_id', $groupId)
$groupFlags = SaGroupsFlags::where('group_id', $groupId)
->get();
// Fetch current permissions from the database
$currentPermissions = $groupServer->pluck('groupsFlags.*.flag')->flatten()->toArray();
// Determine permissions to add and delete
$currentPermissions = $groupFlags->pluck('flag')->toArray(); // Determine permissions to add and delete
$permissionsToAdd = array_diff($submittedPermissions, $currentPermissions);
$permissionsToDelete = array_diff($currentPermissions, $submittedPermissions);

Expand Down Expand Up @@ -571,6 +561,7 @@ public function showGroupDeleteForm(Request $request, $groupId) {

public function deleteGroup(Request $request, $groupId) {
$validated = $request->validate([
'server_ids' => 'required|array',
'server_ids.*' => [
'required',
function ($attribute, $value, $fail) {
Expand All @@ -590,6 +581,7 @@ function ($attribute, $value, $fail) {

SaAdmin::where('group_id', $groupId)
->whereIn('server_id', $validated['server_ids'])->delete();

}

return redirect()->route('groups.list')->with('success', 'Group deleted successfully.');
Expand Down
3 changes: 3 additions & 0 deletions resources/views/admin/admins/delete.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@extends('layouts.app')

@section('content')
@if (session('error'))
<x-alert type="danger" :message="session('error')"/>
@endif
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow">
Expand Down
12 changes: 12 additions & 0 deletions resources/views/admin/groups/delete.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
@extends('layouts.app')

@section('content')
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow">
Expand All @@ -10,6 +19,9 @@
<div class="card-body">
<form action="{{ route('group.delete', ['id' => $groupDetails->id]) }}" method="POST">
@csrf
<div class="note note-info mb-3">
<strong>Note:</strong> If you want to delete a group, select all servers. Selecting specific servers will only remove the group from those servers but will not delete the group itself.
</div>
<div class="mb-3">
<label><b>Group Name:</b> {{$groupDetails->name}}</label>
</div>
Expand Down
15 changes: 0 additions & 15 deletions resources/views/admin/groups/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@
<input value="{{$groupDetails->name}}" placeholder="Should Start with #" type="text" class="form-control" id="group_name" name="name" required/>
<label class="form-label" for="group_name">Group Name</label>
</div>

<div data-mdb-input-init class="form-outline mb-3">
<label class="form-check-label" for="server_id">
Server
</label>
<select class="form-select" id="server_id" name="server_id" required>
<option value="">Select Server</option>
@foreach($servers as $server)
<option value="{{ $server->id }}" {{ $server->id == $server_id ? 'selected' : '' }}>
{{ $server->hostname }}
</option>
@endforeach
</select>

</div>
<hr/>
<div class="mb-3">
<label>Permissions</label><br>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
Route::prefix('group')->group(function () {
Route::get('/create', [AdminController::class, 'createGroup'])->name('group.create')->middleware('superadmin');
Route::post('/store', [AdminController::class, 'storeGroup'])->name('group.store')->middleware('superadmin');
Route::get('/edit/{group_id}/{server_id}', [AdminController::class, 'editGroup'])->name('group.edit')->middleware('superadmin');
Route::get('/edit/{group_id}', [AdminController::class, 'editGroup'])->name('group.edit')->middleware('superadmin');
Route::put('/update/{id}', [AdminController::class, 'updateGroup'])->name('group.update')->middleware('superadmin');
Route::get('/delete/{id}', [AdminController::class, 'showGroupDeleteForm'])->name('group.deleteForm')->middleware('superadmin');
Route::post('/delete/{id}', [AdminController::class, 'deleteGroup'])->name('group.delete')->middleware('superadmin');
Expand Down

0 comments on commit a276478

Please sign in to comment.