Browse Source

人员信息获取保护

dev
巴林闲侠 2 years ago
parent
commit
c61874771a
  1. 18
      api/app/lib/controllers/alarm/data.js
  2. 6
      api/app/lib/controllers/organization/index.js
  3. 2
      api/app/lib/controllers/project/bind.js
  4. 4
      api/app/lib/middlewares/authenticator.js
  5. 43
      api/app/lib/utils/dataRange.js
  6. 54
      api/app/lib/utils/oauth2.js

18
api/app/lib/controllers/alarm/data.js

@ -2,12 +2,24 @@
async function list (ctx) { async function list (ctx) {
try { try {
const models = ctx.fs.dc.models; const { models } = ctx.fs.dc;
const { clickHouse } = ctx.app.fs const { clickHouse } = ctx.app.fs
const { utils: { judgeSuper, anxinStrucRange } } = ctx.app.fs
const { database: anxinyun } = clickHouse.anxinyun.opts.config
const isSuper = judgeSuper(ctx)
let anxinStrucIds = null
if (!isSuper) {
anxinStrucIds = await anxinStrucRange(ctx)
}
const alarmRes = await clickHouse.dataAlarm.query(` const alarmRes = await clickHouse.dataAlarm.query(`
SELECT * FROM alarms SELECT
`) AlarmId, SourceName, name
FROM
alarms
LEFT JOIN ${anxinyun}.t_structure
ON ${anxinyun}.t_structure.id = alarms.StructureId
`).toPromise();
ctx.status = 200; ctx.status = 200;
ctx.body = [] ctx.body = []

6
api/app/lib/controllers/organization/index.js

@ -218,13 +218,13 @@ async function user (ctx) {
for (let u of userRes.rows.concat(adminRes)) { for (let u of userRes.rows.concat(adminRes)) {
const corUsers = userPepRes.filter(up => up.id == u.pepUserId) const corUsers = userPepRes.filter(up => up.id == u.pepUserId)
u.dataValues.name = corUsers[0].name u.dataValues.name = corUsers.length ? corUsers[0].name : ''
u.dataValues.departments = corUsers.map(cu => { u.dataValues.departments = corUsers.length ? corUsers.map(cu => {
return { return {
name: cu.depName, name: cu.depName,
id: cu.depId id: cu.depId
} }
}) }) : []
} }
ctx.status = 200 ctx.status = 200

2
api/app/lib/controllers/project/bind.js

@ -166,7 +166,7 @@ async function del (ctx) {
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
message: typeof error == 'string' ? error : undefined message: typeof error == 'string' ? error : undefined

4
api/app/lib/middlewares/authenticator.js

@ -80,13 +80,13 @@ let authorizeToken = async function (ctx, token) {
where: { where: {
pepUserId: userInfo.id pepUserId: userInfo.id
} }
}) }) || {}
rslt = { rslt = {
'authorized': userInfo.authorized, 'authorized': userInfo.authorized,
'resources': (userInfo || {}).resources || [], 'resources': (userInfo || {}).resources || [],
}; };
ctx.fs.api.userId = pomsUser.id; ctx.fs.api.userId = pomsUser.id;
ctx.fs.api.userInfo = pomsUser; ctx.fs.api.userInfo = pomsUser.dataValues;
ctx.fs.api.pepUserId = userInfo.id; ctx.fs.api.pepUserId = userInfo.id;
ctx.fs.api.pepUserInfo = userInfo; ctx.fs.api.pepUserInfo = userInfo;
ctx.fs.api.token = token; ctx.fs.api.token = token;

43
api/app/lib/utils/dataRange.js

@ -0,0 +1,43 @@
'use strict';
const fs = require('fs');
const moment = require('moment')
module.exports = function (app, opts) {
function judgeSuper (ctx) {
try {
const { userInfo = {} } = ctx.fs.api || {};
const { role = [] } = userInfo
return role.includes('SuperAdmin')
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
}
}
async function anxinStrucRange (ctx) {
try {
const { models } = ctx.fs.dc;
const { userInfo = {} } = ctx.fs.api || {};
const { correlationProject = [] } = userInfo
const bindRes = await models.ProjectCorrelation.findAll({
where: {
pepProjectId: { $in: correlationProject }
}
})
return bindRes.reduce((arr, b) => {
for (let sid of b.anxinProjectId) {
arr.add(sid);
}
return arr;
}, new Set())
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
}
}
return {
judgeSuper,
anxinStrucRange
}
}

54
api/app/lib/utils/oauth2.js

@ -1,54 +0,0 @@
const fs = require('fs');
module.exports = function (app, opts) {
async function oauthParseAuthHeader (auth) {
if (!auth) {
throw new Error('参数无效: 未包含Authorization头');
}
const authSplit = auth.split('Basic');
if (authSplit.length != 2) {
throw new Error('参数无效: Authorization头格式无效,请检查是否包含了"Basic "');
}
const authCode = authSplit[1];
const apikey = Buffer.from(authCode, 'base64').toString();
const keySplit = apikey.split(':');
if (keySplit.length != 2) {
throw new Error('参数无效:请检查Authorization头内容是否经过正确Base64编码');
}
return keySplit;
}
async function oauthParseBody (body, type) {
let checked = true, token = '';
if (type == 'apply' && body['grant_type'] != 'client_credentials') {
checked = false;
} else if (type == 'refresh') {
if (body['grant_type'] != 'refresh_token' || body['token'] == null) {
checked = false;
} else {
token = body['token'];
}
} else if (type == 'invalidate') {
if (body['token'] == null) {
checked = false;
} else {
token = body['token'];
}
}
if (!checked) {
throw new Error('参数无效:请求正文中未包含正确的信息');
}
return token;
}
return {
oauthParseAuthHeader,
oauthParseBody
}
}
Loading…
Cancel
Save