From 9594818f975531a29acb44ea9345774c078287fc Mon Sep 17 00:00:00 2001 From: Adam Jimenez Date: Tue, 29 Mar 2022 11:19:26 +0100 Subject: [PATCH] Error handling for file permission issues This will help devs to resolve file permission issues. At the moment file saving can fail silently resulting in access tokens not being re-used. --- .../persistence/ZohoOAuthPersistenceByFile.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/oauth/persistence/ZohoOAuthPersistenceByFile.php b/src/oauth/persistence/ZohoOAuthPersistenceByFile.php index bfcb6b99..64472823 100644 --- a/src/oauth/persistence/ZohoOAuthPersistenceByFile.php +++ b/src/oauth/persistence/ZohoOAuthPersistenceByFile.php @@ -22,6 +22,11 @@ public function saveOAuthData($zohoOAuthTokens) try { self::deleteOAuthTokens($zohoOAuthTokens->getUserEmailId()); $content = file_get_contents(self::getTokenPersistencePath()."/zcrm_oauthtokens.txt" ); + + if ($content === false) { + throw new ZohoOAuthException("Can not read tokens file, please check permissions: " . self::getTokenPersistencePath()."/zcrm_oauthtokens.txt"); + } + if ($content == "") { $arr = array(); } else { @@ -29,7 +34,11 @@ public function saveOAuthData($zohoOAuthTokens) } array_push($arr, $zohoOAuthTokens); $serialized = serialize($arr); - file_put_contents(self::getTokenPersistencePath()."/zcrm_oauthtokens.txt",$serialized ); + $result = file_put_contents(self::getTokenPersistencePath()."/zcrm_oauthtokens.txt",$serialized ); + + if ($result === false) { + throw new ZohoOAuthException("Can not write to tokens file, please check permissions: " . self::getTokenPersistencePath()."/zcrm_oauthtokens.txt"); + } } catch (Exception $ex) { Logger::severe("Exception occured while Saving OAuthTokens to file(file::ZohoOAuthPersistenceByFile)({$ex->getMessage()})\n{$ex}"); throw $ex; @@ -40,6 +49,11 @@ public function getOAuthTokens($userEmailId) { try { $serialized = file_get_contents(self::getTokenPersistencePath()."/zcrm_oauthtokens.txt" ); + + if ($serialized === false) { + throw new ZohoOAuthException("Can not read tokens file, please check permissions: " . self::getTokenPersistencePath()."/zcrm_oauthtokens.txt"); + } + if (! isset($serialized) || $serialized == "") { throw new ZohoOAuthException("No Tokens exist for the given user-identifier,Please generate and try again."); }