diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..d7116cc --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +export PHP_VERSION=/usr/bin/php7 diff --git a/composer.json b/composer.json index 317c237..216d13c 100644 --- a/composer.json +++ b/composer.json @@ -1,26 +1,26 @@ { - "name": "processmaker/flysystem-msgraph", - "description": "A Flysystem Adapter that supports Microsoft OneDrive and Sharepoint Document Libraries using Microsoft Graph", - "type": "library", - "license": "MIT", - "authors": [ - { - "name": "Taylor Dondich", - "email": "taylor@processmaker.com" - } - ], - "require": { - "league/oauth2-client": "^2.4", - "microsoft/microsoft-graph": "dev-master", - "league/flysystem": "^1.0" - }, - "autoload": { - "psr-4": { - "ProcessMaker\\Flysystem\\Adapter\\": "src/", - "ProcessMaker\\Flysystem\\Adapter\\MSGraph\\Test\\": "tests/" - } - }, - "require-dev": { - "phpunit/phpunit": "^7.5" + "name": "kimmelsg/flysystem-msgraph", + "description": "A Flysystem Adapter that supports Microsoft OneDrive and Sharepoint Document Libraries using Microsoft Graph -- a fork.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Taylor Dondich", + "email": "taylor@processmaker.com" } + ], + "require": { + "league/oauth2-client": "^2.4", + "microsoft/microsoft-graph": "dev-main", + "league/flysystem": "^1.0" + }, + "autoload": { + "psr-4": { + "Kimmelsg\\Flysystem\\Adapter\\": "src/", + "Kimmelsg\\Flysystem\\Adapter\\MSGraph\\Test\\": "tests/" + } + }, + "require-dev": { + "phpunit/phpunit": "^7.5" + } } diff --git a/phpunit.xml b/phpunit.xml index ada80ab..6c80d46 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ - + tests @@ -13,4 +13,4 @@ - \ No newline at end of file + diff --git a/src/MSGraph.php b/src/MSGraph.php index 499fa96..621c29e 100644 --- a/src/MSGraph.php +++ b/src/MSGraph.php @@ -1,157 +1,150 @@ mode = $mode; - - // Initialize the OAuth client - $oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([ - 'clientId' => $appId, - 'clientSecret' => $appPassword, - 'urlAuthorize' => '', - 'urlResourceOwnerDetails' => '', - 'urlAccessToken' => $tokenEndpoint, - ]); - - try { - $this->token = $oauthClient->getAccessToken('client_credentials', [ - 'scope' => 'https://graph.microsoft.com/.default' - ]); - } catch(IdentityProviderException $e) { - throw new AuthException($e->getMessage()); - } + } - // Assign graph instance - $this->graph = new Graph(); - $this->graph->setAccessToken($this->token->getToken()); + public function initialize($graph, $mode = self::MODE_ONEDRIVE, $targetId, $driveName) + { + $this->mode = $mode; + $this->graph = $graph; // Check for existence - if($mode == self::MODE_SHAREPOINT) { + if ($mode == self::MODE_SHAREPOINT) { try { - $site = $this->graph->createRequest('GET', '/sites/' . $targetId) - ->setReturnType(Model\Site::class) - ->execute(); - // Assign the site id triplet to our targetId - $this->targetId = $site->getId(); - } catch(\Exception $e) { - if($e->getCode() == 400) { + $site = $this->graph->createRequest('GET', '/sites/' . $targetId) + ->setReturnType(Model\Site::class) + ->execute(); + // Assign the site id triplet to our targetId + $this->targetId = $site->getId(); + } catch (\Exception $e) { + if ($e->getCode() == 400) { throw new SiteInvalidException("The sharepoint site " . $targetId . " is invalid."); } + throw $e; } $this->prefix = "/sites/" . $this->targetId . '/drive/items/'; - if($driveName != '') { + if ($driveName != '') { // Then we specified a drive name, so let's enumerate the drives and find it $drives = $this->graph->createRequest('GET', '/sites/' . $this->targetId . '/drives') ->execute(); $drives = $drives->getBody()['value']; - foreach($drives as $drive) { - if($drive['name'] == $driveName) { + foreach ($drives as $drive) { + if ($drive['name'] == $driveName) { $this->driveId = $drive['id']; $this->prefix = "/drives/" . $this->driveId . "/items/"; + break; } } - if(!$this->driveId) { - throw new SiteInvalidException("The sharepoint drive with name " . $driveName . " could not be found."); + if (! $this->driveId) { + throw new SiteInvalidException("The sharepoint drive with name " . $driveName . " could not be found."); } - } } - } public function has($path) { - if($this->mode == self::MODE_SHAREPOINT) { + if ($this->mode == self::MODE_SHAREPOINT) { try { $driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path) ->setReturnType(Model\DriveItem::class) ->execute(); // Successfully retrieved meta data. return true; - } catch(ClientException $e) { - if($e->getCode() == 404) { + } catch (ClientException $e) { + if ($e->getCode() == 404) { // Not found, let's return false; return false; } + throw $e; - } catch(Exception $e) { + } catch (Exception $e) { throw $e; } } + return false; } public function read($path) { - if($this->mode == self::MODE_SHAREPOINT) { + if ($this->mode == self::MODE_SHAREPOINT) { try { $driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path) ->setReturnType(Model\DriveItem::class) ->execute(); // Successfully retrieved meta data. // Now get content - $contentStream = $this->graph->createRequest('GET', $this->prefix . $driveItem->getId() .'/content') + $contentStream = $this->graph->createRequest('GET', $this->prefix . $driveItem->getId() . '/content') ->setReturnType(Stream::class) ->execute(); $contents = ''; $bufferSize = 8012; // Copy over the data into a string - while (!$contentStream->eof()) { + while (! $contentStream->eof()) { $contents .= $contentStream->read($bufferSize); } + return ['contents' => $contents]; - } catch(ClientException $e) { - if($e->getCode() == 404) { + } catch (ClientException $e) { + if ($e->getCode() == 404) { // Not found, let's return false; return false; } + throw $e; - } catch(Exception $e) { + } catch (Exception $e) { throw $e; } } + return false; } public function getUrl($path) { - if($this->mode == self::MODE_SHAREPOINT) { + if ($this->mode == self::MODE_SHAREPOINT) { try { $driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path) ->setReturnType(Model\DriveItem::class) @@ -159,22 +152,23 @@ public function getUrl($path) // Successfully retrieved meta data. // Return url property return $driveItem->getWebUrl(); - } catch(ClientException $e) { - if($e->getCode() == 404) { + } catch (ClientException $e) { + if ($e->getCode() == 404) { // Not found, let's return false; return false; } + throw $e; - } catch(Exception $e) { + } catch (Exception $e) { throw $e; } } + return false; } - + public function readStream($path) { - } public function listContents($directory = '', $recursive = false) @@ -186,16 +180,17 @@ public function listContents($directory = '', $recursive = false) ->execute(); // Successfully retrieved meta data. // Now get content - $driveItems = $this->graph->createRequest('GET', $this->prefix . $drive->getId() .'/children') + $driveItems = $this->graph->createRequest('GET', $this->prefix . $drive->getId() . '/children') ->setReturnType(Model\DriveItem::class) ->execute(); - + $children = []; foreach ($driveItems as $driveItem) { $item = $driveItem->getProperties(); $item['path'] = $directory . '/' . $driveItem->getName(); $children[] = $item; } + return $children; } catch (ClientException $e) { throw $e; @@ -203,80 +198,71 @@ public function listContents($directory = '', $recursive = false) throw $e; } } + return []; } public function getMetadata($path) { - } public function getSize($path) { - } public function getMimetype($path) { - } public function getTimestamp($path) { - } public function getVisibility($path) { - } // Write methods public function write($path, $contents, Config $config) { - if($this->mode == self::MODE_SHAREPOINT) { + if ($this->mode == self::MODE_SHAREPOINT) { // Attempt to write to sharepoint - try { - $driveItem = $this->graph->createRequest('PUT', $this->prefix . 'root:/' . $path . ':/content') - ->attachBody($contents) - ->setReturnType(Model\DriveItem::class) - ->execute(); - // Successfully created - return true; - } catch(Exception $e) { - throw $e; - } + $driveItem = $this->graph->createRequest('PUT', $this->prefix . 'root:/' . $path . ':/content') + ->attachBody($contents) + ->setReturnType(Model\DriveItem::class) + ->execute(); + + // Successfully created + return true; } + + return false; } public function writeStream($path, $resource, Config $config) { - } public function update($path, $contents, Config $config) { - + return $this->write($path, $contents, $config); } public function updateStream($path, $resource, Config $config) { - } public function rename($path, $newpath) { - } public function copy($path, $newpath) { - } public function delete($path) { - if($this->mode == self::MODE_SHAREPOINT) { + if ($this->mode == self::MODE_SHAREPOINT) { try { $driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path) ->setReturnType(Model\DriveItem::class) @@ -285,34 +271,32 @@ public function delete($path) // Now delete the file $this->graph->createRequest('DELETE', $this->prefix . $driveItem->getId()) ->execute(); + return true; - } catch(ClientException $e) { - if($e->getCode() == 404) { + } catch (ClientException $e) { + if ($e->getCode() == 404) { // Not found, let's return false; return false; } + throw $e; - } catch(Exception $e) { + } catch (Exception $e) { throw $e; } } - return false; + return false; } public function deleteDir($dirname) { - } public function createDir($dirname, Config $config) { - } public function setVisibility($path, $visibility) { - } - } diff --git a/src/MSGraph/AuthException.php b/src/MSGraph/AuthException.php index 9306436..b72294f 100644 --- a/src/MSGraph/AuthException.php +++ b/src/MSGraph/AuthException.php @@ -1,9 +1,9 @@ setAccessToken($appModeToken); + + $this->initialize($graph, $mode, $targetId, $driveName); + } +} diff --git a/src/MSGraphUser.php b/src/MSGraphUser.php new file mode 100644 index 0000000..ffa4caa --- /dev/null +++ b/src/MSGraphUser.php @@ -0,0 +1,38 @@ + $appId, + 'clientSecret' => $appPassword, + 'urlAuthorize' => '', + 'urlResourceOwnerDetails' => '', + 'urlAccessToken' => $tokenEndpoint, + ]); + + try { + $this->token = $oauthClient->getAccessToken('client_credentials', [ + 'scope' => 'https://graph.microsoft.com/.default' + ]); + } catch(IdentityProviderException $e) { + throw new AuthException($e->getMessage()); + } + + // Assign graph instance + $graph = new Graph(); + $graph->setAccessToken($this->token->getToken()); + + $this->initialize($graph, $mode, $targetId, $driveName); + } +} diff --git a/tests/ConnectivityTest.php b/tests/ConnectivityTest.php index a45d727..0987e99 100644 --- a/tests/ConnectivityTest.php +++ b/tests/ConnectivityTest.php @@ -1,11 +1,11 @@ filesToPurge = []; } -} \ No newline at end of file +} diff --git a/tests/TestBase.php b/tests/TestBase.php index 3a9d48a..1b2f48b 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -1,7 +1,7 @@