101 lines
2.9 KiB
PHP
101 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\User;
|
|
use Inertia\Inertia;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
class UsersController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return Inertia::render('Users/Index', [
|
|
'filters' => Request::all('search', 'role', 'trashed'),
|
|
'users' => Auth::user()->account->users()
|
|
->orderByName()
|
|
->filter(Request::only('search', 'role', 'trashed'))
|
|
->get()
|
|
->transform(function ($user) {
|
|
return [
|
|
'id' => $user->id,
|
|
'name' => $user->name,
|
|
'email' => $user->email,
|
|
'owner' => $user->owner,
|
|
'deleted_at' => $user->deleted_at,
|
|
];
|
|
}),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return Inertia::render('Users/Create');
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
Auth::user()->account->users()->create(
|
|
Request::validate([
|
|
'first_name' => ['required', 'max:50'],
|
|
'last_name' => ['required', 'max:50'],
|
|
'email' => ['required', 'max:50', 'email', Rule::unique('users')],
|
|
'password' => ['nullable'],
|
|
'owner' => ['required', 'boolean'],
|
|
])
|
|
);
|
|
|
|
return Redirect::route('users')->with('success', 'User created.');
|
|
}
|
|
|
|
public function edit(User $user)
|
|
{
|
|
return Inertia::render('Users/Edit', [
|
|
'user' => [
|
|
'id' => $user->id,
|
|
'first_name' => $user->first_name,
|
|
'last_name' => $user->last_name,
|
|
'email' => $user->email,
|
|
'owner' => $user->owner,
|
|
'deleted_at' => $user->deleted_at,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function update(User $user)
|
|
{
|
|
Request::validate([
|
|
'first_name' => ['required', 'max:50'],
|
|
'last_name' => ['required', 'max:50'],
|
|
'email' => ['required', 'max:50', 'email', Rule::unique('users')->ignore($user->id)],
|
|
'password' => ['nullable'],
|
|
'owner' => ['required', 'boolean'],
|
|
]);
|
|
|
|
$user->update(Request::only('first_name', 'last_name', 'email', 'owner'));
|
|
|
|
if (Request::get('password')) {
|
|
$user->update(['password' => Request::get('password')]);
|
|
}
|
|
|
|
return Redirect::route('users.edit', $user)->with('success', 'User updated.');
|
|
}
|
|
|
|
public function destroy(User $user)
|
|
{
|
|
$user->delete();
|
|
|
|
return Redirect::route('users.edit', $user)->with('success', 'User deleted.');
|
|
}
|
|
|
|
public function restore(User $user)
|
|
{
|
|
$user->restore();
|
|
|
|
return Redirect::route('users.edit', $user)->with('success', 'User restored.');
|
|
}
|
|
}
|