Authored by weiqingting

权限验证

... ... @@ -10,6 +10,7 @@ var options=require('../staticConfig.js').staticDir;
var author=require("../stub/service/auth");
/**
* 不需要权限校验配置
*/
... ... @@ -32,24 +33,6 @@ if(env!=='development') {
}
//模块环境适配
// var common = require('../util/common');
// var path = require('path');
// var url = require('url');
// var $routers = { GET: {}, POST: {}, ALL: [] };
// common.readDir(path.join(__dirname, '../interface'), function (module) {
// Array.prototype.push.apply($routers.ALL, module.res);
// });
// //刷选get/post请求
// for (var i in $routers.ALL) {
// var router = $routers.ALL[i];
// var methord = router.method.toUpperCase();
// var url = router.route;
// $routers[methord || "GET"][url] = router;
// }
/**
* 认证中间件
* @param {Object} req 请求对象
... ... @@ -62,6 +45,32 @@ module.exports = function(req, res, next) {
//方法名称
var method = req.method;
function nextRedirect(method,path){
//进行白名单验证和session验证
if(guestAccessList[method+":"+path]||req.session.user) {
//判断是否已存在appendData
if(!res.appendData) {
res.appendData = _.cloneDeep(req.session.user);
} else {
res.appendData = _.merge({},res.appendData,req.session.user);
}
//添加当前菜单激活
addActiveMenu(req,res);
//添加管理员信息到http头
if(req.session.user) {
appendAdminInfo(req,req.session.user.auth);
}
next();
} else {
req.app.logger.log('info',"CurentView:Login");
//如果是登陆界面不加载布局
res.render('pages/login',{layout:false,cssfile:CSS_FILE});
}
}
function filterOriginalUrl(path){
if(!/\.\w+/.test(path)){
return path.replace(/\?.+/,'');
... ... @@ -71,40 +80,27 @@ module.exports = function(req, res, next) {
//访问路由路径
var path = req.route?req.route.path:filterOriginalUrl(req.originalUrl);
if(req.session.user) {
if(path&&!req.xhr){
if(guestAccessList[method+":"+path]){
nextRedirect(method,path);
return;
}
/*判断权限*/
if(path&&req.session.user.allRight[path]){
author.validateAuthor(req.session.user.auth.pid,req.session.user.auth.role_id,path,function(data){
console.log(data);
if(data.code!=200){
res.status(403);
res.render('error/error_nolayout',{message:NO_AUTH,layout:false,cssfile:CSS_FILE});
return;
}
nextRedirect(method,path);
});
}else{
nextRedirect(method,path);
}
}else{
nextRedirect(method,path);
}
//进行白名单验证和session验证
if(guestAccessList[method+":"+path]||req.session.user) {
//判断是否已存在appendData
if(!res.appendData) {
res.appendData = _.cloneDeep(req.session.user);
} else {
res.appendData = _.merge({},res.appendData,req.session.user);
}
//添加当前菜单激活
addActiveMenu(req,res);
//添加管理员信息到http头
if(req.session.user) {
appendAdminInfo(req,req.session.user.auth);
}
next();
} else {
req.app.logger.log('info',"CurentView:Login");
//如果是登陆界面不加载布局
res.render('pages/login',{layout:false,cssfile:CSS_FILE});
}
}
/**
... ...
... ... @@ -44,10 +44,11 @@ var errorMessage = {
var userInfo = {
auth:userData,
menu:data.menu,
noRight:data.noRight
allRight:data.allRight
}
userInfo.auth.uid = userData.pid;
userInfo.auth.name = userData.truename;
res.json({
code:20003,
data:userInfo
... ... @@ -77,13 +78,11 @@ console.log(options);
if(ret.data) {
menuData = _makeMenu(ret.data);
}
callback(null,menuData);
// _getAllMenu(function(ret){
// menuData.noRight = filterRight(ret,menuData);
// delete menuData.right;
// callback(null,menuData);
// });
_getAllRight(function(ret){
menuData.allRight = ret;
callback(null,menuData);
});
}
});
... ... @@ -199,7 +198,6 @@ exports.validateAuthor=function(pid,roleid,path,callback){
url:oldService.isUsedMenuAuth,
form:'['+pid+','+roleid+', "'+path+'", "", "", '+WEBSITE+']'
}
console.log(options);
util.httpCall(options,function(err,ret){
if(err) {
callback({});
... ... @@ -208,3 +206,26 @@ exports.validateAuthor=function(pid,roleid,path,callback){
}
});
}
function _getAllRight(callback){
var options = {
url:oldService.allRight,
form:'[false]'
}
console.log(options);
util.httpCall(options,function(err,ret){
if(err) {
callback({});
} else {
var map={};
ret.data.forEach(function(data){
console.log(data);
if(data.platform_id==WEBSITE){
map[data.path]=true;
}
});
callback(map);
}
});
}
... ...
... ... @@ -10,6 +10,7 @@ module.exports = {
login: oldSerDomain + '/service/account/v1/Profile/login',
getResourceByPid: oldSerDomain + '/service/account/v1/Profile/getMenuByPid',
getAllResByWebsite: oldSerDomain + '/service/setting/v1/menu/getAllMenu',
isUsedMenuAuth:oldSerDomain + '/service/account/v1/profile/authPath'
isUsedMenuAuth:oldSerDomain + '/service/account/v1/profile/authPath',
allRight:oldSerDomain+'/service/setting/v1/action/getAllAction'
};
... ...