Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use App\Http\Exceptions\HTTP403ForbiddenException;
use App\Http\Utils\HTMLCleaner;
use App\ModelSerializers\SerializerRegistry;
use App\ModelSerializers\SerializerUtils;
use Auth\Repositories\IUserRepository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request as LaravelRequest;
Expand Down Expand Up @@ -640,6 +641,27 @@ public function userInfo()
required: true,
schema: new OA\Schema(type: 'integer')
),
new OA\Parameter(
name: 'expand',
description: 'Expand relations: groups',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'fields',
description: 'Comma-separated list of scalar fields to return, e.g. first_name,last_name,pic,public_profile_allow_chat_with_me',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'relations',
description: 'Comma-separated list of relations to include (supported: groups)',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(
Expand All @@ -663,22 +685,19 @@ public function userInfo()
)]
public function get($id)
{
try {
return $this->processRequest(function () use ($id) {
$user = $this->repository->getById(intval($id));
if (is_null($user)) {
throw new EntityNotFoundException();
}
return $this->ok(SerializerRegistry::getInstance()->getSerializer($user, SerializerRegistry::SerializerType_Private)->serialize());
} catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412($ex1->getMessages());
} catch (EntityNotFoundException $ex2) {
Log::warning($ex2);
return $this->error404(['message' => $ex2->getMessage()]);
} catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
return $this->ok(SerializerRegistry::getInstance()
->getSerializer($user, SerializerRegistry::SerializerType_Private)
->serialize(
SerializerUtils::getExpand(),
SerializerUtils::getFields(),
SerializerUtils::getRelations()
));
});
}

/**
Expand Down Expand Up @@ -716,6 +735,20 @@ public function get($id)
required: false,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'fields',
description: 'Comma-separated list of scalar fields to return, e.g. first_name,last_name,pic,public_profile_allow_chat_with_me',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'relations',
description: 'Comma-separated list of relations to include (supported: groups)',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(
Expand Down Expand Up @@ -747,7 +780,9 @@ public function getV2($id)
return $this->ok(SerializerRegistry::getInstance()
->getSerializer($user, SerializerRegistry::SerializerType_Private)
->serialize(
Request::input("expand", '')
SerializerUtils::getExpand(),
SerializerUtils::getFields(),
SerializerUtils::getRelations()
));
});
}
Expand Down
20 changes: 14 additions & 6 deletions app/ModelSerializers/Auth/UserSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ final class PublicUserSerializer extends BaseUserSerializer
final class PrivateUserSerializer extends BaseUserSerializer
{

protected static $allowed_relations = [
'groups',
];

protected static $array_mappings = [
'Email' => 'email:json_string',
'Identifier' => 'identifier:json_string',
Expand Down Expand Up @@ -96,15 +100,19 @@ public function serialize($expand = null, array $fields = [], array $relations =
$user = $this->object;
if (!$user instanceof User) return [];

if (!count($relations)) $relations = $this->getAllowedRelations();

$values = parent::serialize($expand, $fields, $relations, $params);

$groups = [];
foreach ($user->getGroups() as $group) {
if (!$group instanceof Group) continue;
$groups[] = $group->getSlug();
}
if (in_array('groups', $relations)) {
$groups = [];
foreach ($user->getGroups() as $group) {
if (!$group instanceof Group) continue;
$groups[] = $group->getSlug();
}

$values['groups'] = $groups;
$values['groups'] = $groups;
}

if (!empty($expand)) {
$exp_expand = explode(',', $expand);
Expand Down
Loading
Loading