Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4071005
RestAuthorizeTrait.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
RestAuthorizeTrait.php
View Options
<?php
namespace
MediaWiki\Rest\Handler\Helper
;
use
MediaWiki\Page\PageIdentity
;
use
MediaWiki\Permissions\Authority
;
use
MediaWiki\Permissions\PermissionStatus
;
use
MediaWiki\Rest\HttpException
;
use
Wikimedia\Message\MessageValue
;
trait
RestAuthorizeTrait
{
use
RestStatusTrait
;
/**
* Authorize an action
*
* @see Authroity::authorizeWrite
* @throws HttpException
*/
private
function
authorizeActionOrThrow
(
Authority
$authority
,
string
$action
):
void
{
$status
=
PermissionStatus
::
newEmpty
();
if
(
!
$authority
->
authorizeAction
(
$action
,
$status
)
)
{
$this
->
handleStatus
(
$status
);
}
}
/**
* Authorize a read operation
*
* @see Authroity::authorizeWrite
* @throws HttpException
*/
private
function
authorizeReadOrThrow
(
Authority
$authority
,
string
$action
,
PageIdentity
$target
):
void
{
$status
=
PermissionStatus
::
newEmpty
();
if
(
!
$authority
->
authorizeRead
(
$action
,
$target
,
$status
)
)
{
$this
->
handleStatus
(
$status
);
}
}
/**
* Authorize a write operation
*
* @see Authroity::authorizeWrite
* @throws HttpException
*/
private
function
authorizeWriteOrThrow
(
Authority
$authority
,
string
$action
,
PageIdentity
$target
):
void
{
$status
=
PermissionStatus
::
newEmpty
();
if
(
!
$authority
->
authorizeWrite
(
$action
,
$target
,
$status
)
)
{
$this
->
handleStatus
(
$status
);
}
}
/**
* Throw an exception if the status contains an error.
*
* @throws HttpException
* @return never
*/
private
function
handleStatus
(
PermissionStatus
$status
):
void
{
// The permission name should always be set, but don't explode if it isn't.
$permission
=
$status
->
getPermission
()
?:
'(unknown)'
;
if
(
$status
->
isRateLimitExceeded
()
)
{
$this
->
throwExceptionForStatus
(
$status
,
MessageValue
::
new
(
'rest-rate-limit-exceeded'
,
[
$permission
]
),
429
// See https://www.rfc-editor.org/rfc/rfc6585#section-4
);
}
$this
->
throwExceptionForStatus
(
$status
,
MessageValue
::
new
(
'rest-permission-error'
,
[
$permission
]
),
403
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Aug 17 2026, 02:30 (11 w, 5 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c9/83/1b84f0af0bccbafaa6a3e085280f
Default Alt Text
RestAuthorizeTrait.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment