Repository navigation
feat: allow exact UID lookup through Nextcloud global search #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,14 +114,84 @@ public function search(?IUser $actor, string $term, int $limit, int $offset): ar | |
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Inspect one user's populated fields via the global search provider. | ||
| * | ||
| * Each field is a separate paginated search result, so connectors that | ||
| * consume search snippets do not silently lose fields from a profile. | ||
| * This privileged lookup deliberately returns nothing to non-admins. | ||
| * | ||
| * @return array{total: int, items: list<array{ | ||
| * user_uid: string, | ||
| * display_name: string, | ||
| * matched_fields: list<array{field_key: string, field_label: string, value: string}> | ||
| * }>} | ||
| */ | ||
| public function searchByUserUid(?IUser $actor, string $userUid, int $limit, int $offset): array { | ||
| if ($limit < 1) { | ||
| throw new InvalidArgumentException('limit must be greater than 0'); | ||
| } | ||
| if ($offset < 0) { | ||
| throw new InvalidArgumentException('offset must be greater than or equal to 0'); | ||
| } | ||
|
|
||
| $actorUid = $actor?->getUID(); | ||
| if ($actorUid === null || !$this->groupManager->isAdmin($actorUid)) { | ||
| return ['total' => 0, 'items' => []]; | ||
| } | ||
|
|
||
| $userUid = trim($userUid); | ||
| if ($userUid === '') { | ||
| return ['total' => 0, 'items' => []]; | ||
| } | ||
| $user = $this->userManager->get($userUid); | ||
| if ($user === null) { | ||
| return ['total' => 0, 'items' => []]; | ||
| } | ||
|
|
||
| $userUid = $user->getUID(); | ||
| $valuesByDefinitionId = []; | ||
| foreach ($this->fieldValueMapper->findByUserUid($userUid) as $fieldValue) { | ||
| $valuesByDefinitionId[$fieldValue->getFieldDefinitionId()] = $fieldValue; | ||
| } | ||
|
|
||
| $items = []; | ||
| foreach ($this->fieldDefinitionService->findActiveOrdered() as $definition) { | ||
| $fieldValue = $valuesByDefinitionId[$definition->getId()] ?? null; | ||
| if ($fieldValue === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $scalarValue = $this->extractScalarValue($fieldValue); | ||
| if ($scalarValue === null || $scalarValue === '') { | ||
| continue; | ||
| } | ||
|
|
||
| $items[] = [ | ||
| 'user_uid' => $userUid, | ||
| 'display_name' => $this->resolveDisplayName($user, $userUid), | ||
| 'matched_fields' => [[ | ||
| 'field_key' => $definition->getFieldKey(), | ||
| 'field_label' => $definition->getLabel(), | ||
| 'value' => $scalarValue, | ||
| ]], | ||
| ]; | ||
| } | ||
|
|
||
| return [ | ||
| 'total' => count($items), | ||
| 'items' => array_slice($items, $offset, $limit), | ||
| ]; | ||
| } | ||
|
|
||
| private function extractScalarValue(FieldValue $value): ?string { | ||
| $decoded = json_decode($value->getValueJson(), true); | ||
| $scalar = $decoded['value'] ?? null; | ||
| if (is_array($scalar) || is_object($scalar) || $scalar === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return trim((string)$scalar); | ||
| return is_bool($scalar) ? ($scalar ? 'true' : 'false') : trim((string)$scalar); | ||
|
vitormattos marked this conversation as resolved.
Resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed this in my first comment: rendering booleans as words makes the regular search match them by substring. Since the minimum term length is 2, terms like Could boolean fields match only the whole word in $scalarValue = $this->extractScalarValue($value);
if ($scalarValue === null) {
continue;
}
$normalizedValue = mb_strtolower($scalarValue);
$matches = $definition->getType() === FieldType::BOOLEAN->value
? $normalizedValue === $normalizedTerm
: str_contains($normalizedValue, $normalizedTerm);
if (!$matches) {
continue;
}
|
||
| } | ||
|
|
||
| private function isSearchableForActor(FieldExposurePolicy $exposurePolicy, string $currentVisibility, bool $actorIsAdmin, bool $actorIsAuthenticated): bool { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.