MediaWiki  master
PermissionManager.php
Go to the documentation of this file.
1 <?php
21 
22 use Action;
25 use Hooks;
33 use Title;
34 use User;
36 
44 
46  const RIGOR_QUICK = 'quick';
47 
49  const RIGOR_FULL = 'full';
50 
52  const RIGOR_SECURE = 'secure';
53 
55  private $specialPageFactory;
56 
58  private $whitelistRead;
59 
62 
65 
68 
70  private $nsInfo;
71 
80  public function __construct(
87  ) {
88  $this->specialPageFactory = $specialPageFactory;
89  $this->whitelistRead = $whitelistRead;
90  $this->whitelistReadRegexp = $whitelistReadRegexp;
91  $this->emailConfirmToEdit = $emailConfirmToEdit;
92  $this->blockDisablesLogin = $blockDisablesLogin;
93  $this->nsInfo = $nsInfo;
94  }
95 
116  public function userCan( $action, User $user, LinkTarget $page, $rigor = self::RIGOR_SECURE ) {
117  return !count( $this->getPermissionErrorsInternal( $action, $user, $page, $rigor, true ) );
118  }
119 
138  public function getPermissionErrors(
139  $action,
140  User $user,
141  LinkTarget $page,
142  $rigor = self::RIGOR_SECURE,
143  $ignoreErrors = []
144  ) {
145  $errors = $this->getPermissionErrorsInternal( $action, $user, $page, $rigor );
146 
147  // Remove the errors being ignored.
148  foreach ( $errors as $index => $error ) {
149  $errKey = is_array( $error ) ? $error[0] : $error;
150 
151  if ( in_array( $errKey, $ignoreErrors ) ) {
152  unset( $errors[$index] );
153  }
154  if ( $errKey instanceof MessageSpecifier && in_array( $errKey->getKey(), $ignoreErrors ) ) {
155  unset( $errors[$index] );
156  }
157  }
158 
159  return $errors;
160  }
161 
173  public function isBlockedFrom( User $user, LinkTarget $page, $fromReplica = false ) {
174  $blocked = $user->isHidden();
175 
176  // TODO: remove upon further migration to LinkTarget
177  $page = Title::newFromLinkTarget( $page );
178 
179  if ( !$blocked ) {
180  $block = $user->getBlock( $fromReplica );
181  if ( $block ) {
182  // Special handling for a user's own talk page. The block is not aware
183  // of the user, so this must be done here.
184  if ( $page->equals( $user->getTalkPage() ) ) {
185  $blocked = $block->appliesToUsertalk( $page );
186  } else {
187  $blocked = $block->appliesToTitle( $page );
188  }
189  }
190  }
191 
192  // only for the purpose of the hook. We really don't need this here.
193  $allowUsertalk = $user->isAllowUsertalk();
194 
195  Hooks::run( 'UserIsBlockedFrom', [ $user, $page, &$blocked, &$allowUsertalk ] );
196 
197  return $blocked;
198  }
199 
217  private function getPermissionErrorsInternal(
218  $action,
219  User $user,
220  LinkTarget $page,
221  $rigor = self::RIGOR_SECURE,
222  $short = false
223  ) {
224  if ( !in_array( $rigor, [ self::RIGOR_QUICK, self::RIGOR_FULL, self::RIGOR_SECURE ] ) ) {
225  throw new Exception( "Invalid rigor parameter '$rigor'." );
226  }
227 
228  # Read has special handling
229  if ( $action == 'read' ) {
230  $checks = [
231  'checkPermissionHooks',
232  'checkReadPermissions',
233  'checkUserBlock', // for wgBlockDisablesLogin
234  ];
235  # Don't call checkSpecialsAndNSPermissions, checkSiteConfigPermissions
236  # or checkUserConfigPermissions here as it will lead to duplicate
237  # error messages. This is okay to do since anywhere that checks for
238  # create will also check for edit, and those checks are called for edit.
239  } elseif ( $action == 'create' ) {
240  $checks = [
241  'checkQuickPermissions',
242  'checkPermissionHooks',
243  'checkPageRestrictions',
244  'checkCascadingSourcesRestrictions',
245  'checkActionPermissions',
246  'checkUserBlock'
247  ];
248  } else {
249  $checks = [
250  'checkQuickPermissions',
251  'checkPermissionHooks',
252  'checkSpecialsAndNSPermissions',
253  'checkSiteConfigPermissions',
254  'checkUserConfigPermissions',
255  'checkPageRestrictions',
256  'checkCascadingSourcesRestrictions',
257  'checkActionPermissions',
258  'checkUserBlock'
259  ];
260  }
261 
262  $errors = [];
263  foreach ( $checks as $method ) {
264  $errors = $this->$method( $action, $user, $errors, $rigor, $short, $page );
265 
266  if ( $short && $errors !== [] ) {
267  break;
268  }
269  }
270 
271  return $errors;
272  }
273 
292  private function checkPermissionHooks(
293  $action,
294  User $user,
295  $errors,
296  $rigor,
297  $short,
298  LinkTarget $page
299  ) {
300  // TODO: remove when LinkTarget usage will expand further
301  $page = Title::newFromLinkTarget( $page );
302  // Use getUserPermissionsErrors instead
303  $result = '';
304  if ( !Hooks::run( 'userCan', [ &$page, &$user, $action, &$result ] ) ) {
305  return $result ? [] : [ [ 'badaccess-group0' ] ];
306  }
307  // Check getUserPermissionsErrors hook
308  if ( !Hooks::run( 'getUserPermissionsErrors', [ &$page, &$user, $action, &$result ] ) ) {
309  $errors = $this->resultToError( $errors, $result );
310  }
311  // Check getUserPermissionsErrorsExpensive hook
312  if (
313  $rigor !== self::RIGOR_QUICK
314  && !( $short && count( $errors ) > 0 )
315  && !Hooks::run( 'getUserPermissionsErrorsExpensive', [ &$page, &$user, $action, &$result ] )
316  ) {
317  $errors = $this->resultToError( $errors, $result );
318  }
319 
320  return $errors;
321  }
322 
331  private function resultToError( $errors, $result ) {
332  if ( is_array( $result ) && count( $result ) && !is_array( $result[0] ) ) {
333  // A single array representing an error
334  $errors[] = $result;
335  } elseif ( is_array( $result ) && is_array( $result[0] ) ) {
336  // A nested array representing multiple errors
337  $errors = array_merge( $errors, $result );
338  } elseif ( $result !== '' && is_string( $result ) ) {
339  // A string representing a message-id
340  $errors[] = [ $result ];
341  } elseif ( $result instanceof MessageSpecifier ) {
342  // A message specifier representing an error
343  $errors[] = [ $result ];
344  } elseif ( $result === false ) {
345  // a generic "We don't want them to do that"
346  $errors[] = [ 'badaccess-group0' ];
347  }
348  return $errors;
349  }
350 
369  private function checkReadPermissions(
370  $action,
371  User $user,
372  $errors,
373  $rigor,
374  $short,
375  LinkTarget $page
376  ) {
377  // TODO: remove when LinkTarget usage will expand further
378  $page = Title::newFromLinkTarget( $page );
379 
380  $whitelisted = false;
381  if ( User::isEveryoneAllowed( 'read' ) ) {
382  # Shortcut for public wikis, allows skipping quite a bit of code
383  $whitelisted = true;
384  } elseif ( $user->isAllowed( 'read' ) ) {
385  # If the user is allowed to read pages, he is allowed to read all pages
386  $whitelisted = true;
387  } elseif ( $this->isSameSpecialPage( 'Userlogin', $page )
388  || $this->isSameSpecialPage( 'PasswordReset', $page )
389  || $this->isSameSpecialPage( 'Userlogout', $page )
390  ) {
391  # Always grant access to the login page.
392  # Even anons need to be able to log in.
393  $whitelisted = true;
394  } elseif ( is_array( $this->whitelistRead ) && count( $this->whitelistRead ) ) {
395  # Time to check the whitelist
396  # Only do these checks is there's something to check against
397  $name = $page->getPrefixedText();
398  $dbName = $page->getPrefixedDBkey();
399 
400  // Check for explicit whitelisting with and without underscores
401  if ( in_array( $name, $this->whitelistRead, true )
402  || in_array( $dbName, $this->whitelistRead, true ) ) {
403  $whitelisted = true;
404  } elseif ( $page->getNamespace() == NS_MAIN ) {
405  # Old settings might have the title prefixed with
406  # a colon for main-namespace pages
407  if ( in_array( ':' . $name, $this->whitelistRead ) ) {
408  $whitelisted = true;
409  }
410  } elseif ( $page->isSpecialPage() ) {
411  # If it's a special page, ditch the subpage bit and check again
412  $name = $page->getDBkey();
413  list( $name, /* $subpage */ ) =
414  $this->specialPageFactory->resolveAlias( $name );
415  if ( $name ) {
416  $pure = SpecialPage::getTitleFor( $name )->getPrefixedText();
417  if ( in_array( $pure, $this->whitelistRead, true ) ) {
418  $whitelisted = true;
419  }
420  }
421  }
422  }
423 
424  if ( !$whitelisted && is_array( $this->whitelistReadRegexp )
425  && !empty( $this->whitelistReadRegexp ) ) {
426  $name = $page->getPrefixedText();
427  // Check for regex whitelisting
428  foreach ( $this->whitelistReadRegexp as $listItem ) {
429  if ( preg_match( $listItem, $name ) ) {
430  $whitelisted = true;
431  break;
432  }
433  }
434  }
435 
436  if ( !$whitelisted ) {
437  # If the title is not whitelisted, give extensions a chance to do so...
438  Hooks::run( 'TitleReadWhitelist', [ $page, $user, &$whitelisted ] );
439  if ( !$whitelisted ) {
440  $errors[] = $this->missingPermissionError( $action, $short );
441  }
442  }
443 
444  return $errors;
445  }
446 
455  private function missingPermissionError( $action, $short ) {
456  // We avoid expensive display logic for quickUserCan's and such
457  if ( $short ) {
458  return [ 'badaccess-group0' ];
459  }
460 
461  // TODO: it would be a good idea to replace the method below with something else like
462  // maybe callback injection
463  return User::newFatalPermissionDeniedStatus( $action )->getErrorsArray()[0];
464  }
465 
474  private function isSameSpecialPage( $name, LinkTarget $page ) {
475  if ( $page->getNamespace() == NS_SPECIAL ) {
476  list( $thisName, /* $subpage */ ) =
477  $this->specialPageFactory->resolveAlias( $page->getDBkey() );
478  if ( $name == $thisName ) {
479  return true;
480  }
481  }
482  return false;
483  }
484 
502  private function checkUserBlock(
503  $action,
504  User $user,
505  $errors,
506  $rigor,
507  $short,
508  LinkTarget $page
509  ) {
510  // Account creation blocks handled at userlogin.
511  // Unblocking handled in SpecialUnblock
512  if ( $rigor === self::RIGOR_QUICK || in_array( $action, [ 'createaccount', 'unblock' ] ) ) {
513  return $errors;
514  }
515 
516  // Optimize for a very common case
517  if ( $action === 'read' && !$this->blockDisablesLogin ) {
518  return $errors;
519  }
520 
521  if ( $this->emailConfirmToEdit
522  && !$user->isEmailConfirmed()
523  && $action === 'edit'
524  ) {
525  $errors[] = [ 'confirmedittext' ];
526  }
527 
528  $useReplica = ( $rigor !== self::RIGOR_SECURE );
529  $block = $user->getBlock( $useReplica );
530 
531  // If the user does not have a block, or the block they do have explicitly
532  // allows the action (like "read" or "upload").
533  if ( !$block || $block->appliesToRight( $action ) === false ) {
534  return $errors;
535  }
536 
537  // Determine if the user is blocked from this action on this page.
538  // What gets passed into this method is a user right, not an action name.
539  // There is no way to instantiate an action by restriction. However, this
540  // will get the action where the restriction is the same. This may result
541  // in actions being blocked that shouldn't be.
542  $actionObj = null;
543  if ( Action::exists( $action ) ) {
544  // TODO: this drags a ton of dependencies in, would be good to avoid WikiPage
545  // instantiation and decouple it creating an ActionPermissionChecker interface
546  $wikiPage = WikiPage::factory( Title::newFromLinkTarget( $page, 'clone' ) );
547  // Creating an action will perform several database queries to ensure that
548  // the action has not been overridden by the content type.
549  // FIXME: avoid use of RequestContext since it drags in User and Title dependencies
550  // probably we may use fake context object since it's unlikely that Action uses it
551  // anyway. It would be nice if we could avoid instantiating the Action at all.
552  $actionObj = Action::factory( $action, $wikiPage, RequestContext::getMain() );
553  // Ensure that the retrieved action matches the restriction.
554  if ( $actionObj && $actionObj->getRestriction() !== $action ) {
555  $actionObj = null;
556  }
557  }
558 
559  // If no action object is returned, assume that the action requires unblock
560  // which is the default.
561  if ( !$actionObj || $actionObj->requiresUnblock() ) {
562  if ( $this->isBlockedFrom( $user, $page, $useReplica ) ) {
563  // @todo FIXME: Pass the relevant context into this function.
564  $errors[] = $block->getPermissionsError( RequestContext::getMain() );
565  }
566  }
567 
568  return $errors;
569  }
570 
589  private function checkQuickPermissions(
590  $action,
591  User $user,
592  $errors,
593  $rigor,
594  $short,
595  LinkTarget $page
596  ) {
597  // TODO: remove when LinkTarget usage will expand further
598  $page = Title::newFromLinkTarget( $page );
599 
600  if ( !Hooks::run( 'TitleQuickPermissions',
601  [ $page, $user, $action, &$errors, ( $rigor !== self::RIGOR_QUICK ), $short ] )
602  ) {
603  return $errors;
604  }
605 
606  $isSubPage = $this->nsInfo->hasSubpages( $page->getNamespace() ) ?
607  strpos( $page->getText(), '/' ) !== false : false;
608 
609  if ( $action == 'create' ) {
610  if (
611  ( $this->nsInfo->isTalk( $page->getNamespace() ) &&
612  !$user->isAllowed( 'createtalk' ) ) ||
613  ( !$this->nsInfo->isTalk( $page->getNamespace() ) &&
614  !$user->isAllowed( 'createpage' ) )
615  ) {
616  $errors[] = $user->isAnon() ? [ 'nocreatetext' ] : [ 'nocreate-loggedin' ];
617  }
618  } elseif ( $action == 'move' ) {
619  if ( !$user->isAllowed( 'move-rootuserpages' )
620  && $page->getNamespace() == NS_USER && !$isSubPage ) {
621  // Show user page-specific message only if the user can move other pages
622  $errors[] = [ 'cant-move-user-page' ];
623  }
624 
625  // Check if user is allowed to move files if it's a file
626  if ( $page->getNamespace() == NS_FILE && !$user->isAllowed( 'movefile' ) ) {
627  $errors[] = [ 'movenotallowedfile' ];
628  }
629 
630  // Check if user is allowed to move category pages if it's a category page
631  if ( $page->getNamespace() == NS_CATEGORY && !$user->isAllowed( 'move-categorypages' ) ) {
632  $errors[] = [ 'cant-move-category-page' ];
633  }
634 
635  if ( !$user->isAllowed( 'move' ) ) {
636  // User can't move anything
637  $userCanMove = User::groupHasPermission( 'user', 'move' );
638  $autoconfirmedCanMove = User::groupHasPermission( 'autoconfirmed', 'move' );
639  if ( $user->isAnon() && ( $userCanMove || $autoconfirmedCanMove ) ) {
640  // custom message if logged-in users without any special rights can move
641  $errors[] = [ 'movenologintext' ];
642  } else {
643  $errors[] = [ 'movenotallowed' ];
644  }
645  }
646  } elseif ( $action == 'move-target' ) {
647  if ( !$user->isAllowed( 'move' ) ) {
648  // User can't move anything
649  $errors[] = [ 'movenotallowed' ];
650  } elseif ( !$user->isAllowed( 'move-rootuserpages' )
651  && $page->getNamespace() == NS_USER && !$isSubPage ) {
652  // Show user page-specific message only if the user can move other pages
653  $errors[] = [ 'cant-move-to-user-page' ];
654  } elseif ( !$user->isAllowed( 'move-categorypages' )
655  && $page->getNamespace() == NS_CATEGORY ) {
656  // Show category page-specific message only if the user can move other pages
657  $errors[] = [ 'cant-move-to-category-page' ];
658  }
659  } elseif ( !$user->isAllowed( $action ) ) {
660  $errors[] = $this->missingPermissionError( $action, $short );
661  }
662 
663  return $errors;
664  }
665 
684  private function checkPageRestrictions(
685  $action,
686  User $user,
687  $errors,
688  $rigor,
689  $short,
690  LinkTarget $page
691  ) {
692  // TODO: remove & rework upon further use of LinkTarget
693  $page = Title::newFromLinkTarget( $page );
694  foreach ( $page->getRestrictions( $action ) as $right ) {
695  // Backwards compatibility, rewrite sysop -> editprotected
696  if ( $right == 'sysop' ) {
697  $right = 'editprotected';
698  }
699  // Backwards compatibility, rewrite autoconfirmed -> editsemiprotected
700  if ( $right == 'autoconfirmed' ) {
701  $right = 'editsemiprotected';
702  }
703  if ( $right == '' ) {
704  continue;
705  }
706  if ( !$user->isAllowed( $right ) ) {
707  $errors[] = [ 'protectedpagetext', $right, $action ];
708  } elseif ( $page->areRestrictionsCascading() && !$user->isAllowed( 'protect' ) ) {
709  $errors[] = [ 'protectedpagetext', 'protect', $action ];
710  }
711  }
712 
713  return $errors;
714  }
715 
733  $action,
734  User $user,
735  $errors,
736  $rigor,
737  $short,
738  LinkTarget $page
739  ) {
740  // TODO: remove & rework upon further use of LinkTarget
741  $page = Title::newFromLinkTarget( $page );
742  if ( $rigor !== self::RIGOR_QUICK && !$page->isUserConfigPage() ) {
743  # We /could/ use the protection level on the source page, but it's
744  # fairly ugly as we have to establish a precedence hierarchy for pages
745  # included by multiple cascade-protected pages. So just restrict
746  # it to people with 'protect' permission, as they could remove the
747  # protection anyway.
748  list( $cascadingSources, $restrictions ) = $page->getCascadeProtectionSources();
749  # Cascading protection depends on more than this page...
750  # Several cascading protected pages may include this page...
751  # Check each cascading level
752  # This is only for protection restrictions, not for all actions
753  if ( isset( $restrictions[$action] ) ) {
754  foreach ( $restrictions[$action] as $right ) {
755  // Backwards compatibility, rewrite sysop -> editprotected
756  if ( $right == 'sysop' ) {
757  $right = 'editprotected';
758  }
759  // Backwards compatibility, rewrite autoconfirmed -> editsemiprotected
760  if ( $right == 'autoconfirmed' ) {
761  $right = 'editsemiprotected';
762  }
763  if ( $right != '' && !$user->isAllowedAll( 'protect', $right ) ) {
764  $wikiPages = '';
765  foreach ( $cascadingSources as $wikiPage ) {
766  $wikiPages .= '* [[:' . $wikiPage->getPrefixedText() . "]]\n";
767  }
768  $errors[] = [ 'cascadeprotected', count( $cascadingSources ), $wikiPages, $action ];
769  }
770  }
771  }
772  }
773 
774  return $errors;
775  }
776 
794  private function checkActionPermissions(
795  $action,
796  User $user,
797  $errors,
798  $rigor,
799  $short,
800  LinkTarget $page
801  ) {
803 
804  // TODO: remove & rework upon further use of LinkTarget
805  $page = Title::newFromLinkTarget( $page );
806 
807  if ( $action == 'protect' ) {
808  if ( count( $this->getPermissionErrorsInternal( 'edit', $user, $page, $rigor, true ) ) ) {
809  // If they can't edit, they shouldn't protect.
810  $errors[] = [ 'protect-cantedit' ];
811  }
812  } elseif ( $action == 'create' ) {
813  $title_protection = $page->getTitleProtection();
814  if ( $title_protection ) {
815  if ( $title_protection['permission'] == ''
816  || !$user->isAllowed( $title_protection['permission'] )
817  ) {
818  $errors[] = [
819  'titleprotected',
820  // TODO: get rid of the User dependency
821  User::whoIs( $title_protection['user'] ),
822  $title_protection['reason']
823  ];
824  }
825  }
826  } elseif ( $action == 'move' ) {
827  // Check for immobile pages
828  if ( !$this->nsInfo->isMovable( $page->getNamespace() ) ) {
829  // Specific message for this case
830  $errors[] = [ 'immobile-source-namespace', $page->getNsText() ];
831  } elseif ( !$page->isMovable() ) {
832  // Less specific message for rarer cases
833  $errors[] = [ 'immobile-source-page' ];
834  }
835  } elseif ( $action == 'move-target' ) {
836  if ( !$this->nsInfo->isMovable( $page->getNamespace() ) ) {
837  $errors[] = [ 'immobile-target-namespace', $page->getNsText() ];
838  } elseif ( !$page->isMovable() ) {
839  $errors[] = [ 'immobile-target-page' ];
840  }
841  } elseif ( $action == 'delete' ) {
842  $tempErrors = $this->checkPageRestrictions( 'edit', $user, [], $rigor, true, $page );
843  if ( !$tempErrors ) {
844  $tempErrors = $this->checkCascadingSourcesRestrictions( 'edit',
845  $user, $tempErrors, $rigor, true, $page );
846  }
847  if ( $tempErrors ) {
848  // If protection keeps them from editing, they shouldn't be able to delete.
849  $errors[] = [ 'deleteprotected' ];
850  }
851  if ( $rigor !== self::RIGOR_QUICK && $wgDeleteRevisionsLimit
852  && !$this->userCan( 'bigdelete', $user, $page ) && $page->isBigDeletion()
853  ) {
854  $errors[] = [ 'delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ];
855  }
856  } elseif ( $action === 'undelete' ) {
857  if ( count( $this->getPermissionErrorsInternal( 'edit', $user, $page, $rigor, true ) ) ) {
858  // Undeleting implies editing
859  $errors[] = [ 'undelete-cantedit' ];
860  }
861  if ( !$page->exists()
862  && count( $this->getPermissionErrorsInternal( 'create', $user, $page, $rigor, true ) )
863  ) {
864  // Undeleting where nothing currently exists implies creating
865  $errors[] = [ 'undelete-cantcreate' ];
866  }
867  }
868  return $errors;
869  }
870 
888  $action,
889  User $user,
890  $errors,
891  $rigor,
892  $short,
893  LinkTarget $page
894  ) {
895  // TODO: remove & rework upon further use of LinkTarget
896  $page = Title::newFromLinkTarget( $page );
897 
898  # Only 'createaccount' can be performed on special pages,
899  # which don't actually exist in the DB.
900  if ( $page->getNamespace() == NS_SPECIAL && $action !== 'createaccount' ) {
901  $errors[] = [ 'ns-specialprotected' ];
902  }
903 
904  # Check $wgNamespaceProtection for restricted namespaces
905  if ( $page->isNamespaceProtected( $user ) ) {
906  $ns = $page->getNamespace() == NS_MAIN ?
907  wfMessage( 'nstab-main' )->text() : $page->getNsText();
908  $errors[] = $page->getNamespace() == NS_MEDIAWIKI ?
909  [ 'protectedinterface', $action ] : [ 'namespaceprotected', $ns, $action ];
910  }
911 
912  return $errors;
913  }
914 
931  private function checkSiteConfigPermissions(
932  $action,
933  User $user,
934  $errors,
935  $rigor,
936  $short,
937  LinkTarget $page
938  ) {
939  // TODO: remove & rework upon further use of LinkTarget
940  $page = Title::newFromLinkTarget( $page );
941 
942  if ( $action != 'patrol' ) {
943  $error = null;
944  // Sitewide CSS/JSON/JS changes, like all NS_MEDIAWIKI changes, also require the
945  // editinterface right. That's implemented as a restriction so no check needed here.
946  if ( $page->isSiteCssConfigPage() && !$user->isAllowed( 'editsitecss' ) ) {
947  $error = [ 'sitecssprotected', $action ];
948  } elseif ( $page->isSiteJsonConfigPage() && !$user->isAllowed( 'editsitejson' ) ) {
949  $error = [ 'sitejsonprotected', $action ];
950  } elseif ( $page->isSiteJsConfigPage() && !$user->isAllowed( 'editsitejs' ) ) {
951  $error = [ 'sitejsprotected', $action ];
952  } elseif ( $page->isRawHtmlMessage() ) {
953  // Raw HTML can be used to deploy CSS or JS so require rights for both.
954  if ( !$user->isAllowed( 'editsitejs' ) ) {
955  $error = [ 'sitejsprotected', $action ];
956  } elseif ( !$user->isAllowed( 'editsitecss' ) ) {
957  $error = [ 'sitecssprotected', $action ];
958  }
959  }
960 
961  if ( $error ) {
962  if ( $user->isAllowed( 'editinterface' ) ) {
963  // Most users / site admins will probably find out about the new, more restrictive
964  // permissions by failing to edit something. Give them more info.
965  // TODO remove this a few release cycles after 1.32
966  $error = [ 'interfaceadmin-info', wfMessage( $error[0], $error[1] ) ];
967  }
968  $errors[] = $error;
969  }
970  }
971 
972  return $errors;
973  }
974 
991  private function checkUserConfigPermissions(
992  $action,
993  User $user,
994  $errors,
995  $rigor,
996  $short,
997  LinkTarget $page
998  ) {
999  // TODO: remove & rework upon further use of LinkTarget
1000  $page = Title::newFromLinkTarget( $page );
1001 
1002  # Protect css/json/js subpages of user pages
1003  # XXX: this might be better using restrictions
1004 
1005  if ( $action === 'patrol' ) {
1006  return $errors;
1007  }
1008 
1009  if ( preg_match( '/^' . preg_quote( $user->getName(), '/' ) . '\//', $page->getText() ) ) {
1010  // Users need editmyuser* to edit their own CSS/JSON/JS subpages.
1011  if (
1012  $page->isUserCssConfigPage()
1013  && !$user->isAllowedAny( 'editmyusercss', 'editusercss' )
1014  ) {
1015  $errors[] = [ 'mycustomcssprotected', $action ];
1016  } elseif (
1017  $page->isUserJsonConfigPage()
1018  && !$user->isAllowedAny( 'editmyuserjson', 'edituserjson' )
1019  ) {
1020  $errors[] = [ 'mycustomjsonprotected', $action ];
1021  } elseif (
1022  $page->isUserJsConfigPage()
1023  && !$user->isAllowedAny( 'editmyuserjs', 'edituserjs' )
1024  ) {
1025  $errors[] = [ 'mycustomjsprotected', $action ];
1026  }
1027  } else {
1028  // Users need editmyuser* to edit their own CSS/JSON/JS subpages, except for
1029  // deletion/suppression which cannot be used for attacks and we want to avoid the
1030  // situation where an unprivileged user can post abusive content on their subpages
1031  // and only very highly privileged users could remove it.
1032  if ( !in_array( $action, [ 'delete', 'deleterevision', 'suppressrevision' ], true ) ) {
1033  if (
1034  $page->isUserCssConfigPage()
1035  && !$user->isAllowed( 'editusercss' )
1036  ) {
1037  $errors[] = [ 'customcssprotected', $action ];
1038  } elseif (
1039  $page->isUserJsonConfigPage()
1040  && !$user->isAllowed( 'edituserjson' )
1041  ) {
1042  $errors[] = [ 'customjsonprotected', $action ];
1043  } elseif (
1044  $page->isUserJsConfigPage()
1045  && !$user->isAllowed( 'edituserjs' )
1046  ) {
1047  $errors[] = [ 'customjsprotected', $action ];
1048  }
1049  }
1050  }
1051 
1052  return $errors;
1053  }
1054 
1055 }
getPermissionErrors( $action, User $user, LinkTarget $page, $rigor=self::RIGOR_SECURE, $ignoreErrors=[])
Can $user perform $action on a page?
checkPageRestrictions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check against page_restrictions table requirements on this page.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:138
isHidden()
Check if user account is hidden.
Definition: User.php:2260
getPermissionErrorsInternal( $action, User $user, LinkTarget $page, $rigor=self::RIGOR_SECURE, $short=false)
Can $user perform $action on a page? This is an internal function, with multiple levels of checks dep...
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
string [] $whitelistRead
List of pages names anonymous user may see.
isAllowedAll()
Definition: User.php:3708
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition: User.php:903
checkSiteConfigPermissions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check sitewide CSS/JSON/JS permissions.
checkReadPermissions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check that the user is allowed to read this page.
const NS_MAIN
Definition: Defines.php:60
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
static isEveryoneAllowed( $right)
Check if all users may be assumed to have the given permission.
Definition: User.php:4951
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
isAllowedAny()
Check if user is allowed to access a feature / make an action.
Definition: User.php:3693
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:97
getBlock( $fromReplica=true)
Get the block affecting the user, or null if the user is not blocked.
Definition: User.php:2141
userCan( $action, User $user, LinkTarget $page, $rigor=self::RIGOR_SECURE)
Can $user perform $action on a page?
const NS_SPECIAL
Definition: Defines.php:49
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
static exists( $name)
Check if a given action is recognised, even if it&#39;s disabled.
Definition: Action.php:170
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2304
getNamespace()
Get the namespace index.
missingPermissionError( $action, $short)
Get a description array when the user doesn&#39;t have the right to perform $action (i.e.
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1980
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
isEmailConfirmed()
Is this user&#39;s e-mail address valid-looking and confirmed within limits of the current site configura...
Definition: User.php:4775
$wgLang
Definition: Setup.php:931
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
Factory for handling the special page list and generating SpecialPage objects.
static getMain()
Get the RequestContext object associated with the main request.
isAnon()
Get whether the user is anonymous.
Definition: User.php:3668
isAllowUsertalk()
Checks if usertalk is allowed.
Definition: User.php:5546
isBlockedFrom(User $user, LinkTarget $page, $fromReplica=false)
Check if user is blocked from editing a particular article.
checkActionPermissions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check action permissions not already checked in checkQuickPermissions.
resultToError( $errors, $result)
Add the resulting error code to the errors array.
getDBkey()
Get the main part with underscores.
checkUserConfigPermissions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check CSS/JSON/JS sub-page permissions.
const NS_CATEGORY
Definition: Defines.php:74
isAllowed( $action='')
Internal mechanics of testing a permission.
Definition: User.php:3723
bool $blockDisablesLogin
If set to true, blocked users will no longer be allowed to log in.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
__construct(SpecialPageFactory $specialPageFactory, $whitelistRead, $whitelistReadRegexp, $emailConfirmToEdit, $blockDisablesLogin, NamespaceInfo $nsInfo)
const NS_FILE
Definition: Defines.php:66
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
const NS_MEDIAWIKI
Definition: Defines.php:68
checkUserBlock( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check that the user isn&#39;t blocked from editing.
static groupHasPermission( $group, $role)
Check, if the given group has the given permission.
Definition: User.php:4931
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:271
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
checkPermissionHooks( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check various permission hooks.
static newFatalPermissionDeniedStatus( $permission)
Factory function for fatal permission-denied errors.
Definition: User.php:5492
$wgDeleteRevisionsLimit
Optional to restrict deletion of pages with higher revision counts to users with the &#39;bigdelete&#39; perm...
bool $emailConfirmToEdit
Require users to confirm email address before they can edit.
getText()
Returns the link in text form, without namespace prefix or fragment.
checkQuickPermissions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Permissions checks that fail most often, and which are easiest to test.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
string [] $whitelistReadRegexp
Whitelists publicly readable titles with regular expressions.
checkCascadingSourcesRestrictions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check restrictions on cascading pages.
checkSpecialsAndNSPermissions( $action, User $user, $errors, $rigor, $short, LinkTarget $page)
Check permissions on special pages & namespaces.
getTalkPage()
Get this user&#39;s talk page title.
Definition: User.php:4425
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
isSameSpecialPage( $name, LinkTarget $page)
Returns true if this title resolves to the named special page.
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200