Introduction:

“In ancient times, three scholars went to take an exam together. They met a fortune-teller and asked,” How many people can we take an exam this time?” Fortune-telling gentleman stretch out a finger, pretend to be mysterious of say: the chance of heaven cannot divulge. Further questioning, no more opening. After the three scholars walked, the uncle next to sell vegetables pointed to the fortune-teller said, you cow! There are three of them. One of them won the exam. In the test of two, you say a person not, in the test of three, you say together; I didn’t get any. You said I didn’t get any

In code, this logic can be used for permission menus. It is a common scenario in development that users with different identities see different menus. I use this logic in the front end. When I talk to Java engineers, they like it.

The code:

First of all,

Assume three menus: client, administration, and database

Assume three identities: user, operator, and administrator

Without permissions, the menu looks like this:

const menuList = [

​    {

​        name:'Database',

​    },

​    {

​        name:'Client',

​    },{

​        name:'Management side',},]Copy the code

After adding permissions, the menu looks like this

const menuList = [

​    {

​        name:'Database',

​        identity:'Administrator',

​        authority:'show',

​    },

​    {

​        name:'Management side',

​        identity:'users',

​        authority:'Do not display',

​    },

​    {

​        name:'Client',

​        authority:'Full display',

​    },{

​        name:'UI little sister is beautiful',

​        authority:'No display at all',},]Copy the code

The four menus correspond to the scenes in the story, which are who wins, who misses, together and together

Of course, menus cannot be used directly in the front end, so you need to use functions to filter out menus that are not visible to the current user

function menu(menuArr, user) {  
    let menuList = JSON.stringify(menuArr)  
    menuList = JSON.parse(menuList)  / / copy
	function authority(list, user) {    
        // Not displayed by default
        list.show = false    
        switch (list.authority) {      
            case 'Full display':        
                list.show = true  
                break      
            case 'No display at all':        
                // list.show = false       
                break     
            case 'show':        
                if (list.name === user.name) {          
                    list.show = true       
                }        
                break      
            case 'Do not display':        
                if(list.name ! == user.name) { list.show =true     
                }       
                break    
        }     
        return list 
     }  
    let list = []  
    for (let i = 0; i < menuList.length; i++) {    
        let listInner = authority(menuList[i], user)    
        if (listInner.show) {      
            list.push(listInner)    
        }  
    }  
        return list
    } 
Copy the code

The main logic is like this, in order to direct use of Chinese characters, the actual development, can also use 0,1,2 state, more in line with the specification of programmers

In real development, menus might be two-level, three-level. In the database, the parentsId and ID fields are used to associate the parent with the parent

Conclusion: Tao gives birth to one, life to three, and life to all things. In actual scenarios, there may be more identity and permission states. It is recommended that a field have no more than three values. I had no complications. There is no way to summarize, if there are multiple conditions to determine permission, you can add attributes (fields), before the switch in the authority function to add a layer of judgment