User:Joepayne/stripRights.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
Joepayne (talk | contribs)
m Joepayne moved page User:Joepayne/stripAdmin.js to User:Joepayne/stripRights.js without leaving a redirect: Script relocate
Joepayne (talk | contribs)
No edit summary
Line 124: Line 124:
summary: 'Notification',
summary: 'Notification',
nocreate: 1,
nocreate: 1,
appendtext: 'Your rights have been revoked for inactivity.',
appendtext: 'Your rights have been revoked for inactivity --[[User:Joepayne|Joepayne]] ([[User talk:Joepayne|talk]]) 02:58, 17 February 2018 (UTC)',
token: mw.user.tokens.get( 'csrfToken' )
token: mw.user.tokens.get( 'csrfToken' )
}
}
Line 145: Line 145:
summary: 'Notification',
summary: 'Notification',
createonly: 1,
createonly: 1,
text: 'Your rights have been revoked for inactivity.',
text: 'Your rights have been revoked for inactivity --[[User:Joepayne|Joepayne]] ([[User talk:Joepayne|talk]]) 02:58, 17 February 2018 (UTC)',
token: mw.user.tokens.get( 'csrfToken' )
token: mw.user.tokens.get( 'csrfToken' )
}
}

Revision as of 02:58, 17 February 2018

//Script to strip rights from a user on one click
//To do: add toggles for different skins
if( mw.config.get("wgRelevantUserName") ) {
	var username = mw.config.get("wgRelevantUserName");
	$.getJSON(
	//Get user's group membership
    mw.util.wikiScript('api'),
    {
        format: 'json',
        action: 'query',
        list: 'users',
        usprop: 'groups',
        ususers: username
    }
	).done( function ( data ) {
    try {
    	if(data.query.users[0].groups.indexOf(!'sysop') >= 0) {/* Do nothing */}
    	else {
			var link = mw.util.addPortletLink(
	    		'sidebar',
	    		'#',
	    		'Strip rights',
		    	'pt-striprightslink',
			 	'Remove standard rights from this user'
			);
		
			$(link).click(function() {
				checkAndPromote(username);
			});
    	}
    }
    catch ( e ) {
        console.log( "Content request error: " + e.message );
        console.log( "Content request response: " + JSON.stringify( data ) );
    }
	} ).fail( function () {
    	console.log( "While getting the userlist, there was an AJAX error." );
	} );
}

function checkAndPromote(username) {
	$.getJSON(
	//Get user's group membership again
    mw.util.wikiScript('api'),
    {
        format: 'json',
        action: 'query',
        list: 'users',
        usprop: 'groups',
        ususers: username
    }
	).done( function ( data ) {
    try {
    	if(data.query.users[0].groups.indexOf(!'sysop') >= 0) {/* Do nothing */}
    	else {
    		check = confirm("Do you want to remove " + username + "'s rights on this wiki?");
			if (check) {
				stripRights(username);
				alertUser(username);
				alert(username + " has had all rights removed");
			}
    	}
    }
    catch ( e ) {
        console.log( "Content request error: " + e.message );
        console.log( "Content request response: " + JSON.stringify( data ) );
    }
	} ).fail( function () {
    	console.log( "While getting the userlist, there was an AJAX error." );
	} );
}

function stripRights(username) {
$.getJSON(
	//Get userrights token
    mw.util.wikiScript('api'),
    {
        format: 'json',
        action: 'query',
        meta: 'tokens',
        type: 'userrights'
    }
).done( function ( data ) {
    try {
    	var rightsToken = data.query.tokens.userrightstoken;
    	//Strip rights
    	$.ajax( {
	        url: mw.util.wikiScript( 'api' ),
    	    type: 'POST',
        	dataType: 'json',
        	data: {
	            format: 'json',
	            action: 'userrights',
	            user: username,
	            remove: 'sysop|bureaucrat',
	            reason: 'Procedural removal as per [[Test Wiki:Inactivity Policy]]',
	            token: rightsToken,
	        }
	    } ).done(console.log( "Removed rights from: " + username )
	    ).fail( function ( e, data ){
	    	console.log( e.message );
	    	console.log( JSON.stringify( data ) );
	    });
    }
    catch ( e ) {
        console.log( "Content request error: " + e.message );
        console.log( "Content request response: " + JSON.stringify( data ) );
    }
} ).fail( function () {
    console.log( "While getting the userlist, there was an AJAX error." );
} );
}

function alertUser(username) {
	//If page already exists
	$.ajax( {
	    url: mw.util.wikiScript( 'api' ),
    	type: 'POST',
        dataType: 'json',
        data: {
	        format: 'json',
	        action: 'edit',
            title: 'User talk:' + username,
            summary: 'Notification',
            nocreate: 1,
            appendtext: 'Your rights have been revoked for inactivity --[[User:Joepayne|Joepayne]] ([[User talk:Joepayne|talk]]) 02:58, 17 February 2018 (UTC)',
            token: mw.user.tokens.get( 'csrfToken' )
        }
    } ).done( function (data) {
    	//console.log(data);
    }).fail( function ( e, data ){
    	console.log( e.message );
    	console.log( JSON.stringify( data ) );
    });
    
    //If pages does not exist
    $.ajax( {
	    url: mw.util.wikiScript( 'api' ),
    	type: 'POST',
        dataType: 'json',
        data: {
	        format: 'json',
	        action: 'edit',
            title: 'User talk:' + username,
            summary: 'Notification',
            createonly: 1,
            text: 'Your rights have been revoked for inactivity --[[User:Joepayne|Joepayne]] ([[User talk:Joepayne|talk]]) 02:58, 17 February 2018 (UTC)',
            token: mw.user.tokens.get( 'csrfToken' )
        }
    } ).done( function (data) {
    	//console.log(data);
    }).fail( function ( e, data ){
    	console.log( e.message );
    	console.log( JSON.stringify( data ) );
    });
}