Skip to content

Commit

Permalink
Rebase to new dashboard permission check
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-lansdorf committed Apr 26, 2023
1 parent fd9a785 commit 7579367
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
12 changes: 6 additions & 6 deletions api/src/controller/dashboard_content.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export class DashboardContentController implements interfaces.Controller {
await DashboardPermissionService.checkPermission(
dashboard_id,
'VIEW',
req.body.auth?.role_id >= ROLE_TYPES.ADMIN,
req.locale,
req.body.auth ? (req.body.auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
req.body.auth?.id,
req.body.auth ? (req.body.auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
req.body.auth?.role_id,
);
const result = await this.dashboardContentService.list(dashboard_id, filter, sort, pagination);
res.json(result);
Expand All @@ -84,10 +84,10 @@ export class DashboardContentController implements interfaces.Controller {
await DashboardPermissionService.checkPermission(
dashboard_id,
'EDIT',
req.body.auth?.role_id >= ROLE_TYPES.ADMIN,
req.locale,
req.body.auth ? (req.body.auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
req.body.auth?.id,
req.body.auth ? (req.body.auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
req.body.auth?.role_id,
);
const result = await this.dashboardContentService.create(dashboard_id, name, content, req.locale);
res.json(result);
Expand Down Expand Up @@ -116,10 +116,10 @@ export class DashboardContentController implements interfaces.Controller {
await DashboardPermissionService.checkPermission(
result.dashboard_id,
'VIEW',
req.body.auth?.role_id >= ROLE_TYPES.ADMIN,
req.locale,
req.body.auth ? (req.body.auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
req.body.auth?.id,
req.body.auth ? (req.body.auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
req.body.auth?.role_id,
);
res.json(result);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions api/src/controller/dashboard_permission.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export class DashboardPermissionController implements interfaces.Controller {
500: { description: 'SERVER ERROR', type: SwaggerDefinitionConstant.Response.Type.OBJECT, model: 'ApiError' },
},
})
@httpPost('/get', ensureAuthEnabled, permission(ROLE_TYPES.READER))
@httpPost('/get', ensureAuthEnabled, permission(ROLE_TYPES.READER), validate(DashboardPermissionGetRequest))
public async get(req: express.Request, res: express.Response, next: express.NextFunction): Promise<void> {
try {
const { id } = validate(DashboardPermissionGetRequest, req.body);
const { id } = req.body as DashboardPermissionGetRequest;
const result = await this.dashboardPermissionService.get(id);
res.json(result);
} catch (err) {
Expand Down
8 changes: 4 additions & 4 deletions api/src/services/dashboard_content.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export class DashboardContentService {
await DashboardPermissionService.checkPermission(
dashboard.id,
'EDIT',
auth ? auth.role_id >= ROLE_TYPES.ADMIN : false,
locale,
auth ? (auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
auth?.id,
auth ? (auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
auth?.role_id,
);
if (AUTH_ENABLED && dashboard.is_preset && (!auth?.role_id || auth.role_id < ROLE_TYPES.SUPERADMIN)) {
throw new ApiError(BAD_REQUEST, { message: translate('DASHBOARD_CONTENT_EDIT_REQUIRES_SUPERADMIN', locale) });
Expand Down Expand Up @@ -136,10 +136,10 @@ export class DashboardContentService {
await DashboardPermissionService.checkPermission(
dashboard.id,
'EDIT',
auth ? auth.role_id >= ROLE_TYPES.ADMIN : false,
locale,
auth ? (auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
auth?.id,
auth ? (auth instanceof ApiKey ? 'APIKEY' : 'ACCOUNT') : undefined,
auth?.role_id,
);
if (AUTH_ENABLED && dashboard.is_preset && (!auth?.role_id || auth.role_id < ROLE_TYPES.SUPERADMIN)) {
throw new ApiError(BAD_REQUEST, {
Expand Down
12 changes: 9 additions & 3 deletions api/tests/e2e/04_dashboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ describe('DashboardController', () => {
.send(query);

expect(response.body.code).toEqual('NOT_FOUND');
expect(response.body.detail.message).toContain('Could not find any entity of type "Dashboard" matching');
expect(response.body.detail.message).toContain(
'Could not find any entity of type "DashboardPermission" matching',
);
expect(response.body.detail.message).toContain(notFoundId);
});
});
Expand Down Expand Up @@ -354,7 +356,9 @@ describe('DashboardController', () => {
.send(query);

expect(response.body.code).toEqual('NOT_FOUND');
expect(response.body.detail.message).toContain('Could not find any entity of type "Dashboard" matching');
expect(response.body.detail.message).toContain(
'Could not find any entity of type "DashboardPermission" matching',
);
expect(response.body.detail.message).toContain(notFoundId);
});

Expand Down Expand Up @@ -432,7 +436,9 @@ describe('DashboardController', () => {
.send(query);

expect(response.body.code).toEqual('NOT_FOUND');
expect(response.body.detail.message).toContain('Could not find any entity of type "Dashboard" matching');
expect(response.body.detail.message).toContain(
'Could not find any entity of type "DashboardPermission" matching',
);
expect(response.body.detail.message).toContain(notFoundId);
});

Expand Down
11 changes: 4 additions & 7 deletions api/tests/e2e/10_dashboard_permission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe('DashboardPermissionController', () => {
const query1: DashboardPermissionUpdateRequest = {
id: dashboardId1,
access: [
{ type: 'ACCOUNT', id: readerAccount.id, permission: 'EDIT' },
{ type: 'ACCOUNT', id: readerAccount.id, permission: 'VIEW' },
{ type: 'APIKEY', id: authorApiKey.id, permission: 'EDIT' },
{ type: 'ACCOUNT', id: authorAccount.id, permission: 'VIEW' },
{ type: 'APIKEY', id: readerApiKey.id, permission: 'VIEW' },
Expand All @@ -270,7 +270,7 @@ describe('DashboardPermissionController', () => {
owner_id: superadminLogin.account.id,
owner_type: 'ACCOUNT',
access: [
{ type: 'ACCOUNT', id: readerAccount.id, permission: 'EDIT' },
{ type: 'ACCOUNT', id: readerAccount.id, permission: 'VIEW' },
{ type: 'APIKEY', id: authorApiKey.id, permission: 'EDIT' },
{ type: 'ACCOUNT', id: authorAccount.id, permission: 'VIEW' },
{ type: 'APIKEY', id: readerApiKey.id, permission: 'VIEW' },
Expand All @@ -296,7 +296,7 @@ describe('DashboardPermissionController', () => {
owner_id: superadminLogin.account.id,
owner_type: 'ACCOUNT',
access: [
{ type: 'ACCOUNT', id: readerAccount.id, permission: 'EDIT' },
{ type: 'ACCOUNT', id: readerAccount.id, permission: 'VIEW' },
{ type: 'APIKEY', id: authorApiKey.id, permission: 'EDIT' },
],
});
Expand Down Expand Up @@ -397,7 +397,6 @@ describe('DashboardPermissionController', () => {
const query1: DashboardIDRequest = {
id: dashboardId1,
};

const response1 = await server
.post('/dashboard/details')
.set('Authorization', `Bearer ${authorLogin.token}`)
Expand All @@ -406,11 +405,9 @@ describe('DashboardPermissionController', () => {
code: 'FORBIDDEN',
detail: { message: 'Insufficient privileges for this dashboard' },
});

const query2: DashboardIDRequest = {
id: dashboardId2,
};

const response2 = await server
.put('/dashboard/update')
.set('Authorization', `Bearer ${authorLogin.token}`)
Expand Down Expand Up @@ -439,7 +436,7 @@ describe('DashboardPermissionController', () => {
id: dashboardId1,
owner_id: authorApiKey.id,
owner_type: 'APIKEY',
access: [{ id: readerAccount.id, type: 'ACCOUNT', permission: 'EDIT' }],
access: [{ id: readerAccount.id, type: 'ACCOUNT', permission: 'VIEW' }],
});
});

Expand Down
9 changes: 4 additions & 5 deletions api/tests/e2e/11_dashboard_content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,10 @@ describe('DashboardContentController', () => {
.set('Authorization', `Bearer ${superadminLogin.token}`)
.send(query1);

expect(response2.body).toMatchObject({
total: 0,
offset: 0,
data: [],
});
expect(response2.body.code).toEqual('NOT_FOUND');
expect(response2.body.detail.message).toContain(
'Could not find any entity of type "DashboardPermission" matching',
);
});

it('should fail if not found', async () => {
Expand Down

0 comments on commit 7579367

Please sign in to comment.