Skip to content

Commit

Permalink
fix(sys-service): fix func id type to ObjectId;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Dec 7, 2021
1 parent 57fa817 commit 6ac01d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
12 changes: 6 additions & 6 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-11-17 19:21:35
* @LastEditTime: 2021-12-07 09:48:05
* @Description: Application APIs
*/

Expand All @@ -10,7 +10,7 @@ import { DatabaseAgent } from "../lib/db-agent"
import * as assert from 'assert'
import { MongoAccessor } from "database-proxy"
import { generateUUID } from "../utils/rand"
import { MongoClient } from 'mongodb'
import { MongoClient, ObjectId } from 'mongodb'
import Config from "../config"
import * as mongodb_uri from 'mongodb-uri'
import { BucketMode } from "./storage"
Expand All @@ -22,7 +22,7 @@ import { logger } from "../lib/logger"
export interface ApplicationStruct {
_id?: string
name: string
created_by: string
created_by: ObjectId
appid: string
status: 'created' | 'running' | 'stopped' | 'cleared'
config: {
Expand Down Expand Up @@ -83,7 +83,7 @@ export async function getMyApplications(account_id: string) {

const db = DatabaseAgent.db
const docs = await db.collection<ApplicationStruct>(Constants.cn.applications)
.find({ created_by: account_id }, {
.find({ created_by: new ObjectId(account_id) }, {
projection: { config: false }
}).toArray()

Expand All @@ -101,7 +101,7 @@ export async function getMyJoinedApplications(account_id: string) {
const db = DatabaseAgent.db
const docs = await db.collection<ApplicationStruct>(Constants.cn.applications)
.find({
'collaborators.uid': account_id
'collaborators.uid': new ObjectId(account_id)
}).toArray()

return docs
Expand Down Expand Up @@ -134,7 +134,7 @@ export async function getApplicationDbAccessor(app: ApplicationStruct) {
* @returns
*/
export function getUserRolesOfApplication(uid: string, app: ApplicationStruct) {
if (app.created_by === uid) {
if (app.created_by.toHexString() === uid) {
return [Constants.roles.owner.name]
}

Expand Down
4 changes: 2 additions & 2 deletions 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-09-01 18:24:01
* @LastEditTime: 2021-12-07 09:45:49
* @Description:
*/

Expand All @@ -22,7 +22,7 @@ export async function checkPermission(uid: string, permission: string, app: Appl
assert.ok(app, 'empty app got')

// pass directly while the app owner here
if (uid === app.created_by) return 0
if (uid === app.created_by.toHexString()) return 0

// reject while uid is not the collaborator
const [collaborator] = app.collaborators?.filter(co => co.uid === uid) ?? []
Expand Down
6 changes: 3 additions & 3 deletions packages/system-server/src/api/policy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-10-08 00:49:03
* @LastEditTime: 2021-12-07 09:42:03
* @Description:
*/

import * as assert from 'assert'
import { Constants } from '../constants'
import { DatabaseAgent } from "../lib/db-agent"
import { ClientSession } from 'mongodb'
import { ClientSession, ObjectId } from 'mongodb'
import { ApplicationStruct, getApplicationDbAccessor } from './application'
import { logger } from '../lib/logger'

Expand All @@ -26,7 +26,7 @@ export interface PolicyStruct {
hash: string
created_at: number
updated_at: number
created_by: string
created_by: ObjectId
appid: string
}
/**
Expand Down
14 changes: 10 additions & 4 deletions 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-10-08 01:26:40
* @LastEditTime: 2021-12-07 11:01:40
* @Description:
*/

Expand All @@ -12,6 +12,7 @@ import { getApplicationByAppid } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { Constants } from '../../constants'
import { DatabaseAgent } from '../../lib/db-agent'
import { DockerContainerServiceDriver } from '../../lib/service-driver/container'

const { APPLICATION_REMOVE } = Constants.permissions

Expand All @@ -28,8 +29,8 @@ export async function handleRemoveApplication(req: Request, res: Response) {
if (!app)
return res.status(422).send('invalid appid')

if (app.status !== 'cleared' && app.status !== 'created') {
return res.status(422).send('app status must be cleared or created')
if (app.status === 'running') {
return res.status(422).send('you can not remove a running app')
}

// check permission
Expand All @@ -40,10 +41,15 @@ export async function handleRemoveApplication(req: Request, res: Response) {

// 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) {
if (uid !== app.created_by.toHexString()) {
return res.status(403).send('only owner can remove application')
}

if (app.status === 'stopped') {
const ds = new DockerContainerServiceDriver()
await ds.removeService(app)
}

// save app to recycle collection
const recycle = new RecycleCollector(Constants.cn.applications)
const saved = await recycle.insert(app)
Expand Down

0 comments on commit 6ac01d8

Please sign in to comment.