diff options
author | Sergey Linnik <sergey.linnik@onlyoffice.com> | 2020-12-09 17:04:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-09 17:04:16 +0300 |
commit | 742b55ffb90caaa9d3718a41e6cb7a6baea5d175 (patch) | |
tree | 6b5f3f00d6c47b46fba867b387c5b3bddb10119e /lib/keymanager.php | |
parent | 7bdd9a68de1104292a9b9d4e2d868f4c508224c6 (diff) | |
parent | 17cb7606aef6ebbe9c9257c2468ba269a3a28898 (diff) |
Merge pull request #414 from ONLYOFFICE/developv6.2.0
Release/6.2.0
Diffstat (limited to 'lib/keymanager.php')
-rw-r--r-- | lib/keymanager.php | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/lib/keymanager.php b/lib/keymanager.php index 3571696..7be503d 100644 --- a/lib/keymanager.php +++ b/lib/keymanager.php @@ -19,6 +19,7 @@ namespace OCA\Onlyoffice; +use OCP\Files\File; /** * Key manager @@ -28,6 +29,11 @@ namespace OCA\Onlyoffice; class KeyManager { /** + * App name + */ + private const App_Name = "onlyoffice"; + + /** * Table name */ private const TableName_Key = "onlyoffice_filekey"; @@ -94,7 +100,7 @@ class KeyManager { * Change lock status * * @param integer $fileId - file identifier - * @param integer $lock - status + * @param bool $lock - status * * @return bool */ @@ -112,7 +118,7 @@ class KeyManager { * Change forcesave status * * @param integer $fileId - file identifier - * @param integer $fs - status + * @param bool $fs - status * * @return bool */ @@ -147,4 +153,56 @@ class KeyManager { return $fs === "1"; } + + /** + * Change lock status in the federated share + * + * @param File $file - file + * @param bool $lock - status + * @param bool $fs - status + * + * @return bool + */ + public static function lockFederatedKey($file, $lock, $fs) { + $logger = \OC::$server->getLogger(); + $action = $lock ? "lock" : "unlock"; + + $remote = $file->getStorage()->getRemote(); + $shareToken = $file->getStorage()->getToken(); + $internalPath = $file->getInternalPath(); + + $httpClientService = \OC::$server->getHTTPClientService(); + $client = $httpClientService->newClient(); + $data = [ + "timeout" => 5, + "body" => [ + "shareToken" => $shareToken, + "path" => $internalPath, + "lock" => $lock + ] + ]; + if (!empty($fs)) { + $data["body"]["fs"] = $fs; + } + + try { + $response = $client->post($remote . "ocs/v2.php/apps/" . self::App_Name . "/api/v1/keylock?format=json", $data); + $body = \json_decode($response->getBody(), true); + + $data = $body["ocs"]["data"]; + + if (empty($data)) { + $logger->debug("Federated request " . $action . " for " . $file->getFileInfo()->getId() . " is successful", ["app" => self::App_Name]); + return true; + } + + if (!empty($data["error"])) { + $logger->error("Error " . $action . " federated key for " . $file->getFileInfo()->getId() . ": " . $data["error"], ["app" => self::App_Name]); + return false; + } + } catch (\Exception $e) { + $logger->logException($e, ["message" => "Failed to request federated " . $action . " for " . $file->getFileInfo()->getId(), "app" => self::App_Name]); + return false; + } + } } |