0
这是一个nodejs设置。我们的目标是防止执行代码的某些部分,如果用户不是在管理员组:从外部文件调用执行停止代码
这是file1.js:
export function adminAuthRequired(req, res) {
if (req.session.user_group !== "admin") {
const message = "You are not authorized to access this content."
boundaries.redirectToPage(res, message, "redirect-default-page.ejs")
}
}
这是file2.js调用该函数from file1.js:
public destroy(slug) {
utils.adminAuthRequired(this.req, this.res) // Here we include file1 function
// Code needs to stop execution here if user is not admin
// Query db for user details
db.deleteSingleRecord(this.res, "links", `WHERE link_slug = ${slug}`, callback)
// ... rest of code....
}
是否有无论如何在file2的adminAuthRequired调用后停止代码执行?在file1中处理执行停止?
我可以简单地在file2中从文件1返回的bool中执行if语句,但是我想知道是否有方法将它全部嵌入到file1中?
你可以创建一个file2.js函数,就是所谓的“executeAsAdmin”,并通过功能作为在file1.js定义,但执行的参数在file2.js中,并且只有当用户是管理员时。 – Glubus