Files
firefly-iii/app/Http/Controllers/Admin/UserController.php

205 lines
6.3 KiB
PHP
Raw Normal View History

2016-04-03 07:07:17 +02:00
<?php
/**
* 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
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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
*
* 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
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +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-04-03 07:07:17 +02:00
namespace FireflyIII\Http\Controllers\Admin;
use FireflyIII\Http\Controllers\Controller;
2017-12-19 19:25:50 +01:00
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Http\Middleware\IsSandStormUser;
2016-12-15 22:56:31 +01:00
use FireflyIII\Http\Requests\UserFormRequest;
2016-04-03 07:07:17 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2017-03-24 15:01:53 +01:00
use Log;
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
{
/** @var UserRepositoryInterface */
private $repository;
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) {
app('view')->share('title', (string) trans('firefly.administration'));
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-hand-spock-o');
$this->repository = app(UserRepositoryInterface::class);
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']);
$this->middleware(IsSandStormUser::class);
2016-12-09 07:08:20 +01:00
}
2016-08-04 06:07:53 +02:00
2017-09-26 09:15:21 +02:00
/**
2018-07-21 08:06:24 +02:00
* Delete a user.
*
2017-09-26 09:15:21 +02:00
* @param User $user
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function delete(User $user)
{
$subTitle = (string) trans('firefly.delete_user', ['email' => $user->email]);
2017-09-26 09:15:21 +02:00
return view('admin.users.delete', compact('user', 'subTitle'));
}
/**
2018-07-21 08:06:24 +02:00
* Destroy a user.
*
* @param User $user
2017-09-26 09:15:21 +02:00
*
* @return RedirectResponse|Redirector
2017-09-26 09:15:21 +02:00
*/
public function destroy(User $user)
2017-09-26 09:15:21 +02:00
{
$this->repository->destroy($user);
session()->flash('success', (string) trans('firefly.user_deleted'));
2017-09-26 09:15:21 +02:00
return redirect(route('admin.users'));
}
/**
2018-07-21 08:06:24 +02:00
* Edit user form.
*
2016-08-04 06:07:53 +02:00
* @param User $user
2016-08-12 15:10:03 +02:00
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2016-08-04 06:07:53 +02:00
public function edit(User $user)
{
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')) {
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('users.edit.uri');
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
$subTitle = (string) trans('firefly.edit_user', ['email' => $user->email]);
2016-12-09 07:08:20 +01:00
$subTitleIcon = 'fa-user-o';
2016-12-15 22:56:31 +01:00
$codes = [
'' => (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
2016-12-15 22:56:31 +01:00
return view('admin.users.edit', compact('user', 'subTitle', 'subTitleIcon', 'codes'));
}
2016-04-03 07:07:17 +02:00
/**
2018-07-21 08:06:24 +02:00
* Show index of user manager.
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2016-04-03 07:07:17 +02:00
*/
public function index()
2016-04-03 07:07:17 +02:00
{
$subTitle = (string) trans('firefly.user_administration');
$subTitleIcon = 'fa-users';
$users = $this->repository->all();
2016-04-03 07:07:17 +02:00
// add meta stuff.
$users->each(
function (User $user) {
$user->isAdmin = $this->repository->hasRole($user, 'owner');
$user->has2FA = null !== $user->mfa_secret;
2016-04-03 07:07:17 +02:00
}
);
2016-12-09 07:08:20 +01:00
return view('admin.users.index', compact('subTitle', 'subTitleIcon', 'users'));
2016-04-03 07:07:17 +02:00
}
2016-10-15 07:11:53 +02:00
/**
2018-07-21 08:06:24 +02:00
* Show single user.
*
* @param User $user
2016-10-15 07:11:53 +02:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(User $user)
2016-10-15 07:11:53 +02:00
{
$title = (string) trans('firefly.administration');
2016-10-22 09:39:31 +02:00
$mainTitleIcon = 'fa-hand-spock-o';
$subTitle = (string) trans('firefly.single_user_administration', ['email' => $user->email]);
2016-10-22 09:39:31 +02:00
$subTitleIcon = 'fa-user';
$information = $this->repository->getUserData($user);
2016-10-15 07:11:53 +02:00
2018-07-13 15:50:42 +02:00
return view(
'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.
*
* @param UserFormRequest $request
* @param User $user
2016-12-15 22:56:31 +01:00
*
* @return $this|RedirectResponse|Redirector
2016-12-15 22:56:31 +01: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();
// update password
2019-02-13 17:38:41 +01:00
if ('' !== $data['password']) {
$this->repository->changePassword($user, $data['password']);
2016-12-15 22:56:31 +01:00
}
$this->repository->changeStatus($user, $data['blocked'], $data['blocked_code']);
$this->repository->updateEmail($user, $data['email']);
2016-12-15 22:56:31 +01:00
session()->flash('success', (string) trans('firefly.updated_user', ['email' => $user->email]));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('users.edit.uri'));
if (1 === (int) $request->get('return_to_edit')) {
2017-03-24 15:01:53 +01:00
// @codeCoverageIgnoreStart
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]);
2017-03-24 15:01:53 +01:00
// @codeCoverageIgnoreEnd
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
}