Files
grocy/controllers/UsersApiController.php

235 lines
7.0 KiB
PHP
Raw Normal View History

<?php
namespace Grocy\Controllers;
use Grocy\Controllers\Users\User;
class UsersApiController extends BaseApiController
{
2020-08-31 20:40:31 +02:00
public function AddPermission(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
2020-09-01 21:29:47 +02:00
try
{
2020-08-31 20:40:31 +02:00
User::checkPermission($request, User::PERMISSION_ADMIN);
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
2020-08-31 20:40:31 +02:00
$this->getDatabase()->user_permissions()->createRow([
'user_id' => $args['userId'],
'permission_id' => $requestBody['permission_id']
])->save();
return $this->EmptyApiResponse($response);
}
catch (\Slim\Exception\HttpSpecializedException $ex)
2018-07-25 19:28:15 +02:00
{
2020-08-31 20:40:31 +02:00
return $this->GenericErrorResponse($response, $ex->getMessage(), $ex->getCode());
2018-07-25 19:28:15 +02:00
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
2018-07-25 19:28:15 +02:00
}
}
public function CreateUser(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
User::checkPermission($request, User::PERMISSION_USERS_CREATE);
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
try
{
if ($requestBody === null)
{
throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)');
}
$this->getUsersService()->CreateUser($requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password'], $requestBody['picture_file_name']);
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function DeleteUser(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
User::checkPermission($request, User::PERMISSION_USERS_EDIT);
try
{
Convert services to singletons and use lazy loading to improve performance (#479) * use singletons to reduce need to recreate the same objects * unable to make the constructor private * comment out debug printing to log file * correct typo of treating self() as a var instead of a function * utilise Localisation service as a singleton * fix errent line that should have been commented * remove phpinfo * correct mistake in stock controller * try storing app in apcu * serialise inside the app closures * get timings for db-changed-time * get timings for db-changed-time * store localisation service in apcu * stor translations in apcu instead of localisation service (due to database connection) * correct syntax error * forgot to uncomment instance map * correct indentation and variable out of scope * more timings for app execution time * try apc caching for views * correct scope for Pot variable * remove additional fopen * correct timings for app build time * correct timings for app object build time * correct timings for app route build time * get timings for routing timings * get more in depth timings for routing loading * fix more in depth timings for routing loading * start investigating session auth middleware creation * start investigating session auth middleware creation * start investigating Login controller time * start investigating Login controller time * in depth look at Logincontroller timings * comment out debug printing * lazily obtain valus for page rendering * correct syntax error * correct scope of variable * correct visibiity of methds inherited from BaseController * missing use for Userfieldsservice * lazy loading of open api spec * lazy loading of users service * lazy loading of batteries service * lazy loading of services in controllers * lazy loading of services in services * correct mistake * fix userservice * fix userservice * fix userfieldservice * fix chores service * fix calendar service * remove Dockerfile used for development * Remove docker compose file used for development * Clean up app.php * remove last diff * Clean up base controller * Clean up controllers * lean up middleware * Clean up and tuen all services into singletons * remove debug from routes.php * remove acpu from localisation * Complete removal of acpu from localisation * fixes for things broken * More fixes following merge * Fix for start up bug. Re factoring singleton code had brroken due to scope of clas var. * fix bug where getUsersService is declared twice * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * Fix all the not working things... * Deleted off-topic files * Deleted off-topic files Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-03-01 23:47:47 +07:00
$this->getUsersService()->DeleteUser($args['userId']);
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function EditUser(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
2020-08-31 20:40:31 +02:00
if ($args['userId'] == GROCY_USER_ID)
{
User::checkPermission($request, User::PERMISSION_USERS_EDIT_SELF);
2020-08-31 20:40:31 +02:00
}
else
{
User::checkPermission($request, User::PERMISSION_USERS_EDIT);
}
2020-08-31 20:40:31 +02:00
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
try
{
$this->getUsersService()->EditUser($args['userId'], $requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password'], $requestBody['picture_file_name']);
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2020-08-31 20:40:31 +02:00
public function GetUserSetting(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
try
{
2020-08-31 20:40:31 +02:00
$value = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, $args['settingKey']);
return $this->ApiResponse($response, ['value' => $value]);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2020-08-31 20:40:31 +02:00
public function GetUserSettings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
try
{
2020-08-31 20:40:31 +02:00
return $this->ApiResponse($response, $this->getUsersService()->GetUserSettings(GROCY_USER_ID));
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2020-08-31 20:40:31 +02:00
public function GetUsers(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
2020-08-31 20:40:31 +02:00
User::checkPermission($request, User::PERMISSION_USERS_READ);
try
{
return $this->FilteredApiResponse($response, $this->getUsersService()->GetUsersAsDto(), $request->getQueryParams());
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function CurrentUser(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
try
{
return $this->ApiResponse($response, $this->getUsersService()->GetUsersAsDto()->where('id', GROCY_USER_ID));
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function ListPermissions(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
2020-09-01 21:29:47 +02:00
try
{
User::checkPermission($request, User::PERMISSION_ADMIN);
2020-09-01 21:29:47 +02:00
return $this->ApiResponse(
$response,
$this->getDatabase()->user_permissions()->where('user_id', $args['userId'])
);
2020-08-31 20:40:31 +02:00
}
catch (\Slim\Exception\HttpSpecializedException $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage(), $ex->getCode());
2020-08-31 20:40:31 +02:00
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function SetPermissions(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
2020-09-01 21:29:47 +02:00
try
{
User::checkPermission($request, User::PERMISSION_ADMIN);
$requestBody = $request->getParsedBody();
$db = $this->getDatabase();
$db->user_permissions()
->where('user_id', $args['userId'])
->delete();
$perms = [];
if (GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease')
2020-08-31 20:40:31 +02:00
{
// For demo mode always all users have and keep the ADMIN permission
2020-08-31 20:40:31 +02:00
$perms[] = [
'user_id' => $args['userId'],
'permission_id' => 1
2020-08-31 20:40:31 +02:00
];
}
else
{
foreach ($requestBody['permissions'] as $perm_id)
{
$perms[] = [
'user_id' => $args['userId'],
'permission_id' => $perm_id
];
}
}
$db->insert('user_permissions', $perms, 'batch');
return $this->EmptyApiResponse($response);
2020-08-31 20:40:31 +02:00
}
catch (\Slim\Exception\HttpSpecializedException $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage(), $ex->getCode());
2020-08-31 20:40:31 +02:00
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function SetUserSetting(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
try
{
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
2020-08-31 20:40:31 +02:00
$value = $this->getUsersService()->SetUserSetting(GROCY_USER_ID, $args['settingKey'], $requestBody['value']);
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
2020-08-31 20:40:31 +02:00
public function DeleteUserSetting(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
try
{
$value = $this->getUsersService()->DeleteUserSetting(GROCY_USER_ID, $args['settingKey']);
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
}