2016-09-26 2 views
0

ここにnodejsの設定があります。目標は、ユーザーが管理グループ内に存在しない場合、コード実行の一部を防ぐためです:外部ファイルからのコード実行停止

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です:

これはfile1.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.... 
} 

fileAでadminAuthRequiredを呼び出した後にコードの実行を停止する方法はありますか?実行停止がfile1で処理されていますか?

ファイル1から返されたboolのfile2のif文を実行するだけで済みますが、file1にすべてを埋め込む方法があるかどうかを知りたいですか?

+0

file2.jsで、ユーザーが管理者である場合に限ります。 – Glubus

答えて

0

コールバックアプローチを使用できます。

例:その後、

export function adminAuthRequired(req, res, cb) { 
    if (req.session.user_group !== "admin") { 
     const message = "You are not authorized to access this content." 
     boundaries.redirectToPage(res, message, "redirect-default-page.ejs") 
    } else cb(); 
} 

と呼び出すように - >

public destroy(slug) { 
    utils.adminAuthRequired(this.req, this.res, function() { 
     db.deleteSingleRecord(this.res, "links", `WHERE link_slug = ${slug}`, callback); 
     // ... rest of code.... 
    }); 
} 
あなたが「executeAsAdmin」と呼ばれていますfile2.jsで関数を作成し、file1.jsに定義されていますが、実行される関数を引数として渡すことができ
関連する問題