Skip to content

Commit

Permalink
Merge pull request #579 from nasirkhan/dev
Browse files Browse the repository at this point in the history
Test cases added
  • Loading branch information
nasirkhan authored May 2, 2024
2 parents f59faae + af3bed1 commit d5b1f08
Show file tree
Hide file tree
Showing 14 changed files with 737 additions and 270 deletions.
112 changes: 68 additions & 44 deletions app/Http/Controllers/Backend/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function index()

$$module_name = $module_model::paginate();

Log::info("'{$title}' viewed by User:".auth()->user()->name.'(ID:'.auth()->user()->id.')');
logUserAccess($module_title.' '.$module_action);

return view(
"{$module_path}.{$module_name}.index",
Expand Down Expand Up @@ -148,7 +148,7 @@ public function index_list(Request $request)
$module_model = $this->module_model;
$module_name_singular = Str::singular($module_name);

$module_action = 'List';
$module_action = 'Index List';

$page_heading = label_case($module_title);
$title = $page_heading.' '.label_case($module_action);
Expand All @@ -170,6 +170,8 @@ public function index_list(Request $request)
];
}

logUserAccess($module_title.' '.$module_action);

return response()->json($$module_name);
}

Expand All @@ -192,6 +194,8 @@ public function create()
$roles = Role::get();
$permissions = Permission::select('name', 'id')->orderBy('id')->get();

logUserAccess($module_title.' '.$module_action);

return view(
"{$module_path}.{$module_name}.create",
compact('module_title', 'module_name', 'module_path', 'module_icon', 'module_action', 'module_name_singular', 'roles', 'permissions')
Expand Down Expand Up @@ -223,7 +227,8 @@ public function store(Request $request)
'permissions' => 'nullable|array',
]);

$data_array = $request->except('_token', 'roles', 'permissions', 'password_confirmation');
$data_array = Arr::except($validated_data, ['_token', 'roles', 'permissions', 'password_confirmation']);

$data_array['name'] = $request->first_name.' '.$request->last_name;
$data_array['password'] = Hash::make($request->password);

Expand Down Expand Up @@ -263,7 +268,7 @@ public function store(Request $request)

Artisan::call('cache:clear');

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

return redirect("admin/{$module_name}");
}
Expand All @@ -287,7 +292,7 @@ public function show($id)

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

Log::info(label_case($module_title.' '.$module_action).' | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');
logUserAccess(__METHOD__." | {$$module_name_singular->name} ($id)");

return view(
"{$module_path}.{$module_name}.show",
Expand Down Expand Up @@ -323,6 +328,8 @@ public function changePassword($id)

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

logUserAccess("{$module_title} {$module_action} {$$module_name_singular->name} ($id)");

return view(
"{$module_path}.{$module_name}.changePassword",
compact('module_title', 'module_name', 'module_path', 'module_icon', 'module_action', 'module_name_singular', "{$module_name_singular}")
Expand Down Expand Up @@ -365,6 +372,8 @@ public function changePasswordUpdate(Request $request, $id)

flash(Str::singular($module_title)."' Updated Successfully")->success()->important();

logUserAccess("{$module_title} {$module_action} {$$module_name_singular->name} ($id)");

return redirect("admin/{$module_name}");
}

Expand Down Expand Up @@ -399,7 +408,7 @@ public function edit($id)
$roles = Role::get();
$permissions = Permission::select('name', 'id')->orderBy('id')->get();

Log::info(label_case($module_title.' '.$module_action)." | '".$$module_name_singular->name.'(ID:'.$$module_name_singular->id.") ' by User:".auth()->user()->name.'(ID:'.auth()->user()->id.')');
logUserAccess("{$module_title} {$module_action} {$$module_name_singular->name} ($id)");

return view(
"{$module_path}.{$module_name}.edit",
Expand Down Expand Up @@ -439,33 +448,40 @@ public function update(Request $request, $id)
'permissions' => 'nullable|array',
]);

$validated_data['name'] = $validated_data['first_name'].' '.$validated_data['last_name'];

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

$$module_name_singular->update($request->except(['roles', 'permissions']));
$$module_name_singular->update(Arr::except($validated_data, ['roles', 'permissions']));

if ($id === 1) {
$user->syncRoles(['super admin']);

flash(Str::singular($module_title)."' Updated Successfully")->success()->important();

// Clear Cache
Artisan::call('cache:clear');

flash(Str::singular($module_title)."' Updated Successfully")->success()->important();

return redirect("admin/{$module_name}");
}

// Clear Cache
Artisan::call('cache:clear');

// Sync Roles
$$module_name_singular->syncRoles(isset($validated_data['roles']) ? $validated_data['roles'] : []);
$$module_name_singular->syncRoles((isset($validated_data['roles'])) ? $validated_data['roles'] : []);

// Sync Permissions
$$module_name_singular->syncPermissions(isset($validated_data['permissions']) ? $validated_data['permissions'] : []);
$$module_name_singular->syncPermissions((isset($validated_data['permissions'])) ? $validated_data['permissions'] : []);

// Clear Cache
Artisan::call('cache:clear');

event(new UserUpdated($$module_name_singular));

flash(Str::singular($module_title)."' Updated Successfully")->success()->important();

