diff --git a/lib/Db/UserMapper.php b/lib/Db/UserMapper.php index 2ec0c137a..06987671c 100644 --- a/lib/Db/UserMapper.php +++ b/lib/Db/UserMapper.php @@ -138,10 +138,6 @@ public function getUserFromShare(Share $share): GenericUser|Email|User|ContactGr } public function getUserFromShareToken(string $token): UserBase { - // TODO: This is just a dirty hack. Analyse userId while updating to get correct userId - if ($token === '') { - return new Ghost(); - } $share = $this->getShareByToken($token); return $this->getUserFromShare($share); diff --git a/lib/UserSession.php b/lib/UserSession.php index ca5b9eadf..26a5ca30b 100644 --- a/lib/UserSession.php +++ b/lib/UserSession.php @@ -8,10 +8,12 @@ namespace OCA\Polls; +use Exception; use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\UserMapper; use OCA\Polls\Model\User\Cron; +use OCA\Polls\Model\User\Ghost; use OCA\Polls\Model\UserBase; use OCP\ISession; use OCP\IUserSession; @@ -56,12 +58,18 @@ public function __construct( public function getCurrentUser(): UserBase { if (!$this->currentUser) { - if ($this->getIsLoggedIn()) { - $this->currentUser = $this->userMapper->getUserFromUserBase((string)$this->userSession->getUser()?->getUID()); - } elseif ($this->session->get(self::SESSION_KEY_CRON_JOB)) { - $this->currentUser = new Cron(); - } else { - $this->currentUser = $this->userMapper->getUserFromShareToken($this->getShareToken()); + try { + if ($this->getIsLoggedIn()) { + $this->currentUser = $this->userMapper->getUserFromUserBase((string)$this->userSession->getUser()?->getUID()); + } elseif ($this->session->get(self::SESSION_KEY_CRON_JOB)) { + $this->currentUser = new Cron(); + } else { + $this->currentUser = $this->userMapper->getUserFromShareToken($this->getShareToken()); + } + } catch (Exception $e) { + // In case of system jobs, we do not get a valid user, so we return a Ghost user + // can happen while running cron jobs or during app updates + $this->currentUser = new Ghost(); } }