diff --git a/app/Console/Commands/CreateUser.php b/app/Console/Commands/CreateUser.php new file mode 100644 index 0000000..7f12e60 --- /dev/null +++ b/app/Console/Commands/CreateUser.php @@ -0,0 +1,33 @@ +confirm('Should this user be a superuser?'); + $username = $this->ask('What is the username?'); + $email = $this->ask('What is the email?'); + $password = $this->secret('What is the password?'); + + $user = User::create([ + 'name' => $username, + 'email' => $email, + 'password' => Hash::make($password), + 'is_superuser' => $isSuperuser, + ]); + + $this->table(['ID', 'Username', 'Email', 'Superuser'], [ + [$user->id, $user->name, $user->email, $user->is_superuser ? 'Yes' : 'No'], + ]); + } +} diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 8933385..0e71339 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -6,6 +6,7 @@ use Inertia\Response; use Influx\Models\User; use Illuminate\Http\Request; +use Illuminate\Http\RedirectResponse; use Illuminate\Contracts\Hashing\Hasher; class UsersController extends Controller @@ -91,4 +92,20 @@ public function update(Request $request, int $id): Response return Inertia::render('Users/Edit', ['user' => $user]); } + + /** + * Delete a user account. + */ + public function delete(int $id): RedirectResponse + { + $user = User::findOrFail($id); + + try { + $user->delete(); + } catch (\Exception $ex) { + throw new \Exception($ex->getMessage()); + }; + + return redirect()->intended(route('users.index', absolute: false));; + } } diff --git a/package-lock.json b/package-lock.json index cd9a9cc..26f3c45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "influxdev", + "name": "influx", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/resources/js/Pages/Users/DeleteUserForm.tsx b/resources/js/Pages/Users/DeleteUserForm.tsx new file mode 100644 index 0000000..6dee68b --- /dev/null +++ b/resources/js/Pages/Users/DeleteUserForm.tsx @@ -0,0 +1,48 @@ +import { useState } from 'react'; +import Modal from '@/Components/Modal'; +import { router } from '@inertiajs/react'; +import DangerButton from '@/Components/DangerButton'; +import SecondaryButton from '@/Components/SecondaryButton'; + +export default function DeleteUserForm({ id }: { id: number }) { + const [open, setOpen] = useState(false); + const [submitting, setSubmitting] = useState(false); + + const deleteUser = () => { + setSubmitting(true); + + router.delete(`/users/${id}`, { + onError: (error) => console.error(error) + }); + }; + + return ( + <> + setOpen(true)} className={'mr-4'}> + Delete + + + setOpen(false)}> +
+

+ Are you sure? +

+ +

+ Deleting this user account will revoke their access immediately. +

+ +
+ setOpen(false)}> + Cancel + + + + Delete Account + +
+
+
+ + ); +} diff --git a/resources/js/Pages/Users/UserForm.tsx b/resources/js/Pages/Users/UserForm.tsx index 23b9bb7..b000974 100644 --- a/resources/js/Pages/Users/UserForm.tsx +++ b/resources/js/Pages/Users/UserForm.tsx @@ -1,7 +1,9 @@ +import DangerButton from '@/Components/DangerButton'; import PrimaryButton from '@/Components/PrimaryButton'; import { User } from '@/types'; import { router } from '@inertiajs/react'; import { useState } from 'react'; +import DeleteUserForm from './DeleteUserForm'; export default function UserForm({ user }: { user?: User }) { const [values, setValues] = useState({ @@ -106,7 +108,10 @@ export default function UserForm({ user }: { user?: User }) {
- Submit + {user && ( + + )} + {user ? 'Update' : 'Create'} User
); diff --git a/routes/web.php b/routes/web.php index 5d11c58..e3c2b45 100644 --- a/routes/web.php +++ b/routes/web.php @@ -9,7 +9,6 @@ Route::get('/', function () { return Inertia::render('Welcome', [ 'canLogin' => Route::has('login'), - 'canRegister' => Route::has('register'), 'laravelVersion' => Application::VERSION, 'phpVersion' => PHP_VERSION, ]); @@ -28,6 +27,7 @@ Route::get('/{user:id}', [UsersController::class, 'view'])->name('users.view'); Route::put('/{user:id}', [UsersController::class, 'update'])->name('users.update'); + Route::delete('/{user:id}', [UsersController::class, 'delete'])->name('users.delete'); }); Route::prefix('/profile')->group(function () {