Log::info(label_case($module_title.' '.$module_action)." | '".$$module_name_singular->name.'(ID:'.$$module_name_singular->id.") ' by User:".auth()->user()->name.'(ID:'.auth()->user()->id.')');
logUserAccess("{$module_title} {$module_action} {$$module_name_singular->name} ($id)");

return redirect("admin/{$module_name}");
}
Expand All @@ -492,7 +508,7 @@ public function destroy($id)
if (auth()->user()->id === $id || $id === 1) {
flash('You can not delete this user!')->warning()->important();

Log::notice(label_case($module_title.' '.$module_action).' Failed | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');
logUserAccess("{$module_title} {$module_action} Failed! {$$module_name_singular->name} ($id)");

return redirect()->back();
}
Expand All @@ -509,7 +525,7 @@ public function destroy($id)

flash($$module_name_singular->name.' User Successfully Deleted!')->success()->important();

Log::info(label_case($module_action)." '{$module_name}': '".$$module_name_singular->name.', ID:'.$$module_name_singular->id." ' by User:".auth()->user()->name);
logUserAccess("{$module_title} {$module_action} ($id)");

return redirect("admin/{$module_name}");
}
Expand All @@ -534,6 +550,8 @@ public function trashed()

logUserAccess($module_title.' '.$module_action);

logUserAccess("{$module_title} {$module_action}");

return view(
"{$module_path}.{$module_name}.trash",
compact('module_title', 'module_name', 'module_path', "{$module_name}", 'module_icon', 'module_name_singular', 'module_action')
Expand Down Expand Up @@ -569,7 +587,7 @@ public function restore($id)

flash($$module_name_singular->name.' Successfully Restoreded!')->success()->important();

Log::info(label_case($module_action)." '{$module_name}': '".$$module_name_singular->name.', ID:'.$$module_name_singular->id." ' by User:".auth()->user()->name);
logUserAccess("{$module_title} {$module_action} {$$module_name_singular->name} ($id)");

return redirect("admin/{$module_name}");
}
Expand Down Expand Up @@ -607,18 +625,16 @@ public function block($id)

$$module_name_singular = User::withTrashed()->find($id);

try {
$$module_name_singular->status = 2;
$$module_name_singular->save();
$$module_name_singular->status = 2;
$$module_name_singular->save();

event(new UserUpdated($$module_name_singular));

event(new UserUpdated($$module_name_singular));
flash($$module_name_singular->name.' User Successfully Blocked!')->success()->important();

flash($$module_name_singular->name.' User Successfully Blocked!')->success()->important();
logUserAccess("{$module_title} {$module_action} {$$module_name_singular->name} ($id)");

return redirect()->back();
} catch (Exception $e) {
throw new Exception('There was a problem updating this user. Please try again.');
}
return redirect()->back();
}

/**
Expand Down Expand Up @@ -654,23 +670,16 @@ public function unblock($id)

$$module_name_singular = User::withTrashed()->find($id);

try {
$$module_name_singular->status = 1;
$$module_name_singular->save();

event(new UserUpdated($$module_name_singular));
$$module_name_singular->status = 1;
$$module_name_singular->save();

flash($$module_name_singular->name.' - User Successfully Unblocked!')->success()->important();
event(new UserUpdated($$module_name_singular));

Log::notice(label_case($module_title.' '.$module_action).' Success | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');
flash($$module_name_singular->name.' - User Successfully Unblocked!')->success()->important();

return redirect()->back();
} catch (Exception $e) {
flash('There was a problem updating this user. Please try again.!')->error()->important();
logUserAccess("{$module_title} {$module_action} {$$module_name_singular->name} ($id)");

Log::error(label_case($module_title.' '.$module_action).' | User:'.auth()->user()->name.'(ID:'.auth()->user()->id.')');
Log::error($e);
}
return redirect()->back();
}

/**
Expand Down Expand Up @@ -724,16 +733,29 @@ public function userProviderDestroy(Request $request)
*/
public function emailConfirmationResend($id)
{
if ($id !== auth()->user()->id) {
if (auth()->user()->hasAnyRole(['administrator', 'super admin'])) {
Log::info(auth()->user()->name.' ('.auth()->user()->id.') - User Requested for Email Verification.');
} else {
Log::warning(auth()->user()->name.' ('.auth()->user()->id.') - User trying to confirm another users email.');
$module_title = $this->module_title;
$module_name = $this->module_name;
$module_path = $this->module_path;
$module_icon = $this->module_icon;
$module_model = $this->module_model;
$module_name_singular = Str::singular($module_name);

abort('404');
}
$module_action = 'Email Confirmation Resend';

if (! auth()->user()->can('edit_users')) {
$id = auth()->user()->id;
}

// if ($id !== auth()->user()->id) {
// if (auth()->user()->hasAnyRole(['administrator', 'super admin'])) {
// Log::info(auth()->user()->name.' ('.auth()->user()->id.') - User Requested for Email Verification.');
// } else {
// Log::warning(auth()->user()->name.' ('.auth()->user()->id.') - User trying to confirm another users email.');

// abort('403');
// }
// }

$user = User::where('id', '=', $id)->first();

if ($user) {
Expand All @@ -751,6 +773,8 @@ public function emailConfirmationResend($id)

flash($user->name.', You already confirmed your email address at '.$user->email_verified_at->isoFormat('LL'))->success()->important();

logUserAccess($module_title.' '.$module_action);

return redirect()->back();
}
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit d5b1f08

Please sign in to comment.