Skip to content

Commit

Permalink
fix(sys-server): fix app collaborators' uid type to objectid;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Dec 9, 2021
1 parent bbf3c17 commit 835bf20
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
6 changes: 3 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-12-07 13:50:40
* @LastEditTime: 2021-12-09 08:21:51
* @Description: Application APIs
*/

Expand Down Expand Up @@ -52,7 +52,7 @@ export interface ApplicationStruct {
version: string
}[]
collaborators: {
uid: string
uid: ObjectId
roles: string[]
created_at: number
}[]
Expand Down Expand Up @@ -139,7 +139,7 @@ export function getUserRolesOfApplication(uid: string, app: ApplicationStruct) {
}

// reject if not the collaborator
const [found] = app.collaborators.filter(co => co.uid === uid)
const [found] = app.collaborators.filter(co => co.uid.toHexString() === uid)
if (!found) {
return []
}
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-12-07 09:45:49
* @LastEditTime: 2021-12-09 08:22:20
* @Description:
*/

Expand All @@ -25,7 +25,7 @@ export async function checkPermission(uid: string, permission: string, app: Appl
if (uid === app.created_by.toHexString()) return 0

// reject while uid is not the collaborator
const [collaborator] = app.collaborators?.filter(co => co.uid === uid) ?? []
const [collaborator] = app.collaborators?.filter(co => co.uid.toHexString() === uid) ?? []
if (!collaborator) return 403

// reject while uid not have the permission
Expand Down
17 changes: 8 additions & 9 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-10-08 01:23:08
* @LastEditTime: 2021-12-09 08:29:02
* @Description:
*/

Expand Down Expand Up @@ -36,11 +36,10 @@ export async function handleGetCollaborators(req: Request, res: Response) {
}

const db = DatabaseAgent.db
const co_ids = app.collaborators.map(co => co.uid)
const docs = await db.collection(Constants.cn.accounts)
.find({
_id: {
$in: co_ids.map(id => new ObjectId(id))
$in: app.collaborators.map(co => co.uid)
}
}, {
projection: { '_id': 1, 'username': 1, 'name': 1 }
Expand Down Expand Up @@ -82,21 +81,21 @@ export async function handleInviteCollaborator(req: Request, res: Response) {
}

// reject if collaborator exists
const exists = app.collaborators.filter(it => it.uid === member_id)
const exists = app.collaborators.filter(it => it.uid.toHexString() === member_id)
if (exists.length) {
return res.status(422).send('collaborator already exists')
}

// reject if the application owner get here
if (app.created_by === member_id) {
if (app.created_by.toHexString() === member_id) {
return res.status(422).send('collaborator is already the owner of this application')
}

// add a collaborator
const collaborator = {
uid: member_id,
uid: new ObjectId(member_id),
roles,
created_at: Date.now()
created_at: new Date()
}
const ret = await db.collection(Constants.cn.applications)
.updateOne({
Expand Down Expand Up @@ -176,7 +175,7 @@ export async function handleRemoveCollaborator(req: Request, res: Response) {

// check collaborator_id
const collaborator_id = req.params.collaborator_id
const [found] = app.collaborators.filter(co => co.uid === collaborator_id)
const [found] = app.collaborators.filter(co => co.uid.toHexString() === collaborator_id)
if (!found) {
return res.status(422).send('invalid collaborator_id')
}
Expand All @@ -185,7 +184,7 @@ export async function handleRemoveCollaborator(req: Request, res: Response) {
const r = await db.collection(Constants.cn.applications)
.updateOne({ appid }, {
$pull: {
collaborators: { uid: collaborator_id }
collaborators: { uid: new ObjectId(collaborator_id) }
}
})

Expand Down

0 comments on commit 835bf20

Please sign in to comment.