133 lines
4.7 KiB
PHP
133 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Users;
|
|
|
|
use App\Events\User\Deleted;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\User\CreateUserRequest;
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use App\Repositories\Country\CountryRepository;
|
|
use App\Repositories\Role\RoleRepository;
|
|
use App\Repositories\User\UserRepository;
|
|
use App\Support\Enum\UserStatus;
|
|
use App\Support\Options;
|
|
use App\Support\UserPayload;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response as InertiaResponse;
|
|
use Vanguard\Plugins\Vanguard;
|
|
|
|
class UsersController extends Controller
|
|
{
|
|
public function __construct(private readonly UserRepository $users) {}
|
|
|
|
public function index(Request $request): InertiaResponse
|
|
{
|
|
$users = $this->users->paginate(20, $request->search, $request->status);
|
|
|
|
return Inertia::render('users/Index', [
|
|
'users' => $users->through(fn (User $user) => UserPayload::row($user)),
|
|
'statuses' => Options::fromList(['' => __('All')] + UserStatus::lists()),
|
|
'filters' => [
|
|
'search' => (string) $request->search,
|
|
'status' => (string) $request->status,
|
|
],
|
|
'databaseSessions' => config('session.driver') === 'database',
|
|
]);
|
|
}
|
|
|
|
public function show(User $user): InertiaResponse
|
|
{
|
|
return Inertia::render('users/Show', [
|
|
'user' => UserPayload::details($user),
|
|
'canImpersonate' => UserPayload::canImpersonate($user),
|
|
// Replaces the View::composer that let the activity log plugin push
|
|
// a panel onto this page.
|
|
'slots' => Vanguard::resolveSlot('user:show', ['user' => $user]),
|
|
]);
|
|
}
|
|
|
|
public function create(CountryRepository $countryRepository, RoleRepository $roleRepository): InertiaResponse
|
|
{
|
|
return Inertia::render('users/Create', [
|
|
'countries' => $this->parseCountries($countryRepository),
|
|
'roles' => Options::fromList($roleRepository->lists()),
|
|
'statuses' => Options::fromList(UserStatus::lists()),
|
|
'defaultRoleId' => Role::where('name', Role::DEFAULT_USER_ROLE)->value('id'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Countries with a blank first entry, so a user may be left without one.
|
|
*
|
|
* @return array<int, array{value: string|int, label: string}>
|
|
*/
|
|
private function parseCountries(CountryRepository $countryRepository): array
|
|
{
|
|
return Options::fromList([0 => __('Select a Country')] + $countryRepository->lists()->toArray());
|
|
}
|
|
|
|
public function store(CreateUserRequest $request): RedirectResponse
|
|
{
|
|
// When user is created by administrator, we will set his
|
|
// status to Active by default.
|
|
$data = $request->all() + [
|
|
'status' => UserStatus::ACTIVE,
|
|
'email_verified_at' => now(),
|
|
];
|
|
|
|
$data['force_password_change'] = (bool) setting('password-change.enabled');
|
|
|
|
if (! data_get($data, 'country_id')) {
|
|
$data['country_id'] = null;
|
|
}
|
|
|
|
// Username should be updated only if it is provided.
|
|
if (! data_get($data, 'username')) {
|
|
$data['username'] = null;
|
|
}
|
|
|
|
$this->users->create($data);
|
|
|
|
return redirect()->route('users.index')
|
|
->withSuccess(__('User created successfully.'));
|
|
}
|
|
|
|
public function edit(User $user, CountryRepository $countryRepository, RoleRepository $roleRepository): InertiaResponse
|
|
{
|
|
return Inertia::render('users/Edit', [
|
|
'user' => UserPayload::form($user),
|
|
'countries' => $this->parseCountries($countryRepository),
|
|
'roles' => Options::fromList($roleRepository->lists()),
|
|
'statuses' => Options::fromList(UserStatus::lists()),
|
|
'avatarSources' => UserPayload::avatarSources(
|
|
$user,
|
|
$this->users->getUserSocialLogins($user->id)
|
|
),
|
|
'twoFactor' => UserPayload::twoFactor($user),
|
|
'showForcePasswordChange' => ! $user->is(auth()->user())
|
|
&& (bool) setting('password-change.enabled'),
|
|
// The two-factor controller flashes this to send the user back to
|
|
// the tab they were on.
|
|
'activeTab' => session('tab') === '2fa' ? '2fa' : 'details',
|
|
]);
|
|
}
|
|
|
|
public function destroy(User $user): RedirectResponse
|
|
{
|
|
if ($user->is(auth()->user())) {
|
|
return redirect()->route('users.index')
|
|
->withErrors(__('You cannot delete yourself.'));
|
|
}
|
|
|
|
$this->users->delete($user->id);
|
|
|
|
event(new Deleted($user));
|
|
|
|
return redirect()->route('users.index')
|
|
->withSuccess(__('User deleted successfully.'));
|
|
}
|
|
}
|