Skip to content

Instantly share code, notes, and snippets.

@AfterThreeYears
Created September 8, 2019 15:47
Show Gist options
  • Save AfterThreeYears/817b208772c1a3dda6c0e4828f1b0ebc to your computer and use it in GitHub Desktop.
Save AfterThreeYears/817b208772c1a3dda6c0e4828f1b0ebc to your computer and use it in GitHub Desktop.
权限系统设计
const NO = 0b00000; // 初始没权限
const SYS_SETTING = 0b00010; // 系统重要设置权限
const USER_MANG = 0b00100; // 用户管理权限
const DATA_ADMIN = 0b01000; // 数据库管理权限
const POST_EDIT = 0b00001; // 文章编辑操作权限
const POST_VIEW = 0b10000; // 文章查看权
const user = {
name: "老王",
authority: NO,
};
function setAuthority(user, authoritys) {
authoritys.forEach((authority) => {
user.authority |= authority;
});
}
function checkAuthority(user, authority) {
return user.authority & authority;
}
setAuthority(user, [SYS_SETTING, USER_MANG, DATA_ADMIN]);
if (checkAuthority(user, SYS_SETTING)) {
console.log(`${user.name}有系统重要设置权限`);
}
if (checkAuthority(user, USER_MANG)) {
console.log(`${user.name}有用户管理权限`);
}
if (checkAuthority(user, DATA_ADMIN)) {
console.log(`${user.name}有数据库管理权限`);
}
if (checkAuthority(user, POST_EDIT)) {
console.log(`${user.name}有文章编辑操作权限`);
}
if (checkAuthority(user, POST_VIEW)) {
console.log(`${user.name}有文章查看权`);
}
/**
* 结果
* 老王有系统重要设置权限
老王有用户管理权限
老王有数据库管理权限
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment