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 */ 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.')); } }