MediaWiki master
ApiLogout.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
15
22class ApiLogout extends ApiBase {
23
24 public function __construct(
25 ApiMain $main,
26 string $action,
27 private readonly SessionManager $sessionManager
28 ) {
29 parent::__construct( $main, $action );
30 }
31
32 public function execute() {
33 if ( $this->getUser()->isAnon() ) {
34 // Cannot log out an anon user, so add a warning and return early.
35 $this->addWarning( 'apierror-mustbeloggedin-generic', 'notloggedin' );
36 return;
37 }
38
39 if ( $this->getParameter( 'global' ) ) {
40 $this->checkUserRightsAny( 'logout' );
41 $this->doGlobalLogout();
42 } else {
43 $this->doLocalLogout();
44 }
45 }
46
47 protected function doGlobalLogout(): void {
48 $user = $this->getUser();
49 // remove cookies
50 $this->getRequest()->getSession()->unpersist();
51 $this->sessionManager->invalidateSessionsForUser( $user );
52 $this->callUserLogoutComplete( $user, $user->getName() );
53 }
54
55 protected function doLocalLogout(): void {
56 $user = $this->getUser();
57 $oldUserName = $user->getName();
58 $session = $this->getRequest()->getSession();
59
60 // Handle bot password logout specially
61 if ( $session->getProvider() instanceof BotPasswordSessionProvider ) {
62 $session->unpersist();
63 return;
64 }
65
66 // Make sure it's possible to log out
67 if ( !$session->canSetUser() ) {
68 $this->dieWithError(
69 [
70 'cannotlogoutnow-text',
71 $session->getProvider()->describe( $this->getErrorFormatter()->getLanguage() )
72 ],
73 'cannotlogout'
74 );
75 }
76
77 $user->logout();
78 $this->callUserLogoutComplete( $user, $oldUserName );
79 }
80
81 protected function callUserLogoutComplete( User $user, string $oldUserName ) {
82 // Give extensions to do something after user logout
83 $injected_html = '';
84 $this->getHookRunner()->onUserLogoutComplete( $user, $injected_html, $oldUserName );
85 }
86
88 protected function getAllowedParams() {
89 return [
90 'global' => [
91 ParamValidator::PARAM_TYPE => 'boolean',
92 ParamValidator::PARAM_DEFAULT => false,
93 ],
94 ];
95 }
96
98 public function mustBePosted() {
99 return true;
100 }
101
103 public function needsToken() {
104 return 'csrf';
105 }
106
108 public function isWriteMode() {
109 // While core is optimized by default to not require DB writes on log out,
110 // these are authenticated POST requests and extensions (eg. CheckUser) are
111 // allowed to perform DB writes here without warnings.
112 return true;
113 }
114
116 protected function getWebUITokenSalt( array $params ) {
117 return 'logoutToken';
118 }
119
121 public function isReadMode() {
122 return false;
123 }
124
126 protected function getExamplesMessages() {
127 return [
128 'action=logout&token=123ABC'
129 => 'apihelp-logout-example-logout',
130 ];
131 }
132
134 public function getHelpUrls() {
135 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logout';
136 }
137}
138
140class_alias( ApiLogout::class, 'ApiLogout' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1631
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1439
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:958
API module to allow users to log out of the wiki.
Definition ApiLogout.php:22
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiLogout.php:32
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
callUserLogoutComplete(User $user, string $oldUserName)
Definition ApiLogout.php:81
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition ApiLogout.php:88
getWebUITokenSalt(array $params)
Fetch the salt used in the Web UI corresponding to this module.Only override this if the Web UI uses ...
isReadMode()
Indicates whether this module requires read rights.to override bool
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
Definition ApiLogout.php:98
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
__construct(ApiMain $main, string $action, private readonly SessionManager $sessionManager)
Definition ApiLogout.php:24
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
This serves as the entry point to the MediaWiki session handling system.
User class for the MediaWiki software.
Definition User.php:129
Service for formatting and validating API parameters.