diff --git a/README.md b/README.md index d30cd6e..73ccf8f 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ project at the moment is [tus](https://tus.io/). - [simple-uploader.js](#simple-uploader-js-driver) - [Identifiers](#identifiers) - [Session identifier](#session-identifier) + - [Auth identifier](#auth-identifier) - [NOP identifier](#nop-identifier) - [Contribution](#contribution) - [License](#license) @@ -223,11 +224,18 @@ file for a specific client. Without the identifier collisions can happen. Service | Driver name ------------------------------------------|------------- [Session identifier](#session-identifier) | `session` +[Auth identifier](#auth-identifier) | `auth` [NOP identifier](#nop-identifier) | `nop` ### Session identifier -This identifier uses the client session and, the original file name to create an identifier for the upload file. +This identifier uses the client session and the original file name to create an identifier for the upload file. + +### Auth identifier + +This identifier uses the id of the authenticated user and the original file name to create an identifier for the upload file. + +It will throw `UnauthorizedException` when the user is unauthorized. However, it is still recommended to use the `auth` middleware. ### NOP identifier diff --git a/config/chunk-uploader.php b/config/chunk-uploader.php index 27ee52a..b2ef67d 100644 --- a/config/chunk-uploader.php +++ b/config/chunk-uploader.php @@ -28,7 +28,7 @@ | specify which one you're using throughout your application here. By | default, the module is setup for session identity. | - | Supported: "session" + | Supported: "auth", "nop", "session" | */ diff --git a/src/Identifier/AuthIdentifier.php b/src/Identifier/AuthIdentifier.php new file mode 100644 index 0000000..48c44dc --- /dev/null +++ b/src/Identifier/AuthIdentifier.php @@ -0,0 +1,17 @@ +andReturn(100); + + $this->identifier = new AuthIdentifier(); + } + + protected function tearDown(): void + { + parent::tearDown(); + + Auth::clearResolvedInstances(); + } + + public function testGenerateIdentifierThrowsUnauthorizedException() + { + Auth::shouldReceive('check') + ->andReturn(false); + + $this->expectException(UnauthorizedException::class); + $this->identifier->generateIdentifier('any_string'); + } + + public function testGenerateIdentifier() + { + Auth::shouldReceive('check') + ->andReturn(true); + + $identifier = $this->identifier->generateIdentifier('any_string'); + $this->assertEquals('2b2ea43a7652e1f7925c588b9ae7a31f09be3bf9', $identifier); + } + + public function testUploadedFileIdentifierName() + { + Auth::shouldReceive('check') + ->andReturn(true); + + $identifier = $this->identifier->generateFileIdentifier(200, 'any_filename.ext'); + $this->assertEquals('4317e3d56e27deda5bd84dd35830bff799736257', $identifier); + } +}