Extend /users API endpoint

* add /users/{id} to get a single user
* add variable to print fields that are otherwise hidden (e.g. email)
This commit is contained in:
Jascha Sticher 2021-05-06 11:10:49 +02:00
parent 07626669da
commit 4cbd1a9eb5
5 changed files with 35 additions and 9 deletions

View file

@ -10,6 +10,7 @@ class ListingResponseBuilder
protected $query;
protected $request;
protected $fields;
protected $hiddenFields;
protected $filterOperators = [
'eq' => '=',
@ -24,11 +25,12 @@ class ListingResponseBuilder
/**
* ListingResponseBuilder constructor.
*/
public function __construct(Builder $query, Request $request, array $fields)
public function __construct(Builder $query, Request $request, array $fields, array $hiddenFields )
{
$this->query = $query;
$this->request = $request;
$this->fields = $fields;
$this->hiddenFields = $hiddenFields;
}
/**
@ -40,6 +42,7 @@ class ListingResponseBuilder
$total = $filteredQuery->count();
$data = $this->fetchData($filteredQuery);
$data = $data->makeVisible($this->hiddenFields);
return response()->json([
'data' => $data,

View file

@ -64,9 +64,11 @@ class UserRepo
/**
* Get all users as Builder for API
*/
public function getUsersBuilder(): Builder
public function getUsersBuilder(int $id = null ) : Builder
{
$query = User::query()->select(['*']);
$query = User::query()->select(['*'])
->withLastActivityAt()
->with(['roles', 'avatar']);
return $query;
}
/**

View file

@ -9,14 +9,15 @@ abstract class ApiController extends Controller
{
protected $rules = [];
protected $printHidden = [];
/**
* Provide a paginated listing JSON response in a standard format
* taking into account any pagination parameters passed by the user.
*/
protected function apiListingResponse(Builder $query, array $fields): JsonResponse
protected function apiListingResponse(Builder $query, array $fields, array $protectedFieldsToPrint = []): JsonResponse
{
$listing = new ListingResponseBuilder($query, request(), $fields);
$listing = new ListingResponseBuilder($query, request(), $fields, $protectedFieldsToPrint);
return $listing->toResponse();
}

View file

@ -13,6 +13,10 @@ class UserApiController extends ApiController
protected $user;
protected $userRepo;
protected $printHidden = [
'email', 'created_at', 'updated_at', 'last_activity_at'
];
# TBD: Endpoints to create / update users
# protected $rules = [
# 'create' => [
@ -28,15 +32,30 @@ class UserApiController extends ApiController
}
/**
* Get a listing of pages visible to the user.
* Get a listing of users
*/
public function list()
{
$this->checkPermission('users-manage');
$users = $this->userRepo->getUsersBuilder();
return $this->apiListingResponse($users, [
'id', 'name', 'slug',
'email', 'created_at', 'updated_at',
]);
'id', 'name', 'slug', 'email',
'created_at', 'updated_at', 'last_activity_at',
], $this->printHidden);
}
/**
* View the details of a single user
*/
public function read(string $id)
{
$this->checkPermission('users-manage');
$singleUser = $this->userRepo->getById($id);
$singleUser = $singleUser->makeVisible($this->printHidden);
return response()->json($singleUser);
}
}

View file

@ -46,3 +46,4 @@ Route::put('shelves/{id}', 'BookshelfApiController@update');
Route::delete('shelves/{id}', 'BookshelfApiController@delete');
Route::get('users', 'UserApiController@list');
Route::get('users/{id}', 'UserApiController@read');