r85268 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85267‎ | r85268 | r85269 >
Date:19:01, 3 April 2011
Author:maxsem
Status:ok
Tags:
Comment:
Gadgets: bug 12211 - show in preferences only gadgets the user is able to use
Modified paths:
  • /trunk/extensions/Gadgets/Gadgets.i18n.php (modified) (history)
  • /trunk/extensions/Gadgets/Gadgets_body.php (modified) (history)
  • /trunk/extensions/Gadgets/Gadgets_tests.php (modified) (history)
  • /trunk/extensions/Gadgets/SpecialGadgets.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Gadgets/Gadgets_body.php
@@ -41,17 +41,21 @@
4242
4343 $options = array();
4444 foreach( $gadgets as $section => $thisSection ) {
 45+ $available = array();
 46+ foreach( $thisSection as $gadget ) {
 47+ if ( $gadget->isAllowed( $user ) ) {
 48+ $gname = $gadget->getName();
 49+ $available[wfMsgExt( "gadget-$gname", 'parseinline' )] = $gname;
 50+ }
 51+ }
4552 if ( $section !== '' ) {
4653 $section = wfMsgExt( "gadget-section-$section", 'parseinline' );
47 - $options[$section] = array();
48 - $destination = &$options[$section];
 54+ if ( count ( $available ) ) {
 55+ $options[$section] = $available;
 56+ }
4957 } else {
50 - $destination = &$options;
 58+ $options = array_merge( $options, $available );
5159 }
52 - foreach( $thisSection as $gadget ) {
53 - $gname = $gadget->getName();
54 - $destination[wfMsgExt( "gadget-$gname", 'parseinline' )] = $gname;
55 - }
5660 }
5761
5862 $preferences['gadgets-intro'] =
@@ -118,7 +122,7 @@
119123 $pages = array();
120124
121125 foreach ( $gadgets as $gadget ) {
122 - if ( $gadget->isEnabled() ) {
 126+ if ( $gadget->isEnabled( $wgUser ) && $gadget->isAllowed( $wgUser ) ) {
123127 if ( $gadget->hasModule() ) {
124128 $out->addModules( $gadget->getModuleName() );
125129 }
@@ -183,7 +187,7 @@
184188 /**
185189 * Increment this when changing class structure
186190 */
187 - const GADGET_CLASS_VERSION = 2;
 191+ const GADGET_CLASS_VERSION = 3;
188192
189193 private $version = self::GADGET_CLASS_VERSION,
190194 $scripts = array(),
@@ -191,7 +195,8 @@
192196 $dependencies = array(),
193197 $name,
194198 $definition,
195 - $resourceLoaded = false;
 199+ $resourceLoaded = false,
 200+ $requiredRights = array();
196201
197202 /**
198203 * Creates an instance of this class from definition in MediaWiki:Gadgets-definition
@@ -226,6 +231,9 @@
227232 case 'dependencies':
228233 $gadget->dependencies = $params;
229234 break;
 235+ case 'rights':
 236+ $gadget->requiredRights = $params;
 237+ break;
230238 }
231239 }
232240 foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
@@ -262,14 +270,26 @@
263271 }
264272
265273 /**
266 - * @return Boolean: Whether this gadget is enabled for current user
 274+ * Checks whether this gadget is enabled for given user
 275+ *
 276+ * @param $user User: user to check against
 277+ * @return Boolean
267278 */
268 - public function isEnabled() {
269 - global $wgUser;
270 - return (bool)$wgUser->getOption( "gadget-{$this->name}" );
 279+ public function isEnabled( $user ) {
 280+ return (bool)$user->getOption( "gadget-{$this->name}" );
271281 }
272282
273283 /**
 284+ * Checks whether given user has permissions to use this gadget
 285+ *
 286+ * @param $user User: user to check against
 287+ * @return Boolean
 288+ */
 289+ public function isAllowed( $user ) {
 290+ return count( array_intersect( $this->requiredRights, $user->getRights() ) ) == count( $this->requiredRights );
 291+ }
 292+
 293+ /**
274294 * @return Boolean: Whether all of this gadget's JS components support ResourceLoader
275295 */
276296 public function supportsResourceLoader() {
@@ -354,6 +374,14 @@
355375 }
356376
357377 /**
 378+ * Returns array of permissions required by this gadget
 379+ * @return Array
 380+ */
 381+ public function getRequiredRights() {
 382+ return $this->requiredRights;
 383+ }
 384+
 385+ /**
358386 * Loads and returns a list of all gadgets
359387 * @return Mixed: Array of gadgets or false
360388 */
Index: trunk/extensions/Gadgets/Gadgets.i18n.php
@@ -33,6 +33,7 @@
3434 'gadgets-pagetext' => "Below is a list of special gadgets users can enable on their [[Special:Preferences|preferences page]], as defined by the [[MediaWiki:Gadgets-definition|definitions]].
3535 This overview provides easy access to the system message pages that define each gadget's description and code.",
3636 'gadgets-uses' => 'Uses',
 37+ 'gadgets-required-rights' => 'Requires the {{PLURAL:$2|$1 right|following rights: $1}}.',
3738 'gadgets-export' => 'Export',
3839 'gadgets-export-title' => 'Gadget export',
3940 'gadgets-not-found' => 'Gadget "$1" not found.',
Index: trunk/extensions/Gadgets/SpecialGadgets.php
@@ -114,6 +114,12 @@
115115 $lnk[] = $skin->link( $t, htmlspecialchars( $t->getText() ) );
116116 }
117117 $wgOut->addHTML( $wgLang->commaList( $lnk ) );
 118+ $rights = $gadget->getRequiredRights();
 119+ if ( count( $rights ) ) {
 120+ $wgOut->addHTML( '<br />' );
 121+ $wgOut->addWikiMsg( 'gadgets-required-rights', $wgLang->commaList( $rights ), count( $rights ) );
 122+ }
 123+
118124 $wgOut->addHTML( Xml::closeElement( 'li' ) . "\n" );
119125 }
120126 }
Index: trunk/extensions/Gadgets/Gadgets_tests.php
@@ -43,4 +43,24 @@
4444 $this->assertTrue( $g->supportsResourceLoader() );
4545 $this->assertEquals( array( 'jquery.ui' ), $g->getDependencies() );
4646 }
 47+
 48+ function testPreferences() {
 49+ global $wgUser, $wgOut;
 50+ $prefs = array();
 51+ $wgOut->setTitle( Title::newFromText( 'test' ) );
 52+
 53+ Gadget::loadStructuredList( '* foo | foo.js
 54+==keep-section1==
 55+* bar| bar.js
 56+==remove-section==
 57+* baz [rights=embezzle] |baz.js
 58+==keep-section2==
 59+* quux [rights=read] | quux.js' );
 60+ $this->assertTrue( GadgetHooks::getPreferences( $wgUser, $prefs ), 'GetPrefences hook should return true' );
 61+
 62+ $options = $prefs['gadgets']['options'];
 63+ $this->assertFalse( isset( $options['&lt;gadget-section-remove-section&gt;'] ), 'Must not show empty sections' );
 64+ $this->assertTrue( isset( $options['&lt;gadget-section-keep-section1&gt;'] ) );
 65+ $this->assertTrue( isset( $options['&lt;gadget-section-keep-section2&gt;'] ) );
 66+ }
4767 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r100344Display friendly names of rights on Special:Gadgetsliangent13:48, 20 October 2011

Status & tagging log