管理员可以添加角色..但是我如何允许一些死记硬背和保护路由。我为管理员创建属性 isAdmin,为其他角色创建表角色。 我如何允许角色 1 和角色 2 和角色 3 的 getAllClients 和 允许管理员和角色 4 的 C、R、U、D 客户端
用户模型是这样的:
const userSchema = new mongoose.Schema({
roleUser: {
type: roleSchema,
},
email: String,
password: String,
isAdmin: Boolean
})
userSchema.methods.generateAuthToken = function () {
const token = jwt.sign(
{ _id: this._id, isAdmin: this.isAdmin },
config.get("jwtPrivateKey")
);
return token;
};
榜样:
const roleSchema = new mongoose.Schema({roleUser: String})
路由器: 允许管理员和用户的这条路线有角色 4(员工):
router.post("/",[authorization,isAdmin,validate(validateClient)],
asyncMiddleware(async (req, res) => {
//some code ...
})
);
允许 admin 和 role1(manager),2(moderator),3(other) 使用此路由:
router.get("/getAllClients",asyncMiddleware(async (req, res) => {
//some code ..
})
);