All files / ext.wikilambda.edit/store/modules user.js

100% Statements 9/9
100% Branches 2/2
100% Functions 8/8
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85              16x                             17x 1x                       1x                         14x       14x               16x                   8x                   3x        
/*!
 * WikiLambda Vue editor: Store module for frontend user rights and privileges
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
module.exports = exports = {
	state: {
		/**
		 * Array of user rights or null if not initialized
		 */
		userRights: null
	},
	actions: {
		/**
		 * Fetch the user rights from mw.user.getRights and
		 * store them in the state
		 *
		 * @param {Object} context
		 */
		fetchUserRights: function ( context ) {
			mw.user.getRights().then( ( rights ) => {
				context.commit( 'setUserRights', rights );
			} );
		}
	},
	mutations: {
		/**
		 * Store the collection of user rights in the state
		 *
		 * @param {Object} state
		 * @param {Array} rights
		 */
		setUserRights: function ( state, rights ) {
			state.userRights = rights;
		}
	},
	getters: {
		/**
		 * Returns whether the current user has the given
		 * right, or undefined if state is not yet initialized.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		userHasRight: function ( state ) {
			function hasRight( right ) {
				return ( state.userRights !== null ) ?
					state.userRights.includes( right ) :
					undefined;
			}
			return hasRight;
		},
		/**
		 * Returns whether the user is logged in
		 *
		 * @return {boolean}
		 */
		isUserLoggedIn: function () {
			return !!mw.config.values.wgUserName;
		},
		/**
		 * Returns whether the user can execute functions
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {boolean}
		 */
		userCanRunFunction: function ( _state, getters ) {
			return getters.userHasRight( 'wikilambda-execute' );
		},
		/**
		 * Returns whether the user can execute unsaved code
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {boolean}
		 */
		userCanRunUnsavedCode: function ( _state, getters ) {
			return getters.userHasRight( 'wikilambda-execute-unsaved-code' );
		}
	}
};