javascript - 无法为访问函数创建 JavaScript 算法
问题描述
我的应用程序中有一些选项卡,如报告、任务等......用户也有不同的权限,如报告.添加、任务.删除。我需要创建函数来检查允许用户做什么。
// for example array with all current user permissions
// this permissions mean user is allowed to do everything with tasks
// add and edit reports, but not allowed to to delete it
const permissions = ['reports.add', 'reports.edit', 'tasks'];
const isAllowed = (condition) => {
return permissions.some((permission) => {
// here is problem, I can't create algorithm
});
};
// When user clicks delete report button
// I expect to use this function like this
if (isAllowed('reports.delete')) {
deleteReport()
}
解决方案
如果以. permissions
_condition
permission
const
permissions = ['reports.add', 'reports.edit', 'tasks'],
isAllowed = condition => permissions.some(permission => condition.startsWith(permission));
console.log(isAllowed('reports.add')); // true
console.log(isAllowed('tasks.edit')); // true
console.log(isAllowed('tasks')); // true
console.log(isAllowed('task')); // false
推荐阅读
- tarantool - 如何授予用户在 Tarantool 中仅执行特定功能
- python - 在检测到的轮廓opencv-python中查找颜色
- c# - C# windows服务启动前复制文件
- assembly - 将数字与 x86_64 程序集中的方法相关联的最佳方法是什么
- javascript - discord bot 下一行带空格
- c# - 是否可以在中间件 Asp .Net Core 中调用 WebApi
- python - Pandas - 在分组后绘制图表的问题
- excel - 最大如果在 Power 查询中有多个条件
- html - 如何使flexbox中的第一个按钮与第二个按钮大小相同
- java - 我正在尝试在 Netbeans 12 中制作一个摆动 GUI,但是当我尝试运行它时出现了一个巨大的异常