Skip to content

Commit 1b37a7c

Browse files
committed
REST: Allow optional denial context for permission denied errors
Bug: T330914 Change-Id: I5d135a407fea89647d368475d4f91a43b32c98a2
1 parent d7cebe5 commit 1b37a7c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

repo/rest-api/src/Application/UseCases/UseCaseError.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class UseCaseError extends UseCaseException {
6060
public const CONTEXT_PATH = 'path';
6161
public const CONTEXT_PROPERTY_ID = 'property_id';
6262
public const CONTEXT_DENIAL_REASON = 'denial_reason';
63+
public const CONTEXT_DENIAL_CONTEXT = 'denial_context';
6364
public const CONTEXT_REDIRECT_TARGET = 'redirect_target';
6465
public const CONTEXT_RESOURCE_TYPE = 'resource_type';
6566
public const CONTEXT_SITE_ID = 'site_id';
@@ -212,8 +213,13 @@ public static function newPatchResultModifiedReadOnlyValue( string $path ): self
212213
);
213214
}
214215

215-
public static function newPermissionDenied( string $reason ): self {
216-
return new self( self::PERMISSION_DENIED, 'Access to resource is denied', [ self::CONTEXT_DENIAL_REASON => $reason ] );
216+
public static function newPermissionDenied( string $reason, array $denialContext = [] ): self {
217+
$error = new self( self::PERMISSION_DENIED, 'Access to resource is denied', [ self::CONTEXT_DENIAL_REASON => $reason ] );
218+
if ( $denialContext ) {
219+
$error->errorContext[self::CONTEXT_DENIAL_CONTEXT] = $denialContext;
220+
}
221+
222+
return $error;
217223
}
218224

219225
public static function newResourceNotFound( string $resourceType ): self {

repo/rest-api/tests/phpunit/Application/UseCases/UseCaseErrorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,26 @@ public function provideInvalidUseCaseErrorData(): Generator {
7070
];
7171
}
7272

73+
/**
74+
* @dataProvider permissionDeniedContextProvider
75+
*/
76+
public function testNewPermissionDenied( UseCaseError $error, array $expectedContext ): void {
77+
$this->assertEquals( $expectedContext, $error->getErrorContext() );
78+
}
79+
80+
public function permissionDeniedContextProvider(): Generator {
81+
yield 'no additional context' => [
82+
UseCaseError::newPermissionDenied( 'some-reason' ),
83+
[ UseCaseError::CONTEXT_DENIAL_REASON => 'some-reason' ],
84+
];
85+
86+
yield 'with additional context' => [
87+
UseCaseError::newPermissionDenied( 'some-other-reason', [ 'some' => 'context' ] ),
88+
[
89+
UseCaseError::CONTEXT_DENIAL_REASON => 'some-other-reason',
90+
UseCaseError::CONTEXT_DENIAL_CONTEXT => [ 'some' => 'context' ],
91+
],
92+
];
93+
}
94+
7395
}

0 commit comments

Comments
 (0)