2018-02-13 18:24:06 +01:00
|
|
|
<?php
|
2018-05-11 10:08:34 +02:00
|
|
|
|
2018-02-13 18:24:06 +01:00
|
|
|
/**
|
|
|
|
* UserController.php
|
2020-01-23 19:44:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-13 18:24:06 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-13 18:24:06 +01: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.
|
2018-02-13 18:24:06 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-13 18:24:06 +01: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.
|
2018-02-13 18:24:06 +01: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/>.
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
|
|
|
|
2018-05-11 10:08:34 +02:00
|
|
|
declare(strict_types=1);
|
2018-02-13 18:24:06 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Api\V1\Controllers;
|
|
|
|
|
2019-08-22 17:06:43 +02:00
|
|
|
use FireflyIII\Api\V1\Requests\UserStoreRequest;
|
|
|
|
use FireflyIII\Api\V1\Requests\UserUpdateRequest;
|
2018-04-13 17:28:11 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-02-13 18:24:06 +01:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
|
|
|
use FireflyIII\Transformers\UserTransformer;
|
|
|
|
use FireflyIII\User;
|
2018-07-05 06:10:35 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-02-13 18:24:06 +01:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
use League\Fractal\Resource\Collection as FractalCollection;
|
|
|
|
use League\Fractal\Resource\Item;
|
2018-04-13 17:28:11 +02:00
|
|
|
|
2018-02-13 18:24:06 +01:00
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* Class UserController.
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2020-07-31 06:19:48 +02:00
|
|
|
private UserRepositoryInterface $repository;
|
2018-02-13 18:24:06 +01:00
|
|
|
|
2020-07-31 09:24:08 +02:00
|
|
|
|
2018-02-13 18:24:06 +01:00
|
|
|
/**
|
|
|
|
* UserController constructor.
|
2019-09-04 17:39:39 +02:00
|
|
|
*
|
2019-04-12 04:53:18 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
/** @var UserRepositoryInterface repository */
|
|
|
|
$this->repository = app(UserRepositoryInterface::class);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2019-05-30 12:39:06 +02:00
|
|
|
* @param User $user
|
2018-02-13 18:24:06 +01:00
|
|
|
*
|
2020-10-18 08:01:10 +02:00
|
|
|
* @return JsonResponse
|
2018-04-13 17:28:11 +02:00
|
|
|
* @throws FireflyException
|
2019-05-30 12:39:06 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
2018-07-05 06:10:35 +02:00
|
|
|
public function delete(User $user): JsonResponse
|
2018-02-13 18:24:06 +01:00
|
|
|
{
|
2018-07-05 06:10:35 +02:00
|
|
|
/** @var User $admin */
|
|
|
|
$admin = auth()->user();
|
2018-12-03 07:18:05 +01:00
|
|
|
if ($admin->id !== $user->id && $this->repository->hasRole($admin, 'owner')) {
|
2018-02-13 18:24:06 +01:00
|
|
|
$this->repository->destroy($user);
|
|
|
|
|
|
|
|
return response()->json([], 204);
|
|
|
|
}
|
2019-11-02 08:19:50 +01:00
|
|
|
throw new FireflyException('200025: No access to function.'); // @codeCoverageIgnore
|
2018-02-13 18:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2018-07-05 06:10:35 +02:00
|
|
|
* @return JsonResponse
|
2019-05-30 12:39:06 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
2019-09-04 17:39:39 +02:00
|
|
|
public function index(): JsonResponse
|
2018-02-13 18:24:06 +01:00
|
|
|
{
|
2018-03-03 08:12:18 +01:00
|
|
|
// user preferences
|
2020-03-17 14:53:17 +01:00
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
2019-09-04 17:39:39 +02:00
|
|
|
$manager = $this->getManager();
|
2018-03-03 08:12:18 +01:00
|
|
|
|
|
|
|
// build collection
|
2018-02-13 18:24:06 +01:00
|
|
|
$collection = $this->repository->all();
|
|
|
|
$count = $collection->count();
|
|
|
|
$users = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
|
|
|
|
|
|
|
// make paginator:
|
|
|
|
$paginator = new LengthAwarePaginator($users, $count, $pageSize, $this->parameters->get('page'));
|
2018-03-03 08:12:18 +01:00
|
|
|
$paginator->setPath(route('api.v1.users.index') . $this->buildParams());
|
2018-02-13 18:24:06 +01:00
|
|
|
|
2018-03-03 08:12:18 +01:00
|
|
|
// make resource
|
2018-12-15 22:03:05 +01:00
|
|
|
/** @var UserTransformer $transformer */
|
|
|
|
$transformer = app(UserTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
|
|
|
$resource = new FractalCollection($users, $transformer, 'users');
|
2018-02-13 18:24:06 +01:00
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
|
|
|
|
2020-10-03 16:51:44 +02:00
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
2018-02-13 18:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* Show a single user.
|
|
|
|
*
|
2019-05-30 12:39:06 +02:00
|
|
|
* @param User $user
|
2018-02-13 18:24:06 +01:00
|
|
|
*
|
2018-07-05 06:10:35 +02:00
|
|
|
* @return JsonResponse
|
2019-05-30 12:39:06 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
2019-09-04 17:39:39 +02:00
|
|
|
public function show(User $user): JsonResponse
|
2018-02-13 18:24:06 +01:00
|
|
|
{
|
2018-03-03 08:12:18 +01:00
|
|
|
// make manager
|
2019-09-04 17:39:39 +02:00
|
|
|
$manager = $this->getManager();
|
2018-03-03 08:12:18 +01:00
|
|
|
// make resource
|
2018-12-15 22:03:05 +01:00
|
|
|
/** @var UserTransformer $transformer */
|
|
|
|
$transformer = app(UserTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
|
|
|
$resource = new Item($user, $transformer, 'users');
|
2018-02-13 18:24:06 +01:00
|
|
|
|
2020-10-03 16:51:44 +02:00
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
2018-02-13 18:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* Store a new user.
|
|
|
|
*
|
2019-08-22 17:06:43 +02:00
|
|
|
* @param UserStoreRequest $request
|
2018-02-13 18:24:06 +01:00
|
|
|
*
|
2018-07-05 06:10:35 +02:00
|
|
|
* @return JsonResponse
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
2019-08-22 17:06:43 +02:00
|
|
|
public function store(UserStoreRequest $request): JsonResponse
|
2018-02-13 18:24:06 +01:00
|
|
|
{
|
2019-09-04 17:39:39 +02:00
|
|
|
$data = $request->getAll();
|
|
|
|
$user = $this->repository->store($data);
|
|
|
|
$manager = $this->getManager();
|
2018-03-03 08:12:18 +01:00
|
|
|
|
|
|
|
// make resource
|
2018-12-15 22:03:05 +01:00
|
|
|
|
|
|
|
/** @var UserTransformer $transformer */
|
|
|
|
$transformer = app(UserTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
|
|
|
$resource = new Item($user, $transformer, 'users');
|
2018-03-03 08:12:18 +01:00
|
|
|
|
2020-10-03 16:51:44 +02:00
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
2018-02-13 18:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-06 07:15:42 +02:00
|
|
|
* Update a user.
|
|
|
|
*
|
2019-08-22 17:06:43 +02:00
|
|
|
* @param UserUpdateRequest $request
|
2019-09-04 17:39:39 +02:00
|
|
|
* @param User $user
|
2018-02-13 18:24:06 +01:00
|
|
|
*
|
2018-07-05 06:10:35 +02:00
|
|
|
* @return JsonResponse
|
2018-02-13 18:24:06 +01:00
|
|
|
*/
|
2019-08-22 17:06:43 +02:00
|
|
|
public function update(UserUpdateRequest $request, User $user): JsonResponse
|
2018-02-13 18:24:06 +01:00
|
|
|
{
|
2019-09-04 17:39:39 +02:00
|
|
|
$data = $request->getAll();
|
|
|
|
$user = $this->repository->update($user, $data);
|
|
|
|
$manager = $this->getManager();
|
2018-03-03 08:12:18 +01:00
|
|
|
// make resource
|
2018-12-15 22:03:05 +01:00
|
|
|
/** @var UserTransformer $transformer */
|
|
|
|
$transformer = app(UserTransformer::class);
|
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
|
|
|
$resource = new Item($user, $transformer, 'users');
|
2018-02-13 18:24:06 +01:00
|
|
|
|
2020-10-03 16:51:44 +02:00
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
|
2018-02-13 18:24:06 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-05 19:35:58 +01:00
|
|
|
}
|