firefly-iii/app/Api/V1/Controllers/System/UserController.php

210 lines
6.6 KiB
PHP
Raw Normal View History

2018-02-13 11:24:06 -06:00
<?php
2018-05-11 03:08:34 -05:00
2021-03-07 01:16:33 -06:00
/*
2018-02-13 11:24:06 -06:00
* UserController.php
2021-03-07 01:16:33 -06:00
* Copyright (c) 2021 james@firefly-iii.org
2018-02-13 11:24:06 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-13 11:24:06 -06: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 11:24:06 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-02-13 11:24:06 -06: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.
2018-02-13 11:24:06 -06: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 11:24:06 -06:00
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2018-02-13 11:24:06 -06:00
2021-03-07 01:16:33 -06:00
namespace FireflyIII\Api\V1\Controllers\System;
2018-02-13 11:24:06 -06:00
2021-03-07 01:16:33 -06:00
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\System\UserStoreRequest;
use FireflyIII\Api\V1\Requests\System\UserUpdateRequest;
2018-04-13 10:28:11 -05:00
use FireflyIII\Exceptions\FireflyException;
2018-02-13 11:24:06 -06:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Transformers\UserTransformer;
use FireflyIII\User;
2018-07-04 23:10:35 -05:00
use Illuminate\Http\JsonResponse;
2018-02-13 11:24:06 -06: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 10:28:11 -05:00
2018-02-13 11:24:06 -06:00
/**
* Class UserController.
2018-02-13 11:24:06 -06:00
*/
class UserController extends Controller
{
2020-07-30 23:19:48 -05:00
private UserRepositoryInterface $repository;
2018-02-13 11:24:06 -06:00
/**
* UserController constructor.
2019-09-04 10:39:39 -05:00
*
2018-02-13 11:24:06 -06:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(UserRepositoryInterface::class);
return $next($request);
}
);
}
/**
2021-09-19 10:22:16 -05:00
* This endpoint is documented at:
2023-02-11 23:53:36 -06:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/deleteUser
2021-09-19 10:22:16 -05:00
*
2018-02-13 11:24:06 -06:00
* Remove the specified resource from storage.
*
2023-06-21 05:34:58 -05:00
* @param User $user
2018-02-13 11:24:06 -06:00
*
2020-10-18 01:01:10 -05:00
* @return JsonResponse
2018-04-13 10:28:11 -05:00
* @throws FireflyException
2018-02-13 11:24:06 -06:00
*/
2021-03-07 01:16:33 -06:00
public function destroy(User $user): JsonResponse
2018-02-13 11:24:06 -06:00
{
2018-07-04 23:10:35 -05:00
/** @var User $admin */
$admin = auth()->user();
2021-03-21 03:15:40 -05:00
if ($admin->id === $user->id) {
2021-03-07 01:16:33 -06:00
return response()->json([], 500);
}
2022-03-30 13:09:19 -05:00
if ($this->repository->hasRole($admin, 'owner')) {
2018-02-13 11:24:06 -06:00
$this->repository->destroy($user);
return response()->json([], 204);
}
throw new FireflyException('200025: No access to function.');
2018-02-13 11:24:06 -06:00
}
/**
2021-09-19 10:22:16 -05:00
* This endpoint is documented at:
2023-02-11 23:53:36 -06:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/listUser
2021-09-19 10:22:16 -05:00
*
2018-02-13 11:24:06 -06:00
* Display a listing of the resource.
*
2018-07-04 23:10:35 -05:00
* @return JsonResponse
2021-05-24 01:50:17 -05:00
* @throws FireflyException
2018-02-13 11:24:06 -06:00
*/
2019-09-04 10:39:39 -05:00
public function index(): JsonResponse
2018-02-13 11:24:06 -06:00
{
2018-03-03 01:12:18 -06:00
// user preferences
2023-10-05 11:52:01 -05:00
$pageSize = $this->parameters->get('limit');
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-03-03 01:12:18 -06:00
// build collection
2018-02-13 11:24:06 -06: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'));
2023-06-21 05:34:58 -05:00
$paginator->setPath(route('api.v1.users.index') . $this->buildParams());
2018-02-13 11:24:06 -06:00
2018-03-03 01:12:18 -06:00
// make resource
2018-12-15 15:03:05 -06:00
/** @var UserTransformer $transformer */
$transformer = app(UserTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($users, $transformer, 'users');
2018-02-13 11:24:06 -06:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 09:51:44 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-02-13 11:24:06 -06:00
}
/**
2021-09-19 10:22:16 -05:00
* This endpoint is documented at:
2023-02-11 23:53:36 -06:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/getUser
2021-09-19 10:22:16 -05:00
*
* Show a single user.
*
2023-06-21 05:34:58 -05:00
* @param User $user
2018-02-13 11:24:06 -06:00
*
2018-07-04 23:10:35 -05:00
* @return JsonResponse
2018-02-13 11:24:06 -06:00
*/
2019-09-04 10:39:39 -05:00
public function show(User $user): JsonResponse
2018-02-13 11:24:06 -06:00
{
2018-03-03 01:12:18 -06:00
// make manager
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-03-03 01:12:18 -06:00
// make resource
2018-12-15 15:03:05 -06:00
/** @var UserTransformer $transformer */
$transformer = app(UserTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($user, $transformer, 'users');
2018-02-13 11:24:06 -06:00
2020-10-03 09:51:44 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-02-13 11:24:06 -06:00
}
/**
2021-09-19 10:22:16 -05:00
* This endpoint is documented at:
2023-02-11 23:53:36 -06:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/storeUser
2021-09-19 10:22:16 -05:00
*
* Store a new user.
*
2023-06-21 05:34:58 -05:00
* @param UserStoreRequest $request
2018-02-13 11:24:06 -06:00
*
2018-07-04 23:10:35 -05:00
* @return JsonResponse
2018-02-13 11:24:06 -06:00
*/
2019-08-22 10:06:43 -05:00
public function store(UserStoreRequest $request): JsonResponse
2018-02-13 11:24:06 -06:00
{
2019-09-04 10:39:39 -05:00
$data = $request->getAll();
$user = $this->repository->store($data);
$manager = $this->getManager();
2018-03-03 01:12:18 -06:00
// make resource
2018-12-15 15:03:05 -06:00
/** @var UserTransformer $transformer */
$transformer = app(UserTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($user, $transformer, 'users');
2018-03-03 01:12:18 -06:00
2020-10-03 09:51:44 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-02-13 11:24:06 -06:00
}
/**
2021-09-19 10:22:16 -05:00
* This endpoint is documented at:
2023-02-11 23:53:36 -06:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/updateUser
2021-09-19 10:22:16 -05:00
*
* Update a user.
*
2023-06-21 05:34:58 -05:00
* @param UserUpdateRequest $request
* @param User $user
2018-02-13 11:24:06 -06:00
*
2018-07-04 23:10:35 -05:00
* @return JsonResponse
2018-02-13 11:24:06 -06:00
*/
2019-08-22 10:06:43 -05:00
public function update(UserUpdateRequest $request, User $user): JsonResponse
2018-02-13 11:24:06 -06:00
{
2023-02-22 11:14:14 -06:00
$data = $request->getAll();
// can only update 'blocked' when user is admin.
2023-01-15 08:47:25 -06:00
if (!$this->repository->hasRole(auth()->user(), 'owner')) {
2023-10-29 00:33:43 -05:00
app('log')->debug('Quietly drop fields "blocked" and "blocked_code" from request.');
unset($data['blocked'], $data['blocked_code']);
}
2019-09-04 10:39:39 -05:00
$user = $this->repository->update($user, $data);
$manager = $this->getManager();
2018-03-03 01:12:18 -06:00
// make resource
2018-12-15 15:03:05 -06:00
/** @var UserTransformer $transformer */
$transformer = app(UserTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($user, $transformer, 'users');
2018-02-13 11:24:06 -06:00
2020-10-03 09:51:44 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-02-13 11:24:06 -06:00
}
2018-03-05 12:35:58 -06:00
}