(bug 38151) Implement mw.user.getRights and getGroups
authorAlex Monk <krenair@gmail.com>
Thu, 5 Jul 2012 16:27:41 +0000 (17:27 +0100)
committerTimo Tijhof <ttijhof@wikimedia.org>
Wed, 1 Aug 2012 04:13:20 +0000 (21:13 -0700)
* Also deprecate name() and anonymous()
  in favour of getName() and isAnon() (to match User.php), just
  like getRights and getGroups are now as well.

* Added unit tests for all.

Change-Id: I5970be9e8593589018152f4878f168b9b85ca5d5

RELEASE-NOTES-1.20
resources/Resources.php
resources/mediawiki/mediawiki.user.js
tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js

index 19a4fed..25668b1 100644 (file)
@@ -105,6 +105,9 @@ upgrade PHP if you have not done so prior to upgrading MediaWiki.
   and 'subcats'
 * (bug 38362) Make Special:Listuser includeable on wiki pages.
 * Added support in jquery.localize for placeholder attributes.
+* (bug 38151) Implemented mw.user.getRights for getting and caching the current
+  user's user rights.
+* Implemented mw.user.getGroups for getting and caching user groups.
 
 === Bug fixes in 1.20 ===
 * (bug 30245) Use the correct way to construct a log page title.
@@ -238,6 +241,8 @@ changes to languages because of Bugzilla reports.
   and only applies to MyISAM or similar DBs. Those should only be used
   for archived sites anyway. We can't get edit conflicts on such sites,
   so the WikiPage code wasn't useful there either.
+* Deprecated mw.user.name in favour of mw.user.getName.
+* Deprecated mw.user.anonymous in favour of mw.user.isAnon.
 
 == Compatibility ==
 
