当前位置:网站首页>Management system permission design
Management system permission design
2022-06-24 17:05:00 【Angry Birds】
1. Role control design
Method 1 Component level permission control :
// directives.js
import store from '@/store'
export default {
'hasRole': {
inserted: function (el, binding, vnode) {
let roles = store.state.userInfo.roles || ['user']
if(!roles.includes(binding.value)) {
el.parentNode.removeChild(el)
}
}
}
}
// xxx.vue
<div v-hasRole="'admin'">
...
</div>Method 2 Use Vue Router Of addRoutes Method to dynamically add user's permission routing :
// vuex
import vuex from './vuex';
let dynamicRouter = [{
path: '',
name: '',
component: xxx,
meta: {
// Dynamically add to the route through the background addRoutes
types: ['get', 'add', 'edit']
}
}]
state: {
isAddRoutes: false,
// The original data returned by the back end is saved to by default localStorage, Each initialization takes it out
roleRouterRules: JSON.parse(localStorage.getItem('roleRouterRules')),
},
// Public variables => Derive private variables
getters: {
isAddRoutes: state => state.isAddRoutes,
// according to roleRouterRules Generate dynamic routing configuration data for the current role (addRoutes Method can be used directly )
roleRouter: state => {
if (state.roleRouterRules) {
return dynamicRouter.filter(
router => state.roleRouterRules.indexOf(router.name) >= 0,
);
} else return null;
},
},
// Private method ( Sync ) => Changing static variables
mutations: {
set_isAddRoutes: (state, data) => (state.isAddRoutes = data), // payload: true/false
set_roleRouterRules: (state, data) => (state.roleRouterRules = data), // payload: true/false
},
// Public methods ( Asynchronous )=> Call private method
actions: {
set_isAddRoutes({ commit }, data) {
commit('set_isAddRoutes', data);
},
set_roleRouterRules({ commit }, data) {
// Save to vuex
commit('set_roleRouterRules', data);
// Save to localStorage, When the user forces the browser to refresh, we use this data to initialize state.roleRouterRules
localStorage.setItem('roleRouterRules', JSON.stringify(data));
},
}
// routes.js
import vuex from './vuex'
router.beforeEach((to, from, next) => {
let path = to.redirectedFrom || to.path
// White list release
if (whiteList.indexOf(path) >= 0) return next()
// The blacklist
if (!vuex.getters.roleRouter) return next({ path: '/login' })
if (!vuex.getters.isAddRoutes) {
router.addRoutes(vuex.getters.roleRouter)
vuex.dispatch('set_isAddRoutes', true)
next(path);
} else {
next();
}
});
// xxx.vue
import { mapActions } from "vuex";
export default {
methods: {
...mapActions([
"set_roleRouterRules"
]),
login(roleRouterRules) {
// Login successful ,vuex Storage role routing
this.set_roleRouterRules(roleRouterRules);
// Jump to dynamic registration B
this.$router.replace({ path: "/b" });
}
}
};Method 3 Add permission control to the route :
// directives.js
export default {
'hasPermission': {
inserted: function (el, binding, vnode) {
let types = vnode.context.$route.meta.types
let values = binding.value
let flag = true
for(let v of values) {
if(!types.inclues(v)) {
flag = false
}
}
if(!flag) {
el.parentNode.removeChild(el)
}
}
}
}
// vuex
import vuex from './vuex';
let routes = [{
path: '',
name: '',
component: xxx,
meta: {
// Dynamically add to the route through the background addRoutes
types: ['get', 'add', 'edit']
}
}]
// xxx.vue
<span v-hasPermission="['get', 'delete']">
...
</span>2. Menu permission design
- Database design
边栏推荐
- Page scrolling effect library, a little skinny
- A tutorial on how the zblog system obtains user related information based on user ID
- What is cloud development? Why cloud development? Talk about our story
- Edit distance (linear dp+ violence matching)
- 重新定义存储架构,华为用了不止5颗“芯”
- How important is it to document the project? I was chosen by the top 100 up leaders and stood up again
- Can yangjianyun's new media operation in 2021 bear all the expectations of the enterprise's private domain traffic demand?
- The TKE cluster node reports an error when executing kubectl
- 实现TypeScript运行时类型检查
- Markdown syntax -- Formula
猜你喜欢

A survey of training on graphs: taxonomy, methods, and Applications

A survey on model compression for natural language processing (NLP model compression overview)
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

MySQL learning -- table structure of SQL test questions

A survey on dynamic neural networks for natural language processing, University of California

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
随机推荐
How does the easynvr/easygbs live video platform use Wireshark to capture and analyze data locally?
API documents are simple and beautiful. It only needs three steps to open
Nonholonomic constrained robot
Future banks need to think about today's structure with tomorrow's thinking
Implement typescript runtime type checking
Coding enhances security vulnerability scanning capability and helps the team "move left safely"
Serial of H3CNE experiment column - spanning tree STP configuration experiment
About with admin option and with grant option
proxy pattern
How to save data to the greatest extent after deleting LV by misoperation under AIX?
Jmeter+grafana+influxdb build a visual performance test monitoring platform
Construction scheme of campus network clock system (standardized examination room)
Ramda's little-known side
Recent progress of ffmpeg go
Release! Tencent IOA and Tencent sky screen were selected into the first batch of certified products of domestic digital trusted services
Cloud native monitoring via blackbox_ Exporter monitoring website
test
How do HPE servers make RAID5 arrays? Teach you step by step today!
Building a cross public chain platform to solve DAPP development problems
IBM: supporting AI and enterprise digital reshaping in the cloud era with modern architecture