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.
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 ==
/* 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
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.
*/
// 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 ) );