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

203 lines
6.8 KiB
PHP
Raw Normal View History

2016-04-03 07:07:17 +02:00
<?php
/**
* UserController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-04-03 07:07:17 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://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;
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
{
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) {
2018-04-02 15:10:40 +02: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');
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)
{
2018-07-15 09:38:49 +02:00
$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.
*
2017-09-26 09:15:21 +02:00
* @param User $user
* @param UserRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function destroy(User $user, UserRepositoryInterface $repository)
{
$repository->destroy($user);
2018-04-22 17:10:11 +02:00
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
2018-04-02 15:10:40 +02: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 = [
2018-04-02 15:10:40 +02: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
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.
*
2016-04-03 07:07:17 +02:00
* @param UserRepositoryInterface $repository
*
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(UserRepositoryInterface $repository)
{
2018-04-02 15:10:40 +02:00
$subTitle = (string)trans('firefly.user_administration');
$subTitleIcon = 'fa-users';
$users = $repository->all();
2016-04-03 07:07:17 +02:00
// add meta stuff.
$users->each(
2018-07-08 07:59:58 +02:00
function (User $user) use ($repository) {
$list = ['twoFactorAuthEnabled', 'twoFactorAuthSecret'];
$preferences = app('preferences')->getArrayForUser($user, $list);
2018-07-08 07:59:58 +02:00
$user->isAdmin = $repository->hasRole($user, 'owner');
2018-01-11 20:56:42 +01:00
$is2faEnabled = 1 === $preferences['twoFactorAuthEnabled'];
2017-11-15 12:25:49 +01:00
$has2faSecret = null !== $preferences['twoFactorAuthSecret'];
2018-07-08 07:59:58 +02:00
$user->has2FA = ($is2faEnabled && $has2faSecret);
$user->prefs = $preferences;
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.
*
2016-10-15 07:11:53 +02:00
* @param UserRepositoryInterface $repository
* @param User $user
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(UserRepositoryInterface $repository, User $user)
{
2018-04-02 15:10:40 +02:00
$title = (string)trans('firefly.administration');
2016-10-22 09:39:31 +02:00
$mainTitleIcon = 'fa-hand-spock-o';
2018-04-02 15:10:40 +02:00
$subTitle = (string)trans('firefly.single_user_administration', ['email' => $user->email]);
2016-10-22 09:39:31 +02:00
$subTitleIcon = 'fa-user';
$information = $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.
*
2017-04-09 07:56:46 +02:00
* @param UserFormRequest $request
* @param User $user
* @param UserRepositoryInterface $repository
2016-12-15 22:56:31 +01:00
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2017-03-24 15:01:53 +01:00
public function update(UserFormRequest $request, User $user, UserRepositoryInterface $repository)
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']) {
2017-03-24 15:01:53 +01:00
$repository->changePassword($user, $data['password']);
2016-12-15 22:56:31 +01:00
}
2017-03-24 15:01:53 +01:00
$repository->changeStatus($user, $data['blocked'], $data['blocked_code']);
2017-09-26 09:15:21 +02:00
$repository->updateEmail($user, $data['email']);
2016-12-15 22:56:31 +01:00
2018-04-22 17:10:11 +02: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'));
2018-04-02 15:10:40 +02:00
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
}