Debian Codegraph @

includes/api/ApiLogout.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Api;
24
25 use MediaWiki\Session\BotPasswordSessionProvider;
26 use MediaWiki\Session\SessionManager;
27
28 /**
29 * API module to allow users to log out of the wiki. API equivalent of
30 * Special:Userlogout.
31 *
32 * @ingroup API
33 */
34 class ApiLogout extends ApiBase {
35
36 public function execute() {
37 $session = SessionManager::getGlobalSession();
38
39 // Handle bot password logout specially
40 if ( $session->getProvider() instanceof BotPasswordSessionProvider ) {
41 $session->unpersist();
42 return;
43 }
44
45 // Make sure it's possible to log out
46 if ( !$session->canSetUser() ) {
47 $this->dieWithError(
48 [
49 'cannotlogoutnow-text',
50 $session->getProvider()->describe( $this->getErrorFormatter()->getLanguage() )
51 ],
52 'cannotlogout'
53 );
54 }
55
56 $user = $this->getUser();
57
58 if ( $user->isAnon() ) {
59 // Cannot logout a anon user, so add a warning and return early.
60 $this->addWarning( 'apierror-mustbeloggedin-generic', 'notloggedin' );
61 return;
62 }
63
64 $oldName = $user->getName();
65 $user->logout();
66
67 // Give extensions to do something after user logout
68 $injected_html = '';
69 $this->getHookRunner()->onUserLogoutComplete( $user, $injected_html, $oldName );
70 }
71
72 public function mustBePosted() {
73 return true;
74 }
75
76 public function needsToken() {
77 return 'csrf';
78 }
79
80 public function isWriteMode() {
81 // While core is optimized by default to not require DB writes on log out,
82 // these are authenticated POST requests and extensions (eg. CheckUser) are
83 // allowed to perform DB writes here without warnings.
84 return true;
85 }
86
87 protected function getWebUITokenSalt( array $params ) {
88 return 'logoutToken';
89 }
90
91 public function isReadMode() {
92 return false;
93 }
94
95 protected function getExamplesMessages() {
96 return [
97 'action=logout&token=123ABC'
98 => 'apihelp-logout-example-logout',
99 ];
100 }
101
102 public function getHelpUrls() {
103 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logout';
104 }
105 }
106
107 /** @deprecated class alias since 1.43 */
108 class_alias( ApiLogout::class, 'ApiLogout' );