index 337367f..380a099 100644 (file)
@@ -613,6 +613,7 @@ return array(
                'scripts' => 'resources/mediawiki/mediawiki.user.js',
                'dependencies' => array(
                        'jquery.cookie',
+                       'mediawiki.api',
                ),
        ),
        'mediawiki.util' => array(
index 7a15e29..8c6e90c 100644 (file)
@@ -12,6 +12,9 @@
                /* Private Members */
 
                var that = this;
+               var api = new mw.Api();
+               var groupsDeferred;
+               var rightsDeferred;
 
                /* Public Members */
 
                 *
                 * @return Mixed: User name string or null if users is anonymous
                 */
-               this.name = function() {
+               this.getName = function () {
                        return mw.config.get( 'wgUserName' );
                };
 
+               /**
+                * @deprecated since 1.20 use mw.user.getName() instead
+                */
+               this.name = function () {
+                       return this.getName();
+               };
+
                /**
                 * Checks if the current user is anonymous.
                 *
                 * @return Boolean
                 */
-               this.anonymous = function() {
-                       return that.name() ? false : true;
+               this.isAnon = function () {
+                       return that.getName() === null;
+               };
+
+               /**
+                * @deprecated since 1.20 use mw.user.isAnon() instead
+                */
+               this.anonymous = function () {
+                       return that.isAnon();
                };
 
                /**
                 * @return String: User name or random session ID
                 */
                this.id = function() {
-                       var name = that.name();
+                       var name = that.getName();
                        if ( name ) {
                                return name;
                        }
                        }
                        return bucket;
                };
+
+               /**
+                * Gets the current user's groups.
+                */
+               this.getGroups = function ( callback ) {
+                       if ( groupsDeferred ) {
+                               groupsDeferred.always( callback );
+                               return;
+                       }
+
+                       groupsDeferred = $.Deferred();
+                       groupsDeferred.always( callback );
+                       api.get( {
+                               action: 'query',
+                               meta: 'userinfo',
+                               uiprop: 'groups'
+                       } ).done( function ( data ) {
+                               if ( data.query && data.query.userinfo && data.query.userinfo.groups ) {
+                                       groupsDeferred.resolve( data.query.userinfo.groups );
+                               } else {
+                                       groupsDeferred.reject( [] );
+                               }
+                       } ).fail( function ( data ) {
+                                       groupsDeferred.reject( [] );
+                       } );
+               };
+
+               /**
+                * Gets the current user's rights.
+                */
+               this.getRights = function ( callback ) {
+                       if ( rightsDeferred ) {
+                               rightsDeferred.always( callback );
+                               return;
+                       }
+
+                       rightsDeferred = $.Deferred();
+                       rightsDeferred.always( callback );
+                       api.get( {
+                               action: 'query',
+                               meta: 'userinfo',
+                               uiprop: 'rights'
+                       } ).done( function ( data ) {
+                               if ( data.query && data.query.userinfo && data.query.userinfo.rights ) {
+                                       rightsDeferred.resolve( data.query.userinfo.rights );
+                               } else {
+                                       rightsDeferred.reject( [] );
+                               }
+                       } ).fail( function ( data ) {
+                               rightsDeferred.reject( [] );
+                       } );
+               };
        }
 
        // Extend the skeleton mw.user from mediawiki.js
index 79768da..c823baf 100644 (file)
@@ -6,7 +6,7 @@ QUnit.test( 'options', 1, function ( assert ) {
        assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
 });
 
-QUnit.test( 'User login status', 5, function ( assert ) {
+QUnit.test( 'user status', 9, function ( assert ) {
        /**
         * Tests can be run under three different conditions:
         *   1) From tests/qunit/index.html, user will be anonymous.
@@ -15,18 +15,42 @@ QUnit.test( 'User login status', 5, function ( assert ) {
         */
 
        // Forge an anonymous user:
-       mw.config.set( 'wgUserName', null);
+       mw.config.set( 'wgUserName', null );
 
-       assert.strictEqual( mw.user.name(), null, 'user.name should return null when anonymous' );
-       assert.ok( mw.user.anonymous(), 'user.anonymous should reutrn true when anonymous' );
+       assert.strictEqual( mw.user.getName(), null, 'user.getName() returns null when anonymous' );
+       assert.strictEqual( mw.user.name(), null, 'user.name() compatibility' );
+       assert.assertTrue( mw.user.isAnon(), 'user.isAnon() returns true when anonymous' );
+       assert.assertTrue( mw.user.anonymous(), 'user.anonymous() compatibility' );
 
        // Not part of startUp module
        mw.config.set( 'wgUserName', 'John' );
 
-       assert.equal( mw.user.name(), 'John', 'user.name returns username when logged-in' );
-       assert.ok( !mw.user.anonymous(), 'user.anonymous returns false when logged-in' );
+       assert.equal( mw.user.getName(), 'John', 'user.getName() returns username when logged-in' );
+       assert.equal( mw.user.name(), 'John', 'user.name() compatibility' );
+       assert.assertFalse( mw.user.isAnon(), 'user.isAnon() returns false when logged-in' );
+       assert.assertFalse( mw.user.anonymous(), 'user.anonymous() compatibility' );
 
        assert.equal( mw.user.id(), 'John', 'user.id Returns username when logged-in' );
 });
 
+QUnit.asyncTest( 'getGroups', 3, function ( assert ) {
+       mw.user.getGroups( function ( groups ) {
+               // First group should always be '*'
+               assert.equal( $.type( groups ), 'array', 'Callback gets an array' );
+               assert.equal( groups[0], '*', '"*"" is the first group' );
+               // Sort needed because of different methods if creating the arrays,
+               // only the content matters.
+               assert.deepEqual( groups.sort(), mw.config.get( 'wgUserGroups' ).sort(), 'Array contains all groups, just like wgUserGroups' );
+               QUnit.start();
+       });
+});
+
+QUnit.asyncTest( 'getRights', 1, function ( assert ) {
+       mw.user.getRights( function ( rights ) {
+               // First group should always be '*'
+               assert.equal( $.type( rights ), 'array', 'Callback gets an array' );
+               QUnit.start();
+       });
+});
+
 }( mediaWiki ) );