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

100% Statements 94/94
100% Branches 13/13
100% Functions 9/9
100% Lines 94/94

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 86 87 88 89 90 91 92 93 94 957x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 8x 1x 8x 8x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 17x 17x 17x 17x 17x 17x 7x 7x 7x 7x 7x 7x 7x 10x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 7x 7x  
/*!
 * 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' );
		},
		/**
		 * Returns whether the user can edit type
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {boolean}
		 */
		userCanEditTypes: function ( _state, getters ) {
			return getters.userHasRight( 'wikilambda-edit-type' );
		}
	}
};