From 605206b72120d20616e9ec1aef67e9880281b4b0 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Tue, 25 Jun 2024 12:07:43 +0200 Subject: [PATCH] fix: handle missing display name for single user reports - Load the display name if requested - Let getUserDisplyName return null as expected by the formatter - User oc_accounts_data instead of oc_accounts (to avoid the json parsing) Based on https://github.com/nextcloud/user_usage_report/pull/277 Signed-off-by: Daniel Kesselberg --- lib/Reports/SingleUser.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/Reports/SingleUser.php b/lib/Reports/SingleUser.php index 136ddc3..9bcec01 100644 --- a/lib/Reports/SingleUser.php +++ b/lib/Reports/SingleUser.php @@ -69,7 +69,9 @@ public function printReport(InputInterface $input, OutputInterface $output, stri $report['login'] = $this->getUserLastLogin($userId); } $report['shares'] = $this->getNumberOfSharesForUser($userId); - $report['display_name'] = $this->getUserDisplayName($userId); + if ($input->getOption('display-name')) { + $report['display_name'] = $this->getUserDisplayName($userId); + } $this->printRecord($input, $output, $userId, $report); } @@ -205,19 +207,16 @@ protected function getNumberOfSharesForUser(string $userId): int { return $numShares; } - /** - * @param string $userId - * @return string - */ - protected function getUserDisplayName(string $userId): string { + protected function getUserDisplayName(string $userId): ?string { $query = $this->queries['displayName']; $query->setParameter('user', $userId); $result = $query->executeQuery(); - $data = $result->fetchOne(); - $result->closeCursor(); - $json = json_decode($data, true); - $displayName = $json['displayname']['value']; - return (string) $displayName; + + if ($result->rowCount() === 0) { + return null; + } + + return (string) $result->fetchOne(); } @@ -303,9 +302,10 @@ protected function createQueries(): void { // Get User Display Name $query = $this->connection->getQueryBuilder(); - $query->select('data') - ->from('accounts') - ->where($query->expr()->eq('uid', $query->createParameter('user'))); + $query->select('value') + ->from('accounts_data') + ->where($query->expr()->eq('name', $query->createNamedParameter('displayname'))) + ->andWhere($query->expr()->eq('uid', $query->createParameter('user'))); $this->queries['displayName'] = $query; } }