Skip to content

Commit

Permalink
feat(replicate): Impl put replicas
Browse files Browse the repository at this point in the history
  • Loading branch information
laf-yun committed Apr 30, 2022
1 parent 1d67b05 commit 01dca0b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
17 changes: 9 additions & 8 deletions packages/system-server/src/handler/replicate/delete.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { DatabaseAgent } from '../../db'
import { Request, Response } from "express"
import { CN_APPLICATIONS, CONST_DICTS } from "../../constants"
import { CN_REPLICATE_AUTH, CONST_DICTS } from "../../constants"
import { IApplicationData } from "../../support/application"
import { checkPermission } from "../../support/permission"
import { ObjectId } from 'mongodb'

/**
* handle delete replicate auth
*/
export async function handleDeleteReplicateAuth(req: Request, res: Response) {
const db = DatabaseAgent.db
const uid = res["auth"]?.uid
const app: IApplicationData = req["parsed-app"]
const uid = req["auth"]?.uid

// check login
if (!uid) {
res.status(401).send()
}

const app: IApplicationData = req["parsed-app"]
// check permission
const { REPLICATE_AUTH_REMOVE } = CONST_DICTS.permissions
const code = await checkPermission(uid, REPLICATE_AUTH_REMOVE.name, app)
Expand All @@ -27,13 +28,13 @@ export async function handleDeleteReplicateAuth(req: Request, res: Response) {
// check id
const id = req.params.id
const existed = await db
.collection<IApplicationData>(CN_APPLICATIONS)
.countDocuments({ _id: id })
.collection<IApplicationData>(CN_REPLICATE_AUTH)
.countDocuments({ _id: new ObjectId(id) })
if (!existed) {
return res.status(422).send("invalid id")
}

//remove
const r = db.collection(CN_APPLICATIONS).deleteOne({ _id: id })
return res.send({ data: (await r).deletedCount })
// remove
const r = await db.collection(CN_REPLICATE_AUTH).deleteOne({ _id: new ObjectId(id) })
return res.send({ data: r.deletedCount })
}
6 changes: 6 additions & 0 deletions packages/system-server/src/handler/replicate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Router } from "express"
import { handleCreateReplicateAuth } from "./create"
import { handleDeleteReplicateAuth } from "./delete"
import { handleGetReplicateAuth } from "./get"
import { handleUpdateReplicas } from "./replicas"
import { handleUpdateReplicateAuth } from "./update"

export const ReplicateRouter = Router()
Expand All @@ -28,3 +29,8 @@ ReplicateRouter.post("/replicate_auth/:id", handleUpdateReplicateAuth)
* replicate auth delete
*/
ReplicateRouter.delete("/replicate_auth/:id", handleDeleteReplicateAuth)

/**
* put replicas
*/
ReplicateRouter.put("/replicas", handleUpdateReplicas)
79 changes: 79 additions & 0 deletions packages/system-server/src/handler/replicate/replicas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { DatabaseAgent } from '../../db'
import { Request, Response } from "express"
import { CONST_DICTS } from "../../constants"
import { IApplicationData, getApplicationByAppid } from "../../support/application"
import { checkPermission } from "../../support/permission"
import { publishFunctions, publishOneFunction } from '../../support/function'
import { publishAccessPolicies } from '../../support/policy'

/**
* handle put replicates
*/
export async function handleUpdateReplicas(req: Request, res: Response) {
const app: IApplicationData = req["parsed-app"]
const uid = req["auth"]?.uid
const body = req.body

// check login
if (!uid) {
res.status(401).send()
}

// check permission
const { REPLICATES_PUT } = CONST_DICTS.permissions
const code = await checkPermission(uid, REPLICATES_PUT.name, app)
if (code) {
return res.status(code).send()
}

// check params
const target_appid = body?.target_appid
if (!target_appid) {
res.status(422).send("invalid target_appid")
}
const types = ["all", "type"]
const functions = body?.functions
const fun_type = body?.functions?.type
if (functions && !types.includes(fun_type)) {
res.status(422).send(" invalid functions of type")
}
const policies = body?.policies
const pol_type = body?.policies?.type
if (policies && !types.includes(pol_type)) {
res.status(422).send(" invalid policies of type")
}

// check target app
const target_app = await getApplicationByAppid(target_appid)
if (!target_app) {
return res.status(422).send("invalid target_appid")
}

// push replicates
const accessor = DatabaseAgent.sys_accessor
const session = accessor.conn.startSession()
try {
await session.withTransaction(async () => {
if (functions && 'all' === fun_type) {
await publishFunctions(app)
}
if (functions && 'all' == pol_type) {
await publishAccessPolicies(app)
}
if (functions && 'part' === fun_type) {
for (const fuc of functions.items) {
await publishOneFunction(app, fuc.id)
}
}
if (policies && 'part' === pol_type) {
for (const pol of policies.items) {
await publishOneFunction(app, pol.id)
}
}
})
} finally {
await session.endSession()
}
return res.send("pushed")
}

5 changes: 4 additions & 1 deletion packages/system-server/src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export const permissions = {
REPLICATE_AUTH_READ: { name: 'replicate_auth.read', label: '授权资源-读取授权应用' },
REPLICATE_AUTH_ADD: { name: 'replicate_auth.add', label: '授权资源-创建授权应用' },
REPLICATE_AUTH_REMOVE: { name: 'replicate_auth.remove', label: '授权资源-删除授权应用' },
REPLICATE_AUTH_UPDATE: { name: 'replicate_auth.update', label: '授权资源-更新授权应用' }
REPLICATE_AUTH_UPDATE: { name: 'replicate_auth.update', label: '授权资源-更新授权应用' },
REPLICATES_PUT: { name: 'replicates.update', label: '远程推送' },
REPLICATES_POST: { name: 'replicate.add', label: '应用部署' }

}


Expand Down

0 comments on commit 01dca0b

Please sign in to comment.