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

194 lines
6.3 KiB
PHP
Raw Normal View History

2018-02-13 18:24:06 +01:00
<?php
2018-05-11 10:08:34 +02:00
2021-03-07 08:16:33 +01:00
/*
2018-02-13 18:24:06 +01:00
* UserController.php
2021-03-07 08:16:33 +01:00
* Copyright (c) 2021 james@firefly-iii.org
2018-02-13 18:24:06 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-13 18:24:06 +01: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
*
* 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
* GNU Affero General Public License for more details.
2018-02-13 18:24:06 +01: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
2021-03-07 08:16:33 +01:00
namespace FireflyIII\Api\V1\Controllers\System;
2018-02-13 18:24:06 +01:00
2021-03-07 08:16:33 +01:00
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\System\UserStoreRequest;
use FireflyIII\Api\V1\Requests\System\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
/**
* 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
/**
* UserController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(UserRepositoryInterface::class);
return $next($request);
}
);
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/deleteUser
2021-09-19 17:22:16 +02:00
*
2018-02-13 18:24:06 +01:00
* Remove the specified resource from storage.
*
2018-04-13 17:28:11 +02:00
* @throws FireflyException
2018-02-13 18:24:06 +01:00
*/
2021-03-07 08:16:33 +01:00
public function destroy(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();
2021-03-21 09:15:40 +01:00
if ($admin->id === $user->id) {
2021-03-07 08:16:33 +01:00
return response()->json([], 500);
}
2022-03-30 20:09:19 +02:00
if ($this->repository->hasRole($admin, 'owner')) {
2018-02-13 18:24:06 +01:00
$this->repository->destroy($user);
return response()->json([], 204);
}
2023-12-20 19:35:52 +01:00
throw new FireflyException('200025: No access to function.');
2018-02-13 18:24:06 +01:00
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/listUser
2021-09-19 17:22:16 +02:00
*
2018-02-13 18:24:06 +01:00
* Display a listing of the resource.
*
2021-05-24 08:50:17 +02:00
* @throws FireflyException
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
$pageSize = $this->parameters->get('limit');
$manager = $this->getManager();
2018-03-03 08:12:18 +01:00
// build collection
$collection = $this->repository->all();
$count = $collection->count();
$users = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
2018-02-13 18:24:06 +01:00
// make paginator:
$paginator = new LengthAwarePaginator($users, $count, $pageSize, $this->parameters->get('page'));
2023-12-20 19:35:52 +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
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/getUser
2021-09-19 17:22:16 +02:00
*
* Show a single user.
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
$manager = $this->getManager();
2023-12-20 19:35:52 +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 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
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/storeUser
2021-09-19 17:22:16 +02:00
*
* Store a new user.
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
{
$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
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/users/updateUser
2021-09-19 17:22:16 +02:00
*
* Update a user.
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
{
$data = $request->getAll();
// can only update 'blocked' when user is admin.
2023-01-15 15:47:25 +01:00
if (!$this->repository->hasRole(auth()->user(), 'owner')) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Quietly drop fields "blocked" and "blocked_code" from request.');
unset($data['blocked'], $data['blocked_code']);
}
$user = $this->repository->update($user, $data);
$manager = $this->getManager();
2023-12-20 19:35:52 +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 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
}