Skip to content

Commit

Permalink
fix(system-server): add appid to applications;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 2, 2021
1 parent 4103188 commit f1bc2e2
Show file tree
Hide file tree
Showing 32 changed files with 341 additions and 89 deletions.
22 changes: 19 additions & 3 deletions packages/system-server/src/api/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-28 22:00:45
* @LastEditTime: 2021-08-31 15:47:43
* @LastEditTime: 2021-09-02 16:12:47
* @Description: Application APIs
*/

Expand All @@ -18,6 +18,7 @@ export interface ApplicationStruct {
_id?: string
name: string
created_by: string
appid: string
app_secret: string
status: 'created' | 'starting' | 'running' | 'stopped'
config: {
Expand All @@ -43,12 +44,16 @@ export interface ApplicationStruct {
/**
* Get an application created by account_id
*/
export async function getApplicationById(appid: string) {
export async function getApplicationByAppid(appid: string) {
if (!appid) return null

const db = DatabaseAgent.sys_db
const ret = await db.collection(Constants.cn.applications)
.where({ _id: appid })
.where({ appid: appid })
.field({
app_secret: false,
config: false
})
.getOne<ApplicationStruct>()

assert.ok(ret.ok, `getMyApplicationById() got error: ${appid}`)
Expand All @@ -66,6 +71,9 @@ export async function getMyApplications(account_id: string) {
const db = DatabaseAgent.sys_db
const ret = await db.collection(Constants.cn.applications)
.where({ created_by: account_id })
.field({
config: false
})
.get<ApplicationStruct>()

assert.ok(ret.ok, `getMyApplications() got error: ${account_id}`)
Expand Down Expand Up @@ -116,4 +124,12 @@ export async function getApplicationDbAccessor(app: ApplicationStruct) {
export async function generateApplicationSecret() {
const buf = crypto.randomBytes(64)
return buf.toString('base64')
}

/**
* Generate application id
* @returns
*/
export function generateApplicationId() {
return crypto.randomUUID()
}
4 changes: 2 additions & 2 deletions packages/system-server/src/api/function.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-08-29 18:23:38
* @LastEditTime: 2021-09-02 16:34:14
* @Description:
*/

Expand Down Expand Up @@ -56,7 +56,7 @@ export async function publishFunctions(app: ApplicationStruct) {
const ret = await DatabaseAgent.sys_accessor.db
.collection(Constants.cn.functions)
.find({
appid: app._id
appid: app.appid
})
.toArray()

Expand Down
2 changes: 1 addition & 1 deletion packages/system-server/src/api/permission.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-08-30 15:48:51
* @LastEditTime: 2021-09-01 18:24:01
* @Description:
*/

Expand Down
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-08-29 18:24:59
* @LastEditTime: 2021-09-02 16:34:22
* @Description:
*/

Expand All @@ -21,7 +21,7 @@ export async function publishAccessPolicy(app: ApplicationStruct) {
const ret = await DatabaseAgent.sys_accessor.db
.collection(Constants.cn.policies)
.find({
appid: app._id
appid: app.appid
})
.toArray()

Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/api/trigger.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-08-29 18:25:36
* @LastEditTime: 2021-09-02 16:34:27
* @Description:
*/

Expand Down Expand Up @@ -50,7 +50,7 @@ export async function publishTriggers(app: ApplicationStruct) {
const ret = await DatabaseAgent.sys_accessor.db
.collection(Constants.cn.triggers)
.find({
appid: app._id
appid: app.appid
})
.toArray()

Expand Down
4 changes: 4 additions & 0 deletions packages/system-server/src/constants/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ const owner = [

export const roles = {
developer: {
name: 'developer',
label: 'Application Developer',
permissions: developer
},
dba: {
name: 'dba',
label: 'Application Database Administrator',
permissions: dba
},
operator: {
name: 'operator',
label: 'Application Operator',
permissions: operator
},
owner: {
name: 'owner',
label: 'Application Owner',
permissions: owner
}
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/index.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-08-18 13:25:09
* @LastEditTime: 2021-09-02 16:36:36
* @Description:
*/

Expand All @@ -13,7 +13,7 @@ import { router } from './router/index'
import { logger } from './lib/logger'

const server = express()
server.use(express.json())
server.use(express.json() as any)

/**
* Allow CORS by default
Expand Down
7 changes: 4 additions & 3 deletions packages/system-server/src/router/application/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-31 15:00:04
* @LastEditTime: 2021-08-31 15:46:15
* @LastEditTime: 2021-09-02 16:10:58
* @Description:
*/

import { Request, Response } from 'express'
import { ApplicationStruct, generateApplicationSecret } from '../../api/application'
import { ApplicationStruct, generateApplicationId, generateApplicationSecret } from '../../api/application'
import { Constants } from '../../constants'
import { DatabaseAgent } from '../../lib/db-agent'

Expand All @@ -19,14 +19,15 @@ export async function handleCreateApplication(req: Request, res: Response) {
return res.status(401).send()

const app_name = req.body?.name ?? 'default'

const db = DatabaseAgent.sys_db

const appid = generateApplicationId()
const app_secret = await generateApplicationSecret()
const now = Date.now()
const data: ApplicationStruct = {
name: app_name,
created_by: uid,
appid: appid,
app_secret: app_secret,
status: 'created',
collaborators: [],
Expand Down
77 changes: 77 additions & 0 deletions packages/system-server/src/router/application/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 15:22:34
* @LastEditTime: 2021-09-02 16:13:09
* @Description:
*/

import { Request, Response } from 'express'
import { getApplicationByAppid, getMyApplications, getMyJoinedApplications } from '../../api/application'
import { getPermissionsOfRoles } from '../../api/permission'
import { Constants } from '../../constants'

/**
* The handler of getting my applications(created & joined)
*/
export async function handleMyApplications(req: Request, res: Response) {
const uid = req['auth']?.uid
if (!uid)
return res.status(401).send()

const created = await getMyApplications(uid)
const joined = await getMyJoinedApplications(uid)

return res.send({
data: {
created,
joined
}
})
}

/**
* The handler of getting application by id
* @param req
* @param res
* @returns
*/
export async function handleGetApplicationByAppid(req: Request, res: Response) {
const uid = req['auth']?.uid
if (!uid)
return res.status(401).send()

const appid = req.params.appid
const app = await getApplicationByAppid(appid)
if (!app)
return res.status(422).send('invalid appid')

// return directly if the author here
if (app.created_by === uid) {
const roles = [Constants.roles.owner.name]
const permissions = getPermissionsOfRoles(roles)
return res.send({
data: {
application: app,
permissions,
roles
}
})
}

// reject if not the collaborator
const [found] = app.collaborators.filter(co => co.uid === uid)
if (!found) {
return res.status(403).send()
}

const roles = found.roles
const permissions = getPermissionsOfRoles(roles)

return res.send({
data: {
application: app,
permissions,
roles
}
})
}
9 changes: 7 additions & 2 deletions packages/system-server/src/router/application/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:11
* @LastEditTime: 2021-08-31 15:56:38
* @LastEditTime: 2021-09-01 11:05:36
* @Description:
*/

import { Router } from 'express'
import { handleNotImplemented } from '../common'
import { handleCreateApplication } from './create'
import { handleMyApplications } from './my'
import { handleGetApplicationByAppid, handleMyApplications } from './get'

export const ApplicationRouter = Router()

Expand All @@ -17,6 +17,11 @@ export const ApplicationRouter = Router()
*/
ApplicationRouter.get('/my', handleMyApplications)

/**
* Get application by id
*/
ApplicationRouter.get('/:appid', handleGetApplicationByAppid)

/**
* Create an application
*/
Expand Down
28 changes: 0 additions & 28 deletions packages/system-server/src/router/application/my.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/system-server/src/router/dbm/add-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @Description:
*/

import { getApplicationById, getApplicationDbAccessor } from '../../api/application'
import { getApplicationByAppid, getApplicationDbAccessor } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { permissions } from '../../constants/permissions'
import { Request, Response } from 'express'
Expand All @@ -22,7 +22,7 @@ export async function handleCreateIndex(req: Request, res: Response) {
}

const appid = req.params.appid
const app = await getApplicationById(appid)
const app = await getApplicationByAppid(appid)
if (!app) {
return res.status(422).send('app not found')
}
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/router/dbm/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @Description:
*/

import { getApplicationById, getApplicationDbAccessor } from '../../api/application'
import { getApplicationByAppid, getApplicationDbAccessor } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { permissions } from '../../constants/permissions'
import { Request, Response } from 'express'
Expand All @@ -16,7 +16,7 @@ import { Request, Response } from 'express'
*/
export async function handleCollectionList(req: Request, res: Response) {
const appid = req.params.appid
const app = await getApplicationById(appid)
const app = await getApplicationByAppid(appid)
if (!app) {
return res.status(422).send('app not found')
}
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/router/dbm/delete-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @Description:
*/

import { getApplicationById, getApplicationDbAccessor } from '../../api/application'
import { getApplicationByAppid, getApplicationDbAccessor } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { permissions } from '../../constants/permissions'
import { Request, Response } from 'express'
Expand All @@ -21,7 +21,7 @@ export async function handleDeleteIndex(req: Request, res: Response) {
}

const appid = req.params.appid
const app = await getApplicationById(appid)
const app = await getApplicationByAppid(appid)
if (!app) {
return res.status(422).send('app not found')
}
Expand Down
Loading

0 comments on commit f1bc2e2

Please sign in to comment.