User:Void/userRights.js

From Miraheze Meta, Miraheze's central coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//<nowiki>
// Script for changing userrights on meta, or other wikis
// TODO: target user must be steward to recieve CU, OS, or Steward
if( mw.config.get('wgGlobalGroups').includes('steward') ) {
mw.loader.using( ['mediawiki.util', 'mediawiki.api', 'oojs-ui'] ).then( function() {
	var link = mw.util.addPortletLink(
		'p-personal',
		'#',
		'UserRights',
		'pt-userrights',
		'Edit local or interwiki user rights',
		null,
		'#pt-preferences'
	);

	var api = new mw.Api();

	var userBox = new OO.ui.TextInputWidget( {
		value: mw.config.get('wgUserName'),
		id: 'ur-username'
	} );

	var wikiSelect = new OO.ui.MenuTagMultiselectWidget( {
		allowArbitrary: true,
		selected: [mw.config.get('wgDBname')],
		options: [
			/*{ data: 'metawiki' },*/
			{ data: 'testwiki' },
			{ data: 'loginwiki' },
		],
		id: 'ur-wiki'
	} );

	var sysopcb = new OO.ui.CheckboxInputWidget( { id: 'ur-sysop' } );
	var cratcb = new OO.ui.CheckboxInputWidget( { id: 'ur-bureaucrat' } );
	var oscb = new OO.ui.CheckboxInputWidget( { id: 'ur-oversight' } );
	var cucb = new OO.ui.CheckboxInputWidget( { id: 'ur-checkuser' } );
	var stewardcb = new OO.ui.CheckboxInputWidget( { id: 'ur-steward' } );

	var expiryBox = new OO.ui.TextInputWidget( {
		value: 'infinite',
		id: 'ur-expiry'
	} );

	var summaryInput = new OO.ui.ComboBoxInputWidget( {
		options: [
			{ data: 'Redacting information' },
			{ data: 'Investigating possible abuse' },
			{ data: 'Revoking due to abuse' },
			{ data: 'Assigning local permissions' },
		],
		id: 'ur-summary'
	} );

	var addButton = new OO.ui.ButtonOptionWidget( {
		label: 'Add groups',
		flags: 'progressive'
	} );

	var removeButton = new OO.ui.ButtonOptionWidget( {
		label: 'Remove groups',
		flags: 'destructive'
	} );

	var buttonSelection = new OO.ui.ButtonSelectWidget( {
		items: [ removeButton, addButton ]
	} );
	buttonSelection.on( 'select', function ( items ) {
		removeButton.setFlags( {
			destructive: true,
			primary: removeButton.isSelected()
		} );
		addButton.setFlags( {
			progressive: true,
			primary: addButton.isSelected()
		} );
	} );
	buttonSelection.selectItem( addButton ); // Add by default

	function UserRightsProc( config ) {
		UserRightsProc.super.call( this, config );
	}
	OO.inheritClass( UserRightsProc, OO.ui.ProcessDialog );
	UserRightsProc.static.name = 'userRightsProc';
	UserRightsProc.static.title = 'User Rights';
	UserRightsProc.static.actions = [
		{
			action: 'save',
			label: 'Proceed',
			flags: [ 'primary', 'progressive' ]
		},
		{
			label: 'Cancel',
			flags: 'safe'
		}
	];

	UserRightsProc.prototype.initialize = function() {
		UserRightsProc.super.prototype.initialize.apply(this, arguments);

		this.content = new OO.ui.FieldsetLayout( {
			label: 'User rights options'	
		} );
		this.content.addItems([
			new OO.ui.FieldLayout(
				userBox,
				{ label: 'Username:', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				wikiSelect,
				{ label: 'Wiki:', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				sysopcb,
				{ label: 'Administrator', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				cratcb,
				{ label: 'Bureaucrat', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				oscb,
				{ label: 'Oversight', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				cucb,
				{ label: 'CheckUser', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				stewardcb,
				{ label: 'Steward', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				expiryBox,
				{ label: 'Expiry:', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				summaryInput,
				{ label: 'Reason:', align: 'inline' }
			),
			new OO.ui.FieldLayout(
				buttonSelection
			)
		]);
		this.content.$element.css( 'padding', '1em' );

		this.$body.append( this.content.$element );
	};

	UserRightsProc.prototype.getActionProcess = function( action ) {
		var dialog = this;
		if( action ) {
			return new OO.ui.Process( function() {
				if( action == 'save' ) {
					modifyWrapper();
				}

				dialog.close({
					action: action
				});
			});
		}
		return UserRightsProc.super.prototype.getActionProcess.call( this, action );
	};

	UserRightsProc.prototype.getBodyHeight = function() {
		return this.content.$element.outerHeight( true );
	};

	var windowManager = new OO.ui.WindowManager();
	var userRightsProc = new UserRightsProc( {
		size: 'medium'
	} );
	windowManager.addWindows( [ userRightsProc ] );

	$( link ).click( function() {
		windowManager.openWindow( userRightsProc );
	} );

	function modifyWrapper() {
		/* TODO: Remove the need for this wrapper */
		var wikis = wikiSelect.getValue();
		for( var wiki of wikis ) {
			modify( wiki );
		}
	}

	function modify( wiki ) {
		var adding = addButton.isSelected();
		var username = userBox.value;
		var sysop = sysopcb.isSelected();
		var crat = cratcb.isSelected();
		var os = oscb.isSelected();
		var cu = cucb.isSelected();
		var steward = stewardcb.isSelected();
		var expiry = expiryBox.value;
		var summary = summaryInput.value;

		if( wiki.lastIndexOf('wiki') !== wiki.length - 4 ) {
			wiki += 'wiki';
		}

		var selectedGroups = '';
		if( sysop ) {
			selectedGroups += 'sysop';
		}
		if( crat ) {
			selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'bureaucrat';
		}
		if( os ) {
			selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'suppress';
		}
		if( cu ) {
			selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'checkuser';
		}
		if( steward ) {
			selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'steward';
		}

		if( wiki !== 'metawiki' ) {
			username += '@' + wiki;
		}

		api.postWithToken( 'userrights', {
			format: 'json',
			action: 'userrights',
			user: username,
			add: adding ? selectedGroups : '',
			expiry: expiry,
			remove: adding ? '' : selectedGroups,
			reason: summary
		} ).done( function() {
			alert((adding ? 'Added ' : 'Removed ') + selectedGroups.replace(/\|/g, ', ') + (adding ? ' to ' : ' from ') + username);
		} ).fail( function() {
			alert('Could not modify groups of ' + username);
		} );
	}

	$( '#bodyContent' ).append( windowManager.$element );
} );
}
//</nowiki>