Skip to content

Commit

Permalink
feat: 新增部署访问策略接口, 修改 http 测试用例;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 28, 2021
1 parent 29c89c3 commit fbcdb90
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 215 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"rest-client.environmentVariables": {
"$shared": {},
"test": {
"base_url": "http://127.0.0.1:8080"
"base_url": "http://127.0.0.1:9000",
"sys_admin": "laf-sys",
"sys_password": "laf-sys"
}
}
}
4 changes: 2 additions & 2 deletions packages/devops-server/http/admin.http
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "less",
"password": "less123"
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}

### 管理员信息
Expand Down
4 changes: 2 additions & 2 deletions packages/devops-server/http/dbm.http
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "less",
"password": "less123"
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}

### 获取集合列表
Expand Down
20 changes: 20 additions & 0 deletions packages/devops-server/http/deploy.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

@token={{login.response.body.$.data.access_token}}

### 管理员登陆
# @name login

POST {{base_url}}/admin/login HTTP/1.1
Content-Type: application/json

{
"username": "{{sys_admin}}",
"password": "{{sys_password}}"
}


### 部署访问策略

POST {{base_url}}/deploy/policy
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {{token}}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export async function handleAdminLogin(req: Request, res: Response) {
})
}

const ret = await db.collection('admins')
const ret = await db.collection('__admins')
.withOne({
query: db
.collection('password')
.collection('__password')
.where({ password: hashPassword(password), type: 'login' }),
localField: '_id',
foreignField: 'uid'
Expand Down Expand Up @@ -78,7 +78,7 @@ export async function handleAdminInfo(req: Request, res: Response) {
}

//
const ret = await db.collection('admins')
const ret = await db.collection('__admins')
.where({ _id: uid })
.get()

Expand Down Expand Up @@ -124,7 +124,7 @@ export async function handleAdminAdd(req: Request, res: Response) {
}

// 验证用户是否已存在
const { total } = await db.collection('admins').where({ username }).count()
const { total } = await db.collection('__admins').where({ username }).count()
if (total > 0) {
return res.send({
code: 1,
Expand All @@ -133,7 +133,7 @@ export async function handleAdminAdd(req: Request, res: Response) {
}

// 验证 roles 是否合法
const { total: valid_count } = await db.collection('roles')
const { total: valid_count } = await db.collection('__roles')
.where({
name: db.command.in(roles)
}).count()
Expand All @@ -146,7 +146,7 @@ export async function handleAdminAdd(req: Request, res: Response) {
}

// add admin
const r = await db.collection('admins')
const r = await db.collection('__admins')
.add({
username,
name: name ?? null,
Expand All @@ -157,7 +157,7 @@ export async function handleAdminAdd(req: Request, res: Response) {
})

// add admin password
await db.collection('password')
await db.collection('__password')
.add({
uid: r.id,
password: hashPassword(password),
Expand Down Expand Up @@ -199,7 +199,7 @@ export async function handleAdminEdit(req: Request, res: Response) {
}

// 验证 uid 是否合法
const { data: admins } = await db.collection('admins').where({ _id: uid }).get()
const { data: admins } = await db.collection('__admins').where({ _id: uid }).get()
if (!admins || !admins.length) {
return res.send({
code: 1,
Expand All @@ -208,7 +208,7 @@ export async function handleAdminEdit(req: Request, res: Response) {
}

// 验证 roles 是否合法
const { total: valid_count } = await db.collection('roles')
const { total: valid_count } = await db.collection('__roles')
.where({
name: db.command.in(roles)
}).count()
Expand All @@ -222,7 +222,7 @@ export async function handleAdminEdit(req: Request, res: Response) {

// update password
if (password) {
await db.collection('password')
await db.collection('__password')
.where({ uid: uid })
.update({
password: hashPassword(password),
Expand All @@ -239,7 +239,7 @@ export async function handleAdminEdit(req: Request, res: Response) {

// username
if (username && username != old.username) {
const { total } = await db.collection('admins').where({ username }).count()
const { total } = await db.collection('__admins').where({ username }).count()
if (total) {
return res.send({
code: 1,
Expand All @@ -264,7 +264,7 @@ export async function handleAdminEdit(req: Request, res: Response) {
data['roles'] = roles
}

const r = await db.collection('admins')
const r = await db.collection('__admins')
.where({ _id: uid })
.update(data)

Expand Down
2 changes: 1 addition & 1 deletion packages/devops-server/src/router/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router } from 'express'

import { handleAdminAdd, handleAdminEdit, handleAdminInfo, handleAdminLogin } from './admin'
import { handleAdminAdd, handleAdminEdit, handleAdminInfo, handleAdminLogin } from './handlers'

export const AdminRouter = Router()

Expand Down
36 changes: 36 additions & 0 deletions packages/devops-server/src/router/deploy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as express from 'express'
import { checkPermission } from '../../api/permission'
import { deployAccessPolicy } from '../../api/rules'
import { Globals } from '../../lib/globals'

const logger = Globals.logger
export const DeployRouter = express.Router()


/**
*
*/
DeployRouter.post('/policy', async (req, res) => {
const requestId = req['requestId']
logger.info(requestId, `post /deploy/policy`)

// 权限验证
const code = await checkPermission(req['auth']?.uid, 'deploy.policy')
if (code) {
return res.status(code).send()
}

try {
const r = await deployAccessPolicy()

return res.send({
code: 0,
data: r
})
} catch (error) {
return res.send({
code: 1,
error: error
})
}
})
74 changes: 0 additions & 74 deletions packages/devops-server/src/router/entry/admin.ts

This file was deleted.

42 changes: 0 additions & 42 deletions packages/devops-server/src/router/entry/dbm.ts

This file was deleted.

Loading

0 comments on commit fbcdb90

Please sign in to comment.