Files
panel/app/Http/Controllers/Api/Application/Users/UserController.php

149 lines
4.8 KiB
PHP
Raw Normal View History

2017-11-19 16:30:00 -06:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Application\Users;
2017-11-19 16:30:00 -06:00
2024-03-12 22:39:16 -04:00
use App\Models\User;
use Illuminate\Http\JsonResponse;
2020-09-13 11:55:39 -07:00
use Spatie\QueryBuilder\QueryBuilder;
2024-03-12 22:39:16 -04:00
use App\Services\Users\UserUpdateService;
use App\Services\Users\UserCreationService;
use App\Transformers\Api\Application\UserTransformer;
use App\Http\Requests\Api\Application\Users\GetUsersRequest;
use App\Http\Requests\Api\Application\Users\StoreUserRequest;
use App\Http\Requests\Api\Application\Users\DeleteUserRequest;
use App\Http\Requests\Api\Application\Users\UpdateUserRequest;
use App\Http\Controllers\Api\Application\ApplicationApiController;
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
use App\Http\Requests\Api\Application\Users\AssignUserRolesRequest;
use App\Models\Role;
class UserController extends ApplicationApiController
2017-11-19 16:30:00 -06:00
{
/**
* UserController constructor.
*/
public function __construct(
private UserCreationService $creationService,
private UserUpdateService $updateService
2017-11-19 16:30:00 -06:00
) {
parent::__construct();
2017-11-19 16:30:00 -06:00
}
/**
* Handle request to list all users on the panel. Returns a JSON-API representation
* of a collection of users including any defined relations passed in
* the request.
2017-11-19 16:30:00 -06:00
*/
public function index(GetUsersRequest $request): array
2017-11-19 16:30:00 -06:00
{
2020-09-13 11:55:39 -07:00
$users = QueryBuilder::for(User::query())
->allowedFilters(['email', 'uuid', 'username', 'external_id'])
->allowedSorts(['id', 'uuid'])
->paginate($request->query('per_page') ?? 50);
2017-11-19 16:30:00 -06:00
2018-01-03 21:14:53 -06:00
return $this->fractal->collection($users)
->transformWith($this->getTransformer(UserTransformer::class))
2018-01-03 21:14:53 -06:00
->toArray();
}
/**
* Handle a request to view a single user. Includes any relations that
* were defined in the request.
*/
2020-09-13 11:55:39 -07:00
public function view(GetUsersRequest $request, User $user): array
{
2020-09-13 11:55:39 -07:00
return $this->fractal->item($user)
->transformWith($this->getTransformer(UserTransformer::class))
2018-01-03 21:14:53 -06:00
->toArray();
2017-11-19 16:30:00 -06:00
}
/**
* Update an existing user on the system and return the response. Returns the
* updated user model response on success. Supports handling of token revocation
* errors when switching a user from an admin to a normal user.
*
* Revocation errors are returned under the 'revocation_errors' key in the response
* meta. If there are no errors this is an empty array.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Model\DataValidationException
*/
2020-06-25 21:42:21 -07:00
public function update(UpdateUserRequest $request, User $user): array
{
$this->updateService->setUserLevel(User::USER_LEVEL_ADMIN);
2020-06-25 21:42:21 -07:00
$user = $this->updateService->handle($user, $request->validated());
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
$response = $this->fractal->item($user)
->transformWith($this->getTransformer(UserTransformer::class));
return $response->toArray();
}
/**
* Assign roles to a user.
*/
public function assignRoles(AssignUserRolesRequest $request, User $user): array
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
{
foreach ($request->input('roles') as $role) {
if ($role === Role::getRootAdmin()->id) {
continue;
}
$user->assignRole($role);
}
$response = $this->fractal->item($user)
->transformWith($this->getTransformer(UserTransformer::class));
return $response->toArray();
}
/**
* Removes roles from a user.
*/
public function removeRoles(AssignUserRolesRequest $request, User $user): array
{
foreach ($request->input('roles') as $role) {
if ($role === Role::getRootAdmin()->id) {
continue;
}
$user->removeRole($role);
}
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
2020-06-25 21:42:21 -07:00
$response = $this->fractal->item($user)
->transformWith($this->getTransformer(UserTransformer::class));
return $response->toArray();
}
/**
* Store a new user on the system. Returns the created user and an HTTP/201
* header on successful creation.
*
* @throws \Exception
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Model\DataValidationException
*/
public function store(StoreUserRequest $request): JsonResponse
{
$user = $this->creationService->handle($request->validated());
return $this->fractal->item($user)
->transformWith($this->getTransformer(UserTransformer::class))
->addMeta([
'resource' => route('api.application.users.view', [
'user' => $user->id,
]),
])
->respond(201);
}
/**
2024-03-18 21:23:13 -04:00
* Handle a request to delete a user from the Panel. Returns a HTTP/204 response on successful deletion.
*/
2020-09-13 11:55:39 -07:00
public function delete(DeleteUserRequest $request, User $user): JsonResponse
{
2024-03-18 21:23:13 -04:00
$user->delete();
2020-09-13 11:55:39 -07:00
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
2017-11-19 16:30:00 -06:00
}