当前位置:网站首页>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
边栏推荐
- Classic examples of C language 100
- Example description and case of ansible playbook automated cluster server management
- What is Ethernet
- A very good educational man and resource center planning scheme, with word file download
- Introduction to website development for zero foundation Xiaobai
- What is the difference between optical fiber jumper and copper wire
- zblog系统实现前台调用当天发布文章数量的教程
- Page scrolling effect library, a little skinny
- Coding's first closed door meeting on financial technology exchange was successfully held
- After the collective breakthrough, where is the next step of China's public cloud?
猜你喜欢

A survey on model compression for natural language processing (NLP model compression overview)

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

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

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

MySQL learning -- table structure of SQL test questions
![[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)
随机推荐
Coding's first closed door meeting on financial technology exchange was successfully held
New MySQL 8.0 feature - enhanced logical backup recovery
IBM: supporting AI and enterprise digital reshaping in the cloud era with modern architecture
Radiology: contralateral preoperative resting state MRI functional network integration is related to the surgical results of temporal lobe epilepsy
How to perform concurrent stress testing on RTSP video streams distributed by audio and video streaming servers?
实现TypeScript运行时类型检查
Zblog system realizes the tutorial of the number of articles published on the same day when the foreground calls
The mystery of redis data migration capacity
Markdown syntax -- Formula
Introduction to website development for zero foundation Xiaobai
05. Tencent cloud IOT device side learning -- mqtt protocol client implementation
The RTSP video structured intelligent analysis platform easynvr stops calling the PTZ interface through the onvif protocol to troubleshoot the pending status
GB gb28181 video cascading intelligent analysis platform easygbs broadcast video console error 401
With the solution, the nickname of the applet suddenly becomes "wechat user", and the avatar cannot be displayed?
Solution to the problem that kibana's map cannot render longitude and latitude coordinate data
Easycvr, an urban intelligent video monitoring image analysis platform, plays national standard equipment videos and captures unstable packets for troubleshooting
中金证券靠谱吗?是否合法?开股票账户安全吗?
Future banks need to think about today's structure with tomorrow's thinking
What is cloud development? Why cloud development? Talk about our story
Building a cross public chain platform to solve DAPP development problems