ApiUnblock.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<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. use MediaWiki\Block\DatabaseBlock;
  23. /**
  24. * API module that facilitates the unblocking of users. Requires API write mode
  25. * to be enabled.
  26. *
  27. * @ingroup API
  28. */
  29. class ApiUnblock extends ApiBase {
  30. use ApiBlockInfoTrait;
  31. /**
  32. * Unblocks the specified user or provides the reason the unblock failed.
  33. */
  34. public function execute() {
  35. $user = $this->getUser();
  36. $params = $this->extractRequestParams();
  37. $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
  38. if ( !$this->getPermissionManager()->userHasRight( $user, 'block' ) ) {
  39. $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
  40. }
  41. # T17810: blocked admins should have limited access here
  42. $block = $user->getBlock();
  43. if ( $block ) {
  44. $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
  45. if ( $status !== true ) {
  46. $this->dieWithError(
  47. $status,
  48. null,
  49. [ 'blockinfo' => $this->getBlockDetails( $block ) ]
  50. );
  51. }
  52. }
  53. // Check if user can add tags
  54. if ( !is_null( $params['tags'] ) ) {
  55. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  56. if ( !$ableToTag->isOK() ) {
  57. $this->dieStatus( $ableToTag );
  58. }
  59. }
  60. if ( $params['userid'] !== null ) {
  61. $username = User::whoIs( $params['userid'] );
  62. if ( $username === false ) {
  63. $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
  64. } else {
  65. $params['user'] = $username;
  66. }
  67. }
  68. $data = [
  69. 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
  70. 'Reason' => $params['reason'],
  71. 'Tags' => $params['tags']
  72. ];
  73. $block = DatabaseBlock::newFromTarget( $data['Target'] );
  74. $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
  75. if ( $retval !== true ) {
  76. $this->dieStatus( $this->errorArrayToStatus( $retval ) );
  77. }
  78. $target = $block->getType() == DatabaseBlock::TYPE_AUTO ? '' : $block->getTarget();
  79. $res = [
  80. 'id' => $block->getId(),
  81. 'user' => $target instanceof User ? $target->getName() : $target,
  82. 'userid' => $target instanceof User ? $target->getId() : 0,
  83. 'reason' => $params['reason']
  84. ];
  85. $this->getResult()->addValue( null, $this->getModuleName(), $res );
  86. }
  87. public function mustBePosted() {
  88. return true;
  89. }
  90. public function isWriteMode() {
  91. return true;
  92. }
  93. public function getAllowedParams() {
  94. return [
  95. 'id' => [
  96. ApiBase::PARAM_TYPE => 'integer',
  97. ],
  98. 'user' => null,
  99. 'userid' => [
  100. ApiBase::PARAM_TYPE => 'integer'
  101. ],
  102. 'reason' => '',
  103. 'tags' => [
  104. ApiBase::PARAM_TYPE => 'tags',
  105. ApiBase::PARAM_ISMULTI => true,
  106. ],
  107. ];
  108. }
  109. public function needsToken() {
  110. return 'csrf';
  111. }
  112. protected function getExamplesMessages() {
  113. return [
  114. 'action=unblock&id=105'
  115. => 'apihelp-unblock-example-id',
  116. 'action=unblock&user=Bob&reason=Sorry%20Bob'
  117. => 'apihelp-unblock-example-user',
  118. ];
  119. }
  120. public function getHelpUrls() {
  121. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
  122. }
  123. }