. * */ namespace OCA\Passman\Search; use OCA\Passman\AppInfo\Application; use OCA\Passman\Db\CredentialMapper; use OCA\Passman\Db\VaultMapper; use OCA\Passman\Service\VaultService; use OCA\Passman\Utility\Utils; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\IDBConnection; use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUser; use OCP\Search\IProvider; use OCP\Search\ISearchQuery; use OCP\Search\SearchResult; use OCP\Search\SearchResultEntry; use Safe\Exceptions\StringsException; class Provider implements IProvider { /** @var IL10N */ private IL10N $l10n; /** @var IURLGenerator */ private IURLGenerator $urlGenerator; private IDBConnection $db; public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, IDBConnection $db) { $this->l10n = $l10n; $this->urlGenerator = $urlGenerator; $this->db = $db; } public function getId(): string { return Application::APP_ID; } public function getName(): string { return $this->l10n->t('Passman'); } public function getOrder(string $route, array $routeParameters): int { if (strpos($route, Application::APP_ID . '.') === 0) { // Active app, prefer my results return -1; } return 25; } public function search(IUser $user, ISearchQuery $query): SearchResult { $VaultService = new VaultService(new VaultMapper($this->db, new Utils())); $Vaults = $VaultService->getByUser($user->getUID()); $CredentialMapper = new CredentialMapper($this->db, new Utils()); $searchResultEntries = []; foreach ($Vaults as $Vault) { try { $Credentials = $CredentialMapper->getCredentialsByVaultId($Vault->getId(), $Vault->getUserId()); foreach ($Credentials as $Credential) { if (strpos($Credential->getLabel(), $query->getTerm()) !== false) { try { $searchResultEntries[] = new SearchResultEntry( $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'), $Credential->getLabel(), \sprintf("Part of Passman vault %s", $Vault->getName()), $this->urlGenerator->linkToRoute('passman.page.index') . "#/vault/" . $Vault->getGuid() . "?show=" . $Credential->getGuid() ); } catch (Exception $e) { } } } } catch (DoesNotExistException $e) { } catch (MultipleObjectsReturnedException $e) { } } return SearchResult::complete( $this->l10n->t(Application::APP_ID), $searchResultEntries ); } }