fix: read permission for subpage (#4405)

This commit is contained in:
DarkSky
2023-09-18 16:13:36 +08:00
committed by GitHub
parent cdad7edf15
commit 65bb30558e
2 changed files with 50 additions and 29 deletions

View File

@@ -47,8 +47,10 @@ export class PermissionService {
async isAccessible(ws: string, id: string, user?: string): Promise<boolean> { async isAccessible(ws: string, id: string, user?: string): Promise<boolean> {
if (user) { if (user) {
return await this.tryCheck(ws, user); const hasPermission = await this.tryCheck(ws, user);
} else { if (hasPermission) return true;
}
// check if this is a public workspace // check if this is a public workspace
const count = await this.prisma.workspace.count({ const count = await this.prisma.workspace.count({
where: { id: ws, public: true }, where: { id: ws, public: true },
@@ -79,7 +81,6 @@ export class PermissionService {
return subpages.some(subpage => id.endsWith(subpage)); return subpages.some(subpage => id.endsWith(subpage));
} }
} }
}
async check( async check(
ws: string, ws: string,

View File

@@ -132,6 +132,26 @@ test('should share a page', async t => {
t.is(pages.length, 1, 'failed to get shared pages'); t.is(pages.length, 1, 'failed to get shared pages');
t.is(pages[0], 'page1', 'failed to get shared page: page1'); t.is(pages[0], 'page1', 'failed to get shared page: page1');
const resp1 = await request(app.getHttpServer())
.get(`/api/workspaces/${workspace.id}/docs/${workspace.id}`)
.auth(u1.token.token, { type: 'bearer' });
t.is(resp1.statusCode, 200, 'failed to get root doc with u1 token');
const resp2 = await request(app.getHttpServer()).get(
`/api/workspaces/${workspace.id}/docs/${workspace.id}`
);
t.is(resp2.statusCode, 200, 'should not get root doc without token');
const resp3 = await request(app.getHttpServer())
.get(`/api/workspaces/${workspace.id}/docs/page1`)
.auth(u1.token.token, { type: 'bearer' });
// 404 because we don't put the page doc to server
t.is(resp3.statusCode, 404, 'failed to get shared doc with u1 token');
const resp4 = await request(app.getHttpServer()).get(
`/api/workspaces/${workspace.id}/docs/page1`
);
// 404 because we don't put the page doc to server
t.is(resp4.statusCode, 404, 'should not get shared doc without token');
const msg1 = await sharePage(app, u2.token.token, 'not_exists_ws', 'page2'); const msg1 = await sharePage(app, u2.token.token, 'not_exists_ws', 'page2');
t.is(msg1, 'Permission denied', 'unauthorized user can share page'); t.is(msg1, 'Permission denied', 'unauthorized user can share page');
const msg2 = await revokePage(app, u2.token.token, 'not_exists_ws', 'page2'); const msg2 = await revokePage(app, u2.token.token, 'not_exists_ws', 'page2');