From c61874771a3e4cfc7f2732b622877343ad874ac7 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Wed, 21 Sep 2022 14:21:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=BA=E5=91=98=E4=BF=A1=E6=81=AF=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/alarm/data.js | 18 +++++-- api/app/lib/controllers/organization/index.js | 6 +-- api/app/lib/controllers/project/bind.js | 2 +- api/app/lib/middlewares/authenticator.js | 4 +- api/app/lib/utils/dataRange.js | 43 +++++++++++++++ api/app/lib/utils/oauth2.js | 54 ------------------- 6 files changed, 64 insertions(+), 63 deletions(-) create mode 100644 api/app/lib/utils/dataRange.js delete mode 100644 api/app/lib/utils/oauth2.js diff --git a/api/app/lib/controllers/alarm/data.js b/api/app/lib/controllers/alarm/data.js index e2e672f..e763322 100644 --- a/api/app/lib/controllers/alarm/data.js +++ b/api/app/lib/controllers/alarm/data.js @@ -2,12 +2,24 @@ async function list (ctx) { try { - const models = ctx.fs.dc.models; + const { models } = ctx.fs.dc; 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(` - 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.body = [] diff --git a/api/app/lib/controllers/organization/index.js b/api/app/lib/controllers/organization/index.js index 28f68f2..2fcee4e 100644 --- a/api/app/lib/controllers/organization/index.js +++ b/api/app/lib/controllers/organization/index.js @@ -218,13 +218,13 @@ async function user (ctx) { for (let u of userRes.rows.concat(adminRes)) { const corUsers = userPepRes.filter(up => up.id == u.pepUserId) - u.dataValues.name = corUsers[0].name - u.dataValues.departments = corUsers.map(cu => { + u.dataValues.name = corUsers.length ? corUsers[0].name : '' + u.dataValues.departments = corUsers.length ? corUsers.map(cu => { return { name: cu.depName, id: cu.depId } - }) + }) : [] } ctx.status = 200 diff --git a/api/app/lib/controllers/project/bind.js b/api/app/lib/controllers/project/bind.js index 69c954e..4d87cdd 100644 --- a/api/app/lib/controllers/project/bind.js +++ b/api/app/lib/controllers/project/bind.js @@ -166,7 +166,7 @@ async function del (ctx) { ctx.status = 204; } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined diff --git a/api/app/lib/middlewares/authenticator.js b/api/app/lib/middlewares/authenticator.js index fcac4c1..7d17ddb 100644 --- a/api/app/lib/middlewares/authenticator.js +++ b/api/app/lib/middlewares/authenticator.js @@ -80,13 +80,13 @@ let authorizeToken = async function (ctx, token) { where: { pepUserId: userInfo.id } - }) + }) || {} rslt = { 'authorized': userInfo.authorized, 'resources': (userInfo || {}).resources || [], }; 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.pepUserInfo = userInfo; ctx.fs.api.token = token; diff --git a/api/app/lib/utils/dataRange.js b/api/app/lib/utils/dataRange.js new file mode 100644 index 0000000..70431a3 --- /dev/null +++ b/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 + } +} \ No newline at end of file diff --git a/api/app/lib/utils/oauth2.js b/api/app/lib/utils/oauth2.js deleted file mode 100644 index 88d6608..0000000 --- a/api/app/lib/utils/oauth2.js +++ /dev/null @@ -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 - } -} \ No newline at end of file