2021-05-05 07:46:14 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use BookStack\Auth\User;
|
|
|
|
use BookStack\Auth\UserRepo;
|
2022-02-03 07:33:26 -05:00
|
|
|
use Closure;
|
2021-05-05 07:46:14 -04:00
|
|
|
|
|
|
|
class UserApiController extends ApiController
|
|
|
|
{
|
|
|
|
protected $userRepo;
|
|
|
|
|
2022-02-03 07:33:26 -05:00
|
|
|
protected $fieldsToExpose = [
|
|
|
|
'email', 'created_at', 'updated_at', 'last_activity_at', 'external_auth_id'
|
2021-05-06 05:10:49 -04:00
|
|
|
];
|
|
|
|
|
2022-02-03 07:33:26 -05:00
|
|
|
protected $rules = [
|
|
|
|
'create' => [
|
|
|
|
],
|
|
|
|
'update' => [
|
|
|
|
],
|
|
|
|
];
|
2021-05-05 07:46:14 -04:00
|
|
|
|
2022-02-03 07:33:26 -05:00
|
|
|
public function __construct(UserRepo $userRepo)
|
2021-05-05 07:46:14 -04:00
|
|
|
{
|
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-03 07:33:26 -05:00
|
|
|
* Get a listing of users in the system.
|
|
|
|
* Requires permission to manage users.
|
2021-05-05 07:46:14 -04:00
|
|
|
*/
|
|
|
|
public function list()
|
|
|
|
{
|
2021-05-06 05:10:49 -04:00
|
|
|
$this->checkPermission('users-manage');
|
|
|
|
|
2022-02-03 07:33:26 -05:00
|
|
|
$users = $this->userRepo->getApiUsersBuilder();
|
2021-05-05 07:46:14 -04:00
|
|
|
|
|
|
|
return $this->apiListingResponse($users, [
|
2022-02-03 07:33:26 -05:00
|
|
|
'id', 'name', 'slug', 'email', 'external_auth_id',
|
2021-05-06 05:10:49 -04:00
|
|
|
'created_at', 'updated_at', 'last_activity_at',
|
2022-02-03 07:33:26 -05:00
|
|
|
], [Closure::fromCallable([$this, 'listFormatter'])]);
|
2021-05-06 05:10:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-03 07:33:26 -05:00
|
|
|
* View the details of a single user.
|
|
|
|
* Requires permission to manage users.
|
2021-05-06 05:10:49 -04:00
|
|
|
*/
|
|
|
|
public function read(string $id)
|
|
|
|
{
|
|
|
|
$this->checkPermission('users-manage');
|
|
|
|
|
|
|
|
$singleUser = $this->userRepo->getById($id);
|
2022-02-03 07:33:26 -05:00
|
|
|
$this->singleFormatter($singleUser);
|
2021-05-06 05:10:49 -04:00
|
|
|
|
|
|
|
return response()->json($singleUser);
|
2021-05-05 07:46:14 -04:00
|
|
|
}
|
2022-02-03 07:33:26 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the given user model for single-result display.
|
|
|
|
*/
|
|
|
|
protected function singleFormatter(User $user)
|
|
|
|
{
|
|
|
|
$this->listFormatter($user);
|
|
|
|
$user->load('roles:id,display_name');
|
|
|
|
$user->makeVisible(['roles']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the given user model for a listing multi-result display.
|
|
|
|
*/
|
|
|
|
protected function listFormatter(User $user)
|
|
|
|
{
|
|
|
|
$user->makeVisible($this->fieldsToExpose);
|
|
|
|
$user->setAttribute('profile_url', $user->getProfileUrl());
|
|
|
|
$user->setAttribute('edit_url', $user->getEditUrl());
|
|
|
|
$user->setAttribute('avatar_url', $user->getAvatar());
|
|
|
|
}
|
2021-05-05 07:46:14 -04:00
|
|
|
}
|