-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from IronSinew/blake-user-management
Add user management
- Loading branch information
Showing
17 changed files
with
698 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,4 @@ cert.pem | |
/.blueprint | ||
/.editorconfig | ||
/storage/media-library/* | ||
/.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Admin; | ||
|
||
use App\Enums\BannerTypeEnum; | ||
use App\Enums\UserRoleEnum; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
use Inertia\Inertia; | ||
|
||
class UserController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
return Inertia::render('Admin/User/UserIndex')->with([ | ||
'users' => fn () => User::withCount('recipes') | ||
->orderBy('id', 'desc') | ||
->get(), | ||
'roles' => UserRoleEnum::toSelectOptions(), | ||
]); | ||
} | ||
|
||
public function store(Request $request): RedirectResponse | ||
{ | ||
$validated = $request->validate([ | ||
'name' => ['required', 'max:140'], | ||
'email' => ['required', 'email', Rule::unique('users')], | ||
'password' => ['required', 'string', 'confirmed', 'min:8'], | ||
'role' => ['nullable', 'string', 'in:'.collect(UserRoleEnum::cases())->implode(fn ($r) => $r->value, ',')], | ||
]); | ||
|
||
$user = User::create([ | ||
'name' => $validated['name'], | ||
'email' => $validated['email'], | ||
'password' => bcrypt($validated['password']), | ||
'role' => UserRoleEnum::tryFrom($validated['role']), | ||
]); | ||
|
||
return redirect()->route('admin.users.index')->withBanner("Successfully created {$user->name}"); | ||
} | ||
|
||
public function edit(User $user) | ||
{ | ||
return Inertia::render('Admin/User/UserEdit')->with([ | ||
'user' => fn () => $user->makeVisible('id'), | ||
'roles' => fn () => UserRoleEnum::toSelectOptions(), | ||
]); | ||
} | ||
|
||
public function update(User $user, Request $request) | ||
{ | ||
$validated = $request->validate([ | ||
'name' => ['required', 'max:140'], | ||
'email' => ['required', 'email', Rule::unique('users')->ignore($user->id)], | ||
'role' => ['nullable', 'string', 'in:'.collect(UserRoleEnum::cases())->implode(fn ($r) => $r->value, ',')], | ||
]); | ||
|
||
$user->update($validated); | ||
|
||
return redirect()->back()->withBanner("Successfully updated {$user->name}"); | ||
} | ||
|
||
public function create() | ||
{ | ||
return Inertia::render('Admin/User/UserEdit')->with([ | ||
'user' => fn () => new User(), | ||
'roles' => fn () => UserRoleEnum::toSelectOptions(), | ||
]); | ||
} | ||
|
||
public function destroy(User $user) | ||
{ | ||
$name = $user->name; | ||
$user->delete(); | ||
|
||
return redirect()->route('admin.users.index') | ||
->withBanner("Deleted {$name}", BannerTypeEnum::danger); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
trait DefaultLabelsFromEnum | ||
{ | ||
/** @codeCoverageIgnore */ | ||
public function label(): string | ||
{ | ||
return match ($this) { | ||
default => Str::headline(Str::lower($this->name)) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
|
||
use JetBrains\PhpStorm\ArrayShape; | ||
|
||
trait SelectOptionsFromEnum | ||
{ | ||
#[ArrayShape(['name' => 'string', 'value' => 'string'])] | ||
public static function toSelectOptions(): array | ||
{ | ||
return collect(self::cases())->transform(fn ($category) => [ | ||
'name' => $category->label(), | ||
'value' => $category->value, | ||
])->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<script setup> | ||
import AppLayout from '@/Layouts/AppLayout.vue'; | ||
import Breadcrumb from 'primevue/breadcrumb'; | ||
import Button from "primevue/button"; | ||
import Dropdown from 'primevue/dropdown'; | ||
import InputText from "primevue/inputtext"; | ||
import Password from 'primevue/password'; | ||
import {router, useForm, Link} from "@inertiajs/vue3"; | ||
import {onMounted, ref, watch} from "vue"; | ||
const props = defineProps({ | ||
user: { | ||
type: [Array, Object], | ||
required: false, | ||
default() { | ||
return []; | ||
}, | ||
}, | ||
roles: { | ||
type: [Array, Object], | ||
required: false, | ||
default() { | ||
return []; | ||
}, | ||
} | ||
}); | ||
const form = useForm({ | ||
id: null, | ||
name: null, | ||
email: null, | ||
password: null, | ||
password_confirmation: null, | ||
role: null, | ||
}); | ||
const save = () => { | ||
if (form.id) { | ||
form.put(route("admin.users.update", {user: form.id})); | ||
} else { | ||
form.post(route("admin.users.store"), { | ||
preserveScroll: true, | ||
onSuccess: () => { | ||
form.reset() | ||
} | ||
}); | ||
} | ||
} | ||
const breadcrumbs = ref([ | ||
{ label: 'Users', url: route("admin.users.index") }, | ||
{ label: props.user.id ? `Edit User` : "New User" } | ||
]); | ||
onMounted(() => { | ||
for (const [key, value] of Object.entries(props.user)) { | ||
form[key] = value; | ||
} | ||
}); | ||
</script> | ||
|
||
<template> | ||
<AppLayout title="User Admin"> | ||
<template #header> | ||
<Breadcrumb :model="breadcrumbs"/> | ||
</template> | ||
<div class="py-12"> | ||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> | ||
<form @submit.prevent="save" class="mb-5"> | ||
<h5 class="text-xl font-bold text-primary-200 mb-3"> | ||
{{ props.user.id ? `Edit ${props.user.name}` : "New User" }} | ||
</h5> | ||
<div class="grid grid-cols-3 gap-x-6"> | ||
<div class="my-7"> | ||
<label>User's Name</label> | ||
<InputText v-model="form.name" class="font-normal w-full"/> | ||
<div :class="[form.errors.name ? 'opacity-100' : 'opacity-0']" | ||
class="transition-opacity ease-in-out delay-150 duration-300 pt-4 text-sm text-red-500 font-bold"> | ||
{{ form.errors.name }} | ||
</div> | ||
</div> | ||
|
||
<div class="my-7"> | ||
<label>Email</label> | ||
<InputText v-model="form.email" class="font-normal w-full"/> | ||
<div :class="[form.errors.email ? 'opacity-100' : 'opacity-0']" | ||
class="transition-opacity ease-in-out delay-150 duration-300 pt-4 text-sm text-red-500 font-bold"> | ||
{{ form.errors.email }} | ||
</div> | ||
</div> | ||
|
||
<div class="my-7"> | ||
<label>Role</label> | ||
<Dropdown | ||
class="font-normal w-full" | ||
v-model="form.role" | ||
:options="roles" | ||
option-label="name" | ||
option-value="value" | ||
placeholder="Member" | ||
/> | ||
<div :class="[form.errors.email ? 'opacity-100' : 'opacity-0']" | ||
class="transition-opacity ease-in-out delay-150 duration-300 pt-4 text-sm text-red-500 font-bold"> | ||
{{ form.errors.email }} | ||
</div> | ||
</div> | ||
</div> | ||
<div v-if="! props.user.id" class="grid grid-cols-3 gap-x-6"> | ||
<div class="my-7"> | ||
<label>Password</label> | ||
<Password v-model="form.password" class="font-normal w-full"/> | ||
<div :class="[form.errors.password ? 'opacity-100' : 'opacity-0']" | ||
class="transition-opacity ease-in-out delay-150 duration-300 pt-4 text-sm text-red-500 font-bold"> | ||
{{ form.errors.password }} | ||
</div> | ||
</div> | ||
|
||
<div class="my-7"> | ||
<label>Confirm Password</label> | ||
<Password v-model="form.password_confirmation" class="font-normal w-full"/> | ||
<div :class="[form.errors.password_confirmation ? 'opacity-100' : 'opacity-0']" | ||
class="transition-opacity ease-in-out delay-150 duration-300 pt-4 text-sm text-red-500 font-bold"> | ||
{{ form.errors.password_confirmation }} | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
<div class="text-right"> | ||
<Button type="submit" severity="success" label="Save" class="text-right" | ||
:disabled="form.processing"></Button> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</AppLayout> | ||
</template> |
Oops, something went wrong.