2016-04-03 07:07:17 +02:00
|
|
|
<?php
|
2022-12-29 19:41:57 +01:00
|
|
|
|
2016-04-03 07:07:17 +02:00
|
|
|
/**
|
|
|
|
* UserController.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-04-03 07:07:17 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-04-03 07:07:17 +02:00
|
|
|
*/
|
2017-03-24 15:01:53 +01:00
|
|
|
declare(strict_types=1);
|
2016-05-20 12:27:31 +02:00
|
|
|
|
2016-04-03 07:07:17 +02:00
|
|
|
namespace FireflyIII\Http\Controllers\Admin;
|
|
|
|
|
2022-10-01 12:21:42 +02:00
|
|
|
use FireflyIII\Events\Admin\InvitationCreated;
|
2016-04-03 07:07:17 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2017-12-19 19:25:50 +01:00
|
|
|
use FireflyIII\Http\Middleware\IsDemoUser;
|
2022-10-01 12:21:42 +02:00
|
|
|
use FireflyIII\Http\Requests\InviteUserFormRequest;
|
2016-12-15 22:56:31 +01:00
|
|
|
use FireflyIII\Http\Requests\UserFormRequest;
|
2023-03-09 06:33:23 +01:00
|
|
|
use FireflyIII\Models\InvitedUser;
|
2016-04-03 07:07:17 +02:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
|
|
|
use FireflyIII\User;
|
2021-03-28 11:46:23 +02:00
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
use Illuminate\Contracts\View\Factory;
|
2023-03-09 06:33:23 +01:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-03-17 15:01:00 +01:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Routing\Redirector;
|
2023-04-01 07:04:42 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-05-29 13:56:55 +02:00
|
|
|
use Illuminate\View\View;
|
2023-02-22 18:03:31 +01:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2016-04-03 07:07:17 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class UserController.
|
2016-04-03 07:07:17 +02:00
|
|
|
*/
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2020-09-23 06:18:43 +02:00
|
|
|
protected bool $externalIdentity;
|
2021-03-28 11:46:23 +02:00
|
|
|
private UserRepositoryInterface $repository;
|
2019-06-23 11:13:36 +02:00
|
|
|
|
2016-12-09 07:08:20 +01:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* UserController constructor.
|
2016-12-09 07:08:20 +01:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2022-12-29 19:41:57 +01:00
|
|
|
app('view')->share('title', (string)trans('firefly.administration'));
|
2017-12-16 19:46:36 +01:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-hand-spock-o');
|
2019-06-23 11:13:36 +02:00
|
|
|
$this->repository = app(UserRepositoryInterface::class);
|
2020-03-17 15:01:00 +01:00
|
|
|
|
2016-12-09 07:08:20 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2017-12-19 19:25:50 +01:00
|
|
|
$this->middleware(IsDemoUser::class)->except(['index', 'show']);
|
2023-06-11 18:18:46 +02:00
|
|
|
$this->externalIdentity = 'web' !== config('firefly.authentication_guard');
|
2016-12-09 07:08:20 +01:00
|
|
|
}
|
2016-08-04 06:07:53 +02:00
|
|
|
|
2017-09-26 09:15:21 +02:00
|
|
|
/**
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param User $user
|
2021-03-21 09:15:40 +01:00
|
|
|
*
|
2021-03-28 11:46:23 +02:00
|
|
|
* @return Application|Factory|RedirectResponse|Redirector|View
|
2017-09-26 09:15:21 +02:00
|
|
|
*/
|
|
|
|
public function delete(User $user)
|
|
|
|
{
|
2020-09-23 06:18:43 +02:00
|
|
|
if ($this->externalIdentity) {
|
|
|
|
request()->session()->flash('error', trans('firefly.external_user_mgt_disabled'));
|
|
|
|
|
|
|
|
return redirect(route('admin.users'));
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
$subTitle = (string)trans('firefly.delete_user', ['email' => $user->email]);
|
2017-09-26 09:15:21 +02:00
|
|
|
|
2022-01-29 14:11:12 +01:00
|
|
|
return view('admin.users.delete', compact('user', 'subTitle'));
|
2017-09-26 09:15:21 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 06:33:23 +01:00
|
|
|
/**
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param InvitedUser $invitedUser
|
2023-07-15 16:02:42 +02:00
|
|
|
*
|
2023-07-04 13:29:19 +02:00
|
|
|
* @return JsonResponse
|
2023-03-09 06:33:23 +01:00
|
|
|
*/
|
|
|
|
public function deleteInvite(InvitedUser $invitedUser): JsonResponse
|
|
|
|
{
|
|
|
|
Log::debug('Will now delete invitation');
|
|
|
|
if ($invitedUser->redeemed) {
|
|
|
|
Log::debug('Is already redeemed.');
|
|
|
|
session()->flash('error', trans('firefly.invite_is_already_redeemed', ['address' => $invitedUser->email]));
|
|
|
|
return response()->json(['success' => false]);
|
|
|
|
}
|
|
|
|
Log::debug('Delete!');
|
|
|
|
session()->flash('success', trans('firefly.invite_is_deleted', ['address' => $invitedUser->email]));
|
|
|
|
$this->repository->deleteInvite($invitedUser);
|
|
|
|
return response()->json(['success' => true]);
|
|
|
|
}
|
|
|
|
|
2017-09-26 09:15:21 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Destroy a user.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param User $user
|
2017-09-26 09:15:21 +02:00
|
|
|
*
|
2020-03-17 15:01:00 +01:00
|
|
|
* @return RedirectResponse|Redirector
|
2017-09-26 09:15:21 +02:00
|
|
|
*/
|
2019-06-23 11:13:36 +02:00
|
|
|
public function destroy(User $user)
|
2017-09-26 09:15:21 +02:00
|
|
|
{
|
2020-09-23 06:18:43 +02:00
|
|
|
if ($this->externalIdentity) {
|
|
|
|
request()->session()->flash('error', trans('firefly.external_user_mgt_disabled'));
|
|
|
|
|
|
|
|
return redirect(route('admin.users'));
|
|
|
|
}
|
2019-06-23 11:13:36 +02:00
|
|
|
$this->repository->destroy($user);
|
2022-12-29 19:41:57 +01:00
|
|
|
session()->flash('success', (string)trans('firefly.user_deleted'));
|
2017-09-26 09:15:21 +02:00
|
|
|
|
|
|
|
return redirect(route('admin.users'));
|
|
|
|
}
|
|
|
|
|
2016-07-24 18:47:55 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Edit user form.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param User $user
|
2016-08-12 15:10:03 +02:00
|
|
|
*
|
2021-03-28 11:46:23 +02:00
|
|
|
* @return Factory|View
|
2016-07-24 18:47:55 +02:00
|
|
|
*/
|
2016-08-04 06:07:53 +02:00
|
|
|
public function edit(User $user)
|
2016-07-24 18:47:55 +02:00
|
|
|
{
|
2020-09-23 06:18:43 +02:00
|
|
|
$canEditDetails = true;
|
|
|
|
if ($this->externalIdentity) {
|
|
|
|
$canEditDetails = false;
|
|
|
|
}
|
2016-12-15 22:56:31 +01:00
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
2017-11-15 12:25:49 +01:00
|
|
|
if (true !== session('users.edit.fromUpdate')) {
|
2022-04-12 18:19:30 +02:00
|
|
|
$this->rememberPreviousUrl('users.edit.url');
|
2016-12-15 22:56:31 +01:00
|
|
|
}
|
2018-04-22 17:12:22 +02:00
|
|
|
session()->forget('users.edit.fromUpdate');
|
2016-12-15 22:56:31 +01:00
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
$subTitle = (string)trans('firefly.edit_user', ['email' => $user->email]);
|
2016-12-09 07:08:20 +01:00
|
|
|
$subTitleIcon = 'fa-user-o';
|
2020-03-23 17:54:49 +01:00
|
|
|
$currentUser = auth()->user();
|
|
|
|
$isAdmin = $this->repository->hasRole($user, 'owner');
|
2016-12-15 22:56:31 +01:00
|
|
|
$codes = [
|
2022-12-29 19:41:57 +01:00
|
|
|
'' => (string)trans('firefly.no_block_code'),
|
|
|
|
'bounced' => (string)trans('firefly.block_code_bounced'),
|
|
|
|
'expired' => (string)trans('firefly.block_code_expired'),
|
|
|
|
'email_changed' => (string)trans('firefly.block_code_email_changed'),
|
2016-12-15 22:56:31 +01:00
|
|
|
];
|
2016-12-09 07:08:20 +01:00
|
|
|
|
2022-01-29 14:11:12 +01:00
|
|
|
return view('admin.users.edit', compact('user', 'canEditDetails', 'subTitle', 'subTitleIcon', 'codes', 'currentUser', 'isAdmin'));
|
2016-07-24 18:47:55 +02:00
|
|
|
}
|
|
|
|
|
2016-04-03 07:07:17 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Show index of user manager.
|
|
|
|
*
|
2021-03-28 11:46:23 +02:00
|
|
|
* @return Factory|View
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
2016-04-03 07:07:17 +02:00
|
|
|
*/
|
2019-06-23 11:13:36 +02:00
|
|
|
public function index()
|
2016-04-03 07:07:17 +02:00
|
|
|
{
|
2022-12-29 19:41:57 +01:00
|
|
|
$subTitle = (string)trans('firefly.user_administration');
|
2022-10-01 12:21:42 +02:00
|
|
|
$subTitleIcon = 'fa-users';
|
|
|
|
$users = $this->repository->all();
|
|
|
|
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
|
|
|
|
$allowInvites = false;
|
|
|
|
if (!$this->externalIdentity && $singleUserMode) {
|
|
|
|
// also registration enabled.
|
|
|
|
$allowInvites = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invitedUsers = $this->repository->getInvitedUsers();
|
2016-04-03 07:07:17 +02:00
|
|
|
|
|
|
|
// add meta stuff.
|
|
|
|
$users->each(
|
2019-06-23 11:13:36 +02:00
|
|
|
function (User $user) {
|
|
|
|
$user->isAdmin = $this->repository->hasRole($user, 'owner');
|
2019-08-04 10:27:37 +02:00
|
|
|
$user->has2FA = null !== $user->mfa_secret;
|
2016-04-03 07:07:17 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-10-01 12:21:42 +02:00
|
|
|
return view('admin.users.index', compact('subTitle', 'subTitleIcon', 'users', 'allowInvites', 'invitedUsers'));
|
2016-04-03 07:07:17 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
/**
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param InviteUserFormRequest $request
|
2023-07-15 16:02:42 +02:00
|
|
|
*
|
2022-12-29 19:41:57 +01:00
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function invite(InviteUserFormRequest $request): RedirectResponse
|
|
|
|
{
|
|
|
|
$address = (string)$request->get('invited_user');
|
|
|
|
$invitee = $this->repository->inviteUser(auth()->user(), $address);
|
|
|
|
session()->flash('info', trans('firefly.user_is_invited', ['address' => $address]));
|
|
|
|
|
|
|
|
// event!
|
|
|
|
event(new InvitationCreated($invitee));
|
|
|
|
|
|
|
|
return redirect(route('admin.users'));
|
|
|
|
}
|
|
|
|
|
2016-10-15 07:11:53 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Show single user.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param User $user
|
2016-10-15 07:11:53 +02:00
|
|
|
*
|
2021-03-28 11:46:23 +02:00
|
|
|
* @return Factory|View
|
2016-10-15 07:11:53 +02:00
|
|
|
*/
|
2019-06-23 11:13:36 +02:00
|
|
|
public function show(User $user)
|
2016-10-15 07:11:53 +02:00
|
|
|
{
|
2022-12-29 19:41:57 +01:00
|
|
|
$title = (string)trans('firefly.administration');
|
2016-10-22 09:39:31 +02:00
|
|
|
$mainTitleIcon = 'fa-hand-spock-o';
|
2022-12-29 19:41:57 +01:00
|
|
|
$subTitle = (string)trans('firefly.single_user_administration', ['email' => $user->email]);
|
2016-10-22 09:39:31 +02:00
|
|
|
$subTitleIcon = 'fa-user';
|
2019-06-23 11:13:36 +02:00
|
|
|
$information = $this->repository->getUserData($user);
|
2016-10-15 07:11:53 +02:00
|
|
|
|
2022-01-29 14:11:12 +01:00
|
|
|
return view(
|
2020-03-17 15:01:00 +01:00
|
|
|
'admin.users.show',
|
|
|
|
compact(
|
|
|
|
'title',
|
|
|
|
'mainTitleIcon',
|
|
|
|
'subTitle',
|
|
|
|
'subTitleIcon',
|
|
|
|
'information',
|
|
|
|
'user'
|
|
|
|
)
|
2016-10-15 07:11:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-15 22:56:31 +01:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Update single user.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param UserFormRequest $request
|
|
|
|
* @param User $user
|
2016-12-15 22:56:31 +01:00
|
|
|
*
|
2020-03-17 15:01:00 +01:00
|
|
|
* @return $this|RedirectResponse|Redirector
|
2016-12-15 22:56:31 +01:00
|
|
|
*/
|
2019-06-23 11:13:36 +02:00
|
|
|
public function update(UserFormRequest $request, User $user)
|
2016-12-15 22:56:31 +01:00
|
|
|
{
|
2017-03-24 15:01:53 +01:00
|
|
|
Log::debug('Actually here');
|
2016-12-15 22:56:31 +01:00
|
|
|
$data = $request->getUserData();
|
|
|
|
|
2021-03-15 19:51:55 +01:00
|
|
|
//var_dump($data);
|
2020-09-23 06:18:43 +02:00
|
|
|
|
2016-12-15 22:56:31 +01:00
|
|
|
// update password
|
2020-09-23 06:18:43 +02:00
|
|
|
if (array_key_exists('password', $data) && '' !== $data['password']) {
|
2019-06-23 11:13:36 +02:00
|
|
|
$this->repository->changePassword($user, $data['password']);
|
2016-12-15 22:56:31 +01:00
|
|
|
}
|
2020-03-23 17:54:49 +01:00
|
|
|
if (true === $data['is_owner']) {
|
|
|
|
$this->repository->attachRole($user, 'owner');
|
|
|
|
session()->flash('info', trans('firefly.give_admin_careful'));
|
|
|
|
}
|
2020-03-24 05:57:04 +01:00
|
|
|
if (false === $data['is_owner'] && $user->id !== auth()->user()->id) {
|
2020-03-23 17:54:49 +01:00
|
|
|
$this->repository->removeRole($user, 'owner');
|
|
|
|
}
|
2016-12-15 22:56:31 +01:00
|
|
|
|
2019-06-23 11:13:36 +02:00
|
|
|
$this->repository->changeStatus($user, $data['blocked'], $data['blocked_code']);
|
|
|
|
$this->repository->updateEmail($user, $data['email']);
|
2016-12-15 22:56:31 +01:00
|
|
|
|
2022-12-29 19:41:57 +01:00
|
|
|
session()->flash('success', (string)trans('firefly.updated_user', ['email' => $user->email]));
|
2018-07-08 12:08:53 +02:00
|
|
|
app('preferences')->mark();
|
2022-04-12 18:19:30 +02:00
|
|
|
$redirect = redirect($this->getPreviousUrl('users.edit.url'));
|
2022-12-29 19:41:57 +01:00
|
|
|
if (1 === (int)$request->get('return_to_edit')) {
|
2018-04-22 17:12:22 +02:00
|
|
|
session()->put('users.edit.fromUpdate', true);
|
2016-12-15 22:56:31 +01:00
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
$redirect = redirect(route('admin.users.edit', [$user->id]))->withInput(['return_to_edit' => 1]);
|
2016-12-15 22:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// redirect to previous URL.
|
2018-07-08 12:08:53 +02:00
|
|
|
return $redirect;
|
2016-12-15 22:56:31 +01:00
|
|
|
}
|
2016-04-08 17:54:25 +02:00
|
|
|
}
|