fixed post request issue and admin verification issue

This commit is contained in:
strawmanbobi
2016-12-09 21:21:47 +08:00
parent 712e1c24e9
commit 2090caab15
7 changed files with 103 additions and 93 deletions

View File

@@ -61,7 +61,17 @@ function tokenValidation (req, res, next) {
if (req.url.indexOf("/irext/int/list_remote_indexes") != -1) { if (req.url.indexOf("/irext/int/list_remote_indexes") != -1) {
// override for get method // override for get method
adminID = req.query.id; adminID = req.query.admin_id;
token = req.query.token;
}
if (req.url.indexOf("/irext/int/search_remote_indexes") != -1) {
// override for get method
adminID = req.query.admin_id;
token = req.query.token;
}
if (req.url.indexOf("/irext/int/download_remote_index") != -1) {
// override for get method
adminID = req.query.admin_id;
token = req.query.token; token = req.query.token;
} }
if (req.url.indexOf("/irext/int") != -1) { if (req.url.indexOf("/irext/int") != -1) {

View File

@@ -12,8 +12,6 @@ app.post('/irext/int/list_operators', intService.listOperators);
app.post('/irext/int/list_categories', intService.listCategories); app.post('/irext/int/list_categories', intService.listCategories);
app.post('/irext/int/list_brands', intService.listBrands); app.post('/irext/int/list_brands', intService.listBrands);
app.post('/irext/int/list_ir_protocols', intService.listIRProtocols); app.post('/irext/int/list_ir_protocols', intService.listIRProtocols);
app.post('/irext/int/search_remote_indexes', intService.searchRemoteIndexes);
app.post('/irext/int/download_remote_index', intService.downloadRemoteIndex);
app.post('/irext/int/list_unpublished_brands', intService.listUnpublishedBrands); app.post('/irext/int/list_unpublished_brands', intService.listUnpublishedBrands);
app.post('/irext/int/list_unpublished_remote_indexes', intService.listUnpublishedRemoteIndexes); app.post('/irext/int/list_unpublished_remote_indexes', intService.listUnpublishedRemoteIndexes);
@@ -30,3 +28,5 @@ app.post('/irext/int/publish_brands', intService.publishBrands);
app.post('/irext/int/create_protocol', intService.createProtocol); app.post('/irext/int/create_protocol', intService.createProtocol);
app.get('/irext/int/list_remote_indexes', intService.listRemoteIndexes); app.get('/irext/int/list_remote_indexes', intService.listRemoteIndexes);
app.get('/irext/int/search_remote_indexes', intService.searchRemoteIndexes);
app.get('/irext/int/download_remote_index', intService.downloadRemoteIndex);

View File

@@ -202,9 +202,9 @@ exports.listRemoteIndexes = function (req, res) {
* return : Remote Index List * return : Remote Index List
*/ */
exports.searchRemoteIndexes = function (req, res) { exports.searchRemoteIndexes = function (req, res) {
var remoteMap = req.body.remote_map; var remoteMap = req.query.remote_map;
var from = req.body.from; var from = req.query.from;
var count = req.body.count; var count = req.query.count;
internalLogic.searchRemoteIndexesWorkUnit(remoteMap, from, count, internalLogic.searchRemoteIndexesWorkUnit(remoteMap, from, count,
function (listRemoteIndexesErr, remoteIndexes) { function (listRemoteIndexesErr, remoteIndexes) {
@@ -221,7 +221,7 @@ exports.searchRemoteIndexes = function (req, res) {
* return : Redirect to binary download * return : Redirect to binary download
*/ */
exports.downloadRemoteIndex = function (req, res) { exports.downloadRemoteIndex = function (req, res) {
var remoteIndexID = req.body.remote_index_id; var remoteIndexID = req.query.remote_index_id;
internalLogic.downloadRemoteBinCachedWorkUnit(remoteIndexID, function (serveBinErr, filePath) { internalLogic.downloadRemoteBinCachedWorkUnit(remoteIndexID, function (serveBinErr, filePath) {
if (errorCode.SUCCESS.code == serveBinErr.code) { if (errorCode.SUCCESS.code == serveBinErr.code) {
@@ -265,8 +265,7 @@ exports.createRemoteIndex = function (req, res) {
var remoteIndex; var remoteIndex;
var filePath; var filePath;
var contentType; var contentType;
var adminID;
var adminID = req.body.admin_id;
form.on('file', function(field, file) { form.on('file', function(field, file) {
// rename the incoming file to the file's name // rename the incoming file to the file's name
@@ -289,6 +288,7 @@ exports.createRemoteIndex = function (req, res) {
} else { } else {
logger.info("remote index form submitted successfully"); logger.info("remote index form submitted successfully");
remoteIndex = fields; remoteIndex = fields;
adminID = remoteIndex.admin_id;
filePath = files.remote_file.path; filePath = files.remote_file.path;
// set MIME to octet-stream as there might not be any contentType passed from the front-end form // set MIME to octet-stream as there might not be any contentType passed from the front-end form
contentType = files.type || "application/octet-stream"; contentType = files.type || "application/octet-stream";
@@ -430,7 +430,7 @@ exports.createProtocol = function (req, res) {
var protocol; var protocol;
var filePath; var filePath;
var contentType; var contentType;
var adminID = req.body.admin_id; var adminID;
form.on('file', function(field, file) { form.on('file', function(field, file) {
fs.rename(file.path, form.uploadDir + "/" + file.name); fs.rename(file.path, form.uploadDir + "/" + file.name);
@@ -450,6 +450,7 @@ exports.createProtocol = function (req, res) {
} else { } else {
logger.info("protocol form submitted successfully"); logger.info("protocol form submitted successfully");
protocol = fields; protocol = fields;
adminID = protocol.admin_id;
filePath = files.protocol_file.path; filePath = files.protocol_file.path;
// set MIME to octet-stream as there might not be any contentType passed from the front-end form // set MIME to octet-stream as there might not be any contentType passed from the front-end form
contentType = files.type || "application/octet-stream"; contentType = files.type || "application/octet-stream";

View File

@@ -214,6 +214,7 @@
<input type="file" class="form-control" name="remote_file" placeholder="Remote File" id="remote_file"> <input type="file" class="form-control" name="remote_file" placeholder="Remote File" id="remote_file">
</div> </div>
</div> </div>
<input type="hidden" name="admin_id" id="admin_id">
</form> </form>
</div> </div>
</div> </div>
@@ -454,6 +455,7 @@
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="createProtocol();">确定添加</button> <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="createProtocol();">确定添加</button>
</div> </div>
<input type="hidden" id="protocol_admin_id" name="admin_id">
</form> </form>
</div> </div>
</div> </div>

View File

@@ -122,14 +122,14 @@ function loadRemoteList(isSearch, remoteMap) {
var url; var url;
if (isSearch && remoteMap) { if (isSearch && remoteMap) {
url = '/irext/int/search_remote_indexes?remote_map='+remoteMap+'&from=0&count=2000&id='+id+'&token='+token; url = '/irext/int/search_remote_indexes?remote_map='+remoteMap+'&from=0&count=2000&admin_id='+id+'&token='+token;
} else { } else {
if(currentFilterCategory.id == 3) { if(currentFilterCategory.id == 3) {
url = '/irext/int/list_remote_indexes?category_id='+currentFilterCategory.id+'&city_code='+currentFilterCity.code+ url = '/irext/int/list_remote_indexes?category_id='+currentFilterCategory.id+'&city_code='+currentFilterCity.code+
'&from=0&count=100&id='+id+'&token='+token; '&from=0&count=100&admin_id='+id+'&token='+token;
} else { } else {
url = '/irext/int/list_remote_indexes?category_id='+currentFilterCategory.id+'&brand_id='+currentFilterBrand.id+ url = '/irext/int/list_remote_indexes?category_id='+currentFilterCategory.id+'&brand_id='+currentFilterBrand.id+
'&from=0&count=100&id='+id+'&token='+token; '&from=0&count=100&admin_id='+id+'&token='+token;
} }
} }
@@ -307,7 +307,7 @@ function createRemote() {
', remoteName = ' +remoteName + ', remoteFile = ' + remoteFile + ', remoteNumber = ' + remoteNumber); ', remoteName = ' +remoteName + ', remoteFile = ' + remoteFile + ', remoteNumber = ' + remoteNumber);
var form = $('#remote_upload_form'); var form = $('#remote_upload_form');
form.attr('action', '/irext/int/create_remote_index?id='+id+'&token='+token); form.attr('action', '/irext/int/create_remote_index');
//form.attr('method', 'post'); //form.attr('method', 'post');
//form.attr('encoding', 'multipart/form-data'); //form.attr('encoding', 'multipart/form-data');
//form.attr('enctype', 'multipart/form-data'); //form.attr('enctype', 'multipart/form-data');
@@ -323,6 +323,7 @@ function createRemote() {
$('#brand_name_tw').val(currentBrand.name_tw); $('#brand_name_tw').val(currentBrand.name_tw);
$('#city_name_tw').val(currentCity.name_tw); $('#city_name_tw').val(currentCity.name_tw);
$('#operator_name_tw').val(currentOperator.name_tw); $('#operator_name_tw').val(currentOperator.name_tw);
$('#admin_id').val(id);
form.submit(); form.submit();
$('#create_remote_dialog').modal('hide'); $('#create_remote_dialog').modal('hide');
@@ -641,6 +642,7 @@ function createProtocol() {
//form.attr('method', 'post'); //form.attr('method', 'post');
//form.attr('encoding', 'multipart/form-data'); //form.attr('encoding', 'multipart/form-data');
//form.attr('enctype', 'multipart/form-data'); //form.attr('enctype', 'multipart/form-data');
$('#protocol_admin_id').val(id);
form.submit(); form.submit();
$('#create_protocol_dialog').modal('hide'); $('#create_protocol_dialog').modal('hide');
@@ -1453,7 +1455,7 @@ function downloadBin() {
popUpHintDialog('请先选中一个索引'); popUpHintDialog('请先选中一个索引');
return; return;
} }
downloadURL = '/irext/int/download_remote_index?remote_index_id='+selectedRemote.id+'&id='+id+'&token='+token; downloadURL = '/irext/int/download_remote_index?remote_index_id='+selectedRemote.id+'&admin_id='+id+'&token='+token;
if (null != client && client == 'console') { if (null != client && client == 'console') {
// directly download binary to remote via serial port // directly download binary to remote via serial port
@@ -1761,5 +1763,5 @@ function translateToTC(textID, targetTextID) {
} }
function gotoIndex() { function gotoIndex() {
window.location = '../index.html?id='+id+'&token='+token; window.location = '../index.html?admin_id='+id+'&token='+token;
} }

View File

@@ -40,23 +40,30 @@ exports.adminLoginWorkUnit = function (userName, password, callback) {
var resp = JSON.parse(signInResponse); var resp = JSON.parse(signInResponse);
if (undefined != resp.entity) { if (undefined != resp.entity) {
var admin = resp.entity; var admin = resp.entity;
var userID, var token,
token,
key, key,
ttl = 24 * 60 * 60 * 14, ttl = 24 * 60 * 60 * 14,
timeStamp; timeStamp,
name;
timeStamp = new Date().getTime(); timeStamp = new Date().getTime();
token = MD5.MD5(password + timeStamp); token = MD5.MD5(password + timeStamp);
token += "," + admin.permissions; token += "," + admin.permissions;
key = "admin_" + admin.id; key = "admin_" + admin.id;
adminAuth.setAuthInfo(key, token, ttl, function(setAdminAuthErr) { adminAuth.setAuthInfo(key, token, ttl, function(setAdminAuthErr) {
if (errorCode.SUCCESS.code == setAdminAuthErr.code) {
key = "admin_name_" + admin.id;
name = admin.user_name;
adminAuth.setAuthInfo(key, name, ttl, function(setAdminNameErr) {
if (errorCode.SUCCESS.code == setAdminNameErr.code) {
admin.token = token; admin.token = token;
callback(setAdminAuthErr, admin); }
callback(setAdminNameErr, admin);
});
}
}); });
} else { } else {
callback(errorCode.FAILED, null); callback(errorCode.FAILED, null);
} }
} else { } else {
logger.error("admin sign in failed"); logger.error("admin sign in failed");
callback(errorCode.FAILED, null); callback(errorCode.FAILED, null);

View File

@@ -10,6 +10,7 @@ var crypto = require('crypto');
// global inclusion // global inclusion
require('../mini_poem/configuration/constants'); require('../mini_poem/configuration/constants');
var orm = require('orm'); var orm = require('orm');
var AdminAuth = require('../authority/admin_auth.js');
var PythonCaller = require('../mini_poem/external/python_caller'); var PythonCaller = require('../mini_poem/external/python_caller');
var Category = require('../model/category_dao.js'); var Category = require('../model/category_dao.js');
@@ -31,6 +32,8 @@ var errorCode = new ErrorCode();
var async = require('async'); var async = require('async');
var adminAuth = new AdminAuth(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, null);
// relative XML file path // relative XML file path
var PROTOCOL_PATH = "protocol"; var PROTOCOL_PATH = "protocol";
@@ -213,9 +216,10 @@ exports.createRemoteIndexWorkUnit = function(remoteIndex, filePath, contentType,
userArgs = []; userArgs = [];
// verify admin // verify admin
Admin.getAdminByID(adminID, function(getAdminErr, admin) { var key = "admin_name_" + adminID;
if (errorCode.SUCCESS.code == getAdminErr.code && null != admin) { adminAuth.getAuthInfo(key, function(getAdminAuthErr, result) {
contributor = admin.user_name; if (errorCode.SUCCESS.code == getAdminAuthErr.code && null != result) {
contributor = result;
// begin creating remote index // begin creating remote index
switch(parseInt(categoryID)) { switch(parseInt(categoryID)) {
@@ -536,15 +540,14 @@ exports.deleteRemoteIndexWorkUnit = function (remoteIndex, adminID, callback) {
queryParams.put("app_key", REQUEST_APP_KEY); queryParams.put("app_key", REQUEST_APP_KEY);
queryParams.put("app_token", REQUEST_APP_TOKEN); queryParams.put("app_token", REQUEST_APP_TOKEN);
Admin.getAdminByID(adminID, function(getAdminErr, admin) { var key = "admin_name_" + adminID;
if (errorCode.SUCCESS.code == getAdminErr.code && null != admin) { adminAuth.getAuthInfo(key, function(getAdminAuthErr, result) {
if (admin.admin_type == enums.ADMIN_TYPE_EXTERNAL) { if (errorCode.SUCCESS.code == getAdminAuthErr.code && null != result) {
if(remoteIndex.contributor.indexOf(admin.user_name) == -1) { if(remoteIndex.contributor.indexOf(result) == -1) {
logger.info("this admin " + admin.user_name + " could not change this remote index"); logger.info("the admin " + result + " could not change this remote index");
callback(errorCode.FAILED); callback(errorCode.FAILED);
return; return;
} }
}
var requestSender = var requestSender =
new RequestSender(PRIMARY_SERVER_ADDRESS, new RequestSender(PRIMARY_SERVER_ADDRESS,
PRIMARY_SERVER_PORT, PRIMARY_SERVER_PORT,
@@ -576,15 +579,14 @@ exports.deleteRemoteIndexWorkUnit = function (remoteIndex, adminID, callback) {
}; };
exports.verifyRemoteIndexWorkUnit = function (remoteIndex, pass, adminID, callback) { exports.verifyRemoteIndexWorkUnit = function (remoteIndex, pass, adminID, callback) {
Admin.getAdminByID(adminID, function(getAdminErr, admin) { var key = "admin_name_" + adminID;
if (errorCode.SUCCESS.code == getAdminErr.code && null != admin) { adminAuth.getAuthInfo(key, function(getAdminAuthErr, result) {
if (admin.admin_type == enums.ADMIN_TYPE_EXTERNAL) { if (errorCode.SUCCESS.code == getAdminAuthErr.code && null != result) {
if (remoteIndex.contributor.indexOf(admin.user_name) == -1) { if(remoteIndex.contributor.indexOf(result) == -1) {
logger.info("this admin " + admin.user_name + " could not change this remote index"); logger.info("the admin " + result + " could not change this remote index");
callback(errorCode.FAILED); callback(errorCode.FAILED);
return; return;
} }
}
var status = 0 == pass ? enums.ITEM_PASS : enums.ITEM_FAILED; var status = 0 == pass ? enums.ITEM_PASS : enums.ITEM_FAILED;
RemoteIndex.verifyRemoteIndex(remoteIndex.id, status, function(updateRemoteIndexErr) { RemoteIndex.verifyRemoteIndex(remoteIndex.id, status, function(updateRemoteIndexErr) {
@@ -597,15 +599,14 @@ exports.verifyRemoteIndexWorkUnit = function (remoteIndex, pass, adminID, callba
}; };
exports.fallbackRemoteIndexWorkUnit = function (remoteIndex, adminID, callback) { exports.fallbackRemoteIndexWorkUnit = function (remoteIndex, adminID, callback) {
Admin.getAdminByID(adminID, function(getAdminErr, admin) { var key = "admin_name_" + adminID;
if (errorCode.SUCCESS.code == getAdminErr.code && null != admin) { adminAuth.getAuthInfo(key, function(getAdminAuthErr, result) {
if (admin.admin_type == enums.ADMIN_TYPE_EXTERNAL) { if (errorCode.SUCCESS.code == getAdminAuthErr.code && null != result) {
if (remoteIndex.contributor.indexOf(admin.user_name) == -1) { if (remoteIndex.contributor.indexOf(result) == -1) {
logger.info("this admin " + admin.user_name + " could not change this remote index"); logger.info("the admin " + result + " could not change this remote index");
callback(errorCode.FAILED); callback(errorCode.FAILED);
return; return;
} }
}
var status = enums.ITEM_VERIFY; var status = enums.ITEM_VERIFY;
RemoteIndex.fallbackRemoteIndex(remoteIndex.id, status, function (updateRemoteIndexErr) { RemoteIndex.fallbackRemoteIndex(remoteIndex.id, status, function (updateRemoteIndexErr) {
@@ -751,15 +752,11 @@ exports.createBrandWorkUnit = function (brand, adminID, callback) {
status: enums.ITEM_VERIFY status: enums.ITEM_VERIFY
}; };
Admin.getAdminByID(adminID, function(getAdminErr, admin) { var key = "admin_name_" + adminID;
if (errorCode.SUCCESS.code == getAdminErr.code && null != admin) { adminAuth.getAuthInfo(key, function(getAdminAuthErr, result) {
if (admin.admin_type == enums.ADMIN_TYPE_EXTERNAL) { if (getAdminAuthErr.code == errorCode.SUCCESS.code &&
logger.info("this admin " + admin.user_name + " could not change this remote index"); null != result) {
callback(errorCode.FAILED); brand.contributor = result;
return;
}
brand.contributor = admin.user_name;
Brand.findBrandByConditions(conditions, function(findBrandErr, brands) { Brand.findBrandByConditions(conditions, function(findBrandErr, brands) {
if(errorCode.SUCCESS.code == findBrandErr.code && null != brands && brands.length > 0) { if(errorCode.SUCCESS.code == findBrandErr.code && null != brands && brands.length > 0) {
logger.info("brand already exists"); logger.info("brand already exists");
@@ -771,7 +768,6 @@ exports.createBrandWorkUnit = function (brand, adminID, callback) {
} }
}); });
} else { } else {
logger.info("invalid admin ID, return directly");
callback(errorCode.FAILED, null); callback(errorCode.FAILED, null);
} }
}); });
@@ -838,25 +834,18 @@ exports.createProtocolWorkUnit = function(protocol, filePath, contentType, admin
pythonFile = "irda_tv_protocol.py", pythonFile = "irda_tv_protocol.py",
userArgs = []; userArgs = [];
/////////////////////////////////////
// step 2, get admin name as contributor
var key = "admin_name_" + adminID;
adminAuth.getAuthInfo(key, function(getAdminAuthErr, result) {
if (errorCode.SUCCESS.code == getAdminAuthErr.code && null != result) {
contributor = result;
if (enums.PROTOCOL_TYPE_G2_QUATERNARY == protocolType) { if (enums.PROTOCOL_TYPE_G2_QUATERNARY == protocolType) {
pythonFile = "irda_tv_protocol.py"; pythonFile = "irda_tv_protocol.py";
} else if (enums.PROTOCOL_TYPE_G2_HEXDECIMAL == protocolType) { } else if (enums.PROTOCOL_TYPE_G2_HEXDECIMAL == protocolType) {
pythonFile = "irda_tv_protocol_hex.py"; pythonFile = "irda_tv_protocol_hex.py";
} }
Admin.getAdminByID(adminID, function(getAdminErr, admin) {
if (errorCode.SUCCESS.code == getAdminErr.code && null != admin) {
contributor = admin.user_name;
logger.info("get admin error code = " + JSON.stringify(getAdminErr) + ", admin = " + JSON.stringify(admin));
if (admin.admin_type == enums.ADMIN_TYPE_EXTERNAL) {
logger.info("this admin " + admin.user_name + " could not create protocol");
callback(errorCode.FAILED);
return;
}
//////////////////////////////////////
// step 2, parse python run-time path, python file name and user arguments
logger.info("prepare to parse protocol"); logger.info("prepare to parse protocol");
userArgs.length = 0; userArgs.length = 0;
// python s_$category.py [remote_xml_file_abs_file] [remote_xml_file_name] [remote_xml_dir_abs_path] // python s_$category.py [remote_xml_file_abs_file] [remote_xml_file_name] [remote_xml_dir_abs_path]
@@ -920,7 +909,6 @@ exports.createProtocolWorkUnit = function(protocol, filePath, contentType, admin
callback(errorCode.FAILED); callback(errorCode.FAILED);
} }
} else { } else {
logger.error('failed to check admin type');
callback(errorCode.FAILED); callback(errorCode.FAILED);
} }
}); });