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

100% Statements 80/80
100% Branches 12/12
100% Functions 8/8
100% Lines 80/80

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 81129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 32x 32x 32x 32x 129x 129x 129x 129x 129x 129x 129x 129x 11x 129x 129x 129x 129x 129x 129x 129x 129x 16x 129x 129x 129x 129x 129x 129x 129x 129x 3x 129x 129x 129x 129x 129x 129x 129x 129x 10x 10x 129x 129x 129x 129x 129x 129x 129x 129x 23x 1x 23x 23x 129x 129x  
/*!
 * WikiLambda Vue editor: Pinia store for frontend user rights and privileges
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
module.exports = {
	state: {
		/**
		 * Array of user rights or null if not initialized
		 */
		userRights: null
	},
 
	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 ) {
			const findUserHasRight = ( right ) => state.userRights !== null ?
				state.userRights.includes( right ) :
				undefined;
			return findUserHasRight;
		},
 
		/**
		 * Returns whether the user is logged in
		 *
		 * @return {boolean}
		 */
		isUserLoggedIn: function () {
			return !!mw.config.values.wgUserName;
		},
 
		/**
		 * Returns whether the user can execute functions
		 *
		 * @return {boolean}
		 */
		userCanRunFunction: function () {
			return this.userHasRight( 'wikilambda-execute' );
		},
 
		/**
		 * Returns whether the user can execute unsaved code
		 *
		 * @return {boolean}
		 */
		userCanRunUnsavedCode: function () {
			return this.userHasRight( 'wikilambda-execute-unsaved-code' );
		},
 
		/**
		 * Returns whether the user can edit type
		 *
		 * @return {boolean}
		 */
		userCanEditTypes: function () {
			return this.userHasRight( 'wikilambda-edit-type' );
		}
	},
 
	actions: {
		/**
		 * Fetch the user rights from mw.user.getRights and
		 * store them in the state
		 */
		fetchUserRights: function () {
			mw.user.getRights().then( ( rights ) => {
				this.userRights = rights;
			} );
		}
	}
};