Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nasirkhan committed Apr 16, 2024
2 parents 68c89c0 + 68a1f04 commit 7b18edb
Show file tree
Hide file tree
Showing 22 changed files with 381 additions and 368 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync

CACHE_STORE=database
CACHE_STORE=file
CACHE_PREFIX=

MEMCACHED_HOST=127.0.0.1
Expand Down
5 changes: 3 additions & 2 deletions app/Console/Commands/InsertDemoData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\Category\Models\Category;
use Modules\Comment\Models\Comment;
use Modules\Post\Models\Post;
Expand Down Expand Up @@ -101,7 +102,7 @@ public function truncate_tables()

if ($confirmed) {
// Disable foreign key checks!
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::disableForeignKeyConstraints();

foreach ($tables_list as $row) {
$table_name = $row;
Expand All @@ -112,7 +113,7 @@ public function truncate_tables()
}

// Enable foreign key checks!
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
Schema::enableForeignKeyConstraints();
} else {
$this->warn('Skipped database truncate.');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Backend/BackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function create()
Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n".$output);

// return the results as a response to the ajax call
flash(icon().'New backup created')->success()->important();
flash('New backup created')->success()->important();

return redirect()->back();
} catch (Exception $e) {
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/Backend/NotificationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Laracasts\Flash\Flash;

class NotificationsController extends Controller
{
Expand Down Expand Up @@ -125,7 +124,7 @@ public function deleteAll()

$user->notifications()->delete();

Flash::success("<i class='fas fa-check'></i> All Notifications Deleted")->important();
flash('All Notifications Deleted')->success()->important();

Log::info(label_case($module_title.' '.$module_action).' | User:'.Auth::user()->name.'(ID:'.Auth::user()->id.')');

Expand All @@ -152,7 +151,7 @@ public function markAllAsRead()

$user->unreadNotifications()->update(['read_at' => now()]);

Flash::success("<i class='fas fa-check'></i> All Notifications Marked As Read")->important();
flash('All Notifications Marked As Read')->success()->important();

Log::info(label_case($module_title.' '.$module_action).' | User:'.Auth::user()->name.'(ID:'.Auth::user()->id.')');

Expand Down
74 changes: 39 additions & 35 deletions app/Http/Controllers/Backend/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use App\Http\Controllers\Controller;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Laracasts\Flash\Flash;
Expand Down Expand Up @@ -109,17 +111,20 @@ public function store(Request $request)

$module_action = 'Store';

$$module_name_singular = Role::create($request->except('permissions'));
$validated_data = $request->validate([
'name' => 'required|max:100|unique:roles,name',
'permissions' => 'nullable|array',
]);

$permissions = $request['permissions'];
// Update Name
$data = Arr::except($validated_data, ['permissions']);
$$module_name_singular = Role::create($data);

// Sync Permissions
if (isset($permissions)) {
$$module_name_singular->syncPermissions($permissions);
} else {
$permissions = [];
$$module_name_singular->syncPermissions($permissions);
}
$permissions = isset($validated_data['permissions']) ? $validated_data['permissions'] : [];
$$module_name_singular->syncPermissions($permissions);

flash("{$$module_name_singular->name} {$module_name_singular} created successfully!")->success()->important();

Log::info(label_case($module_title.' '.$module_action).' | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');

Expand All @@ -146,11 +151,13 @@ public function show($id)

$$module_name_singular = $module_model::findOrFail($id);

$users = User::role($$module_name_singular->name)->get();

Log::info(label_case($module_title.' '.$module_action).' | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');

return view(
"backend.{$module_name}.show",
compact('module_title', 'module_name', 'module_path', 'module_icon', 'module_action', 'module_name_singular', "{$module_name_singular}")
compact('module_title', 'module_name', 'module_path', 'module_icon', 'module_action', 'module_name_singular', "{$module_name_singular}", 'users')
);
}

Expand Down Expand Up @@ -201,25 +208,20 @@ public function update(Request $request, $id)

$$module_name_singular = $module_model::findOrFail($id);

$request->validate($request, [
'name' => 'required|max:20|unique:roles,name,'.$id,
'permissions' => 'required',
$validated_data = $request->validate([
'name' => 'required|max:100|unique:roles,name,'.$id,
'permissions' => 'nullable|array',
]);

$input = $request->except(['permissions']);
$permissions = $request['permissions'];
$$module_name_singular->fill($input)->save();

$p_all = Permission::all(); //Get all permissions
// Update Name
$data = Arr::except($validated_data, ['permissions']);
$$module_name_singular->update($data);

foreach ($p_all as $p) {
$$module_name_singular->revokePermissionTo($p); //Remove all permissions associated with role
}
// Update Permissions
$permissions = isset($validated_data['permissions']) ? $validated_data['permissions'] : [];
$$module_name_singular->syncPermissions($permissions);

foreach ($permissions as $permission) {
$p = Permission::where('name', '=', $permission)->firstOrFail(); //Get corresponding form //permission in db
$$module_name_singular->givePermissionTo($p); //Assign permission to role
}
flash(label_case($$module_name_singular->name.' '.$module_name_singular).' updated successfully!')->success()->important();

Log::info(label_case($module_title.' '.$module_action).' | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');

Expand All @@ -245,26 +247,28 @@ public function destroy($id)
$module_action = 'Destroy';

$$module_name_singular = Role::findOrFail($id);
$role_name = $$module_name_singular->name;

$user_roles = auth()->user()->getRoleNames();

$user_roles = auth()->user()->roles()->pluck('id');
$role_users = $$module_name_singular->users;
$role_users = User::with('roles')->get()->filter(
fn ($user) => $user->roles->where('name', $role_name)->toArray()
)->count();

if ($id === 1) {
Flash::warning("<i class='fas fa-exclamation-triangle'></i> You can not delete 'Administrator'!")->important();
if ($id == 1) {
Flash::warning("You can not delete {$$module_name_singular->name} role!")->important();

Log::notice(label_case($module_title.' '.$module_action).' Failed | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');

return redirect()->route("backend.{$module_name}.index");
}
if (in_array($id, $user_roles->toArray())) {
Flash::warning("<i class='fas fa-exclamation-triangle'></i> You can not delete your Role!")->important();
} elseif (in_array($role_name, $user_roles->toArray())) {
Flash::warning('You can not delete your Role!')->important();

Log::notice(label_case($module_title.' '.$module_action).' Failed | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');

return redirect()->route("backend.{$module_name}.index");
}
if ($role_users->count()) {
Flash::warning("<i class='fas fa-exclamation-triangle'></i> Can not be deleted! ".$role_users->count().' user found!')->important();
} elseif ($role_users) {
Flash::warning('Can not be deleted! '.$role_users.' user(s) found!')->important();

Log::notice(label_case($module_title.' '.$module_action).' Failed | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');

Expand All @@ -277,7 +281,7 @@ public function destroy($id)

Log::info(label_case($module_title.' '.$module_action).' | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');

return redirect()->back();
return redirect()->route("backend.{$module_name}.index");
}
} catch (\Exception $e) {
Log::error($e);
Expand Down
Loading

0 comments on commit 7b18edb

Please sign in to comment.