Skip to content

Commit

Permalink
fix(sys-server): add owner role to app; impl get tags of func api;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 9, 2021
1 parent 3ff72c5 commit 5afecf9
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/system-server/src/api/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-09-09 17:59:47
* @LastEditTime: 2021-09-10 00:42:48
* @Description:
*/

Expand Down Expand Up @@ -46,7 +46,7 @@ export async function getPolicyByName(appid: string, policy_name: string) {
* Publish access policies
* Means that copying sys db functions to app db
*/
export async function publishAccessPolicy(app: ApplicationStruct) {
export async function publishAccessPolicies(app: ApplicationStruct) {
// read policies from sys db
const ret = await DatabaseAgent.sys_accessor.db
.collection(Constants.cn.policies)
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/router/application/collaborator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-31 15:00:04
* @LastEditTime: 2021-09-08 23:05:03
* @LastEditTime: 2021-09-10 01:01:21
* @Description:
*/

Expand Down Expand Up @@ -136,7 +136,7 @@ export async function handleGetRoles(req: Request, res: Response) {

const roles = getRoles()
const rets = Object.keys(roles)
.filter(key => key !== roles.owner.name)
// .filter(key => key !== roles.owner.name)
.map(key => roles[key])

return res.send({
Expand Down
7 changes: 6 additions & 1 deletion packages/system-server/src/router/application/importer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-09 14:46:44
* @LastEditTime: 2021-09-09 17:52:13
* @LastEditTime: 2021-09-10 00:42:10
* @Description:
*/

import { Request, Response } from 'express'
import { getApplicationByAppid } from '../../api/application'
import { publishFunctions } from '../../api/function'
import { checkPermission } from '../../api/permission'
import { publishAccessPolicies } from '../../api/policy'
import { Constants } from '../../constants'
import { ApplicationImporter } from '../../lib/importer'
import { logger } from '../../lib/logger'
Expand Down Expand Up @@ -37,6 +39,9 @@ export async function handleImportApplication(req: Request, res: Response) {
try {
importer.parse()
await importer.import()

await publishFunctions(app)
await publishAccessPolicies(app)
return res.send({ data: 'ok' })
} catch (error) {
logger.error('import application got error:', error)
Expand Down
8 changes: 7 additions & 1 deletion packages/system-server/src/router/application/remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 15:22:34
* @LastEditTime: 2021-09-09 11:00:29
* @LastEditTime: 2021-09-10 01:07:11
* @Description:
*/

Expand Down Expand Up @@ -38,6 +38,12 @@ export async function handleRemoveApplication(req: Request, res: Response) {
return res.status(code).send()
}

// i know that we just checked the permission, but also limit this permission to the owner.
// just ignore the above permission checking, we will re-considered in future.
if (uid !== app.created_by) {
return res.status(403).send('only owner can remove application')
}

// save app to recycle collection
const recycle = new RecycleCollector(Constants.cn.applications)
const saved = await recycle.insert(app)
Expand Down
6 changes: 3 additions & 3 deletions packages/system-server/src/router/deploy/request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-06 15:47:57
* @LastEditTime: 2021-09-10 00:06:46
* @LastEditTime: 2021-09-10 00:42:56
* @Description:
*/

Expand All @@ -12,7 +12,7 @@ import { ApplicationStruct } from '../../api/application'
import { DatabaseAgent } from '../../lib/db-agent'
import * as assert from 'assert'
import { deployFunctions, publishFunctions } from '../../api/function'
import { deployPolicies, publishAccessPolicy } from '../../api/policy'
import { deployPolicies, publishAccessPolicies } from '../../api/policy'


const { DEPLOY_REQUEST_REMOVE, DEPLOY_REQUEST_READ, DEPLOY_REQUEST_APPLY } = Constants.permissions
Expand Down Expand Up @@ -134,7 +134,7 @@ export async function handleApplyDeployRequest(req: Request, res: Response) {
// deploy policies
if (type === 'policy') {
await deployPolicies(deploy_request.data)
await publishAccessPolicy(app)
await publishAccessPolicies(app)
}

// update deploy request status to 'deployed'
Expand Down
39 changes: 37 additions & 2 deletions packages/system-server/src/router/function/get.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-09-06 16:37:34
* @LastEditTime: 2021-09-10 01:44:34
* @Description:
*/

import { CloudFunctionStruct } from 'cloud-function-engine'
import { Request, Response } from 'express'
import { ApplicationStruct } from '../../api/application'
import { FunctionStruct } from '../../api/function'
import { checkPermission } from '../../api/permission'
import { Constants } from '../../constants'
import { permissions } from '../../constants/permissions'
Expand Down Expand Up @@ -100,4 +101,38 @@ export async function handleGetFunctionById(req: Request, res: Response) {
return res.send({
data: ret.data
})
}
}


/**
* Get all of the function tags
*/
export async function handleGetAllFunctionTags(req: Request, res: Response) {
const app: ApplicationStruct = req['parsed-app']

// check permission
const code = await checkPermission(req['auth']?.uid, FUNCTION_READ.name, app)
if (code) {
return res.status(code).send()
}

const db = DatabaseAgent.sys_accessor.db

const docs = await db.collection<FunctionStruct>(Constants.cn.functions)
.find({
tags: { $exists: true, $ne: [] }
}, {
projection: { tags: 1 }
})
.toArray()

const all_tags = []
for (const doc of docs) {
const tags = doc.tags
all_tags.push(...tags)
}
const rets = Array.from(new Set(all_tags))
return res.send({
data: rets
})
}
9 changes: 7 additions & 2 deletions packages/system-server/src/router/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:05
* @LastEditTime: 2021-09-10 00:06:13
* @LastEditTime: 2021-09-10 01:37:17
* @Description:
*/

import { Router } from "express"
import { handleCreateFunction } from "./create"
import { handleGetFunctionById, handleGetFunctions } from "./get"
import { handleGetAllFunctionTags, handleGetFunctionById, handleGetFunctions } from "./get"
import { handleGetFunctionLogs } from "./logs"
import { handlePublishFunctions } from "./publish"
import { handleRemoveFunctionById } from "./remove"
Expand All @@ -22,6 +22,11 @@ export const FunctionRouter = Router()
*/
FunctionRouter.get('/', handleGetFunctions)

/**
* Get functions of an application
*/
FunctionRouter.get('/tags/all', handleGetAllFunctionTags)

/**
* Get a function by id of an application
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/system-server/src/router/function/logs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-09-05 23:24:48
* @LastEditTime: 2021-09-10 01:14:32
* @Description:
*/

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

// build query object
const { requestId, func_id, triggerId } = req.body
const { requestId, func_id, trigger_id } = req.query
const limit = req.body?.limit ?? 10
const page = req.body?.page ?? 1

Expand All @@ -43,8 +43,8 @@ export async function handleGetFunctionLogs(req: Request, res: Response) {
query['func_id'] = func_id
}

if (triggerId) {
query['trigger_id'] = triggerId
if (trigger_id) {
query['trigger_id'] = trigger_id
}

const coll = db.collection('__function_logs')
Expand Down
6 changes: 3 additions & 3 deletions packages/system-server/src/router/policy/publish.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-09-04 00:02:20
* @LastEditTime: 2021-09-10 00:42:50
* @Description:
*/

import { Request, Response } from 'express'
import { ApplicationStruct } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { publishAccessPolicy } from '../../api/policy'
import { publishAccessPolicies } from '../../api/policy'
import Config from '../../config'
import { permissions } from '../../constants/permissions'
import { logger } from '../../lib/logger'
Expand All @@ -30,7 +30,7 @@ export async function handlePublishPolicies(req: Request, res: Response) {
}

try {
await publishAccessPolicy(app)
await publishAccessPolicies(app)

return res.send({
data: 'ok'
Expand Down

0 comments on commit 5afecf9

Please sign in to comment.