Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Add support for roles protection

Yehor Dremliuhae6a410e8d58e
diff --git a/apps/production/src/project/dto/project-id.dto.ts b/apps/production/src/project/dto/project-id.dto.ts
new file mode 100644
index 000000000..4c33ab2c7
--- /dev/null
+++ b/apps/production/src/project/dto/project-id.dto.ts
@@ -0,0 +1,11 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { Matches, IsNotEmpty } from 'class-validator'
+
+const PROJECT_ID_REGEX = /^(?!.*--)[a-zA-Z0-9-]{12}$/
+
+export class ProjectIdDto {
+ @ApiProperty()
+ @Matches(PROJECT_ID_REGEX)
+ @IsNotEmpty()
+ readonly projectId: string
+}
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index bbe4c2f29..cf850aa11 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -102,6 +102,7 @@ import {
} from './dto'
import { ProjectsViewsRepository } from './repositories/projects-views.repository'
import { ProjectViewEntity } from './entity/project-view.entity'
+import { ProjectIdDto } from './dto/project-id.dto'
const PROJECTS_MAXIMUM = 50
@@ -397,6 +398,7 @@ export class ProjectController {
}
}
+ @ApiBearerAuth()
@Post('/')
@ApiResponse({ status: 201, type: Project })
@Auth([], true)
@@ -1896,14 +1898,31 @@ export class ProjectController {
@ApiOperation({ summary: 'Get project views' })
@ApiOkResponse({ type: ProjectViewEntity })
+ @ApiBearerAuth()
@Get(':projectId/views')
- async getProjectViews(@Param('projectId') projectId: string) {
- const project = await this.projectService.findProject(projectId)
+ @UseGuards(JwtAccessTokenGuard, RolesGuard)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ async getProjectViews(
+ @Param() params: ProjectIdDto,
+ @CurrentUserId() userId: string,
+ ) {
+ const project = await this.projectService.findProject(params.projectId, [
+ 'admin',
+ 'share',
+ ])
if (!project) {
throw new NotFoundException('Project not found.')
}
- return this.projectsViewsRepository.findViews(projectId)
+ const user = await this.userService.findUserV2(userId, ['roles'])
+
+ if (!user) {
+ throw new NotFoundException('User not found.')
+ }
+
+ this.projectService.allowedToManage(project, userId, user.roles)
+
+ return this.projectsViewsRepository.findViews(params.projectId)
}
}
diff --git a/apps/production/src/user/user.service.ts b/apps/production/src/user/user.service.ts
index ea1f92a3c..a9a8de085 100644
--- a/apps/production/src/user/user.service.ts
+++ b/apps/production/src/user/user.service.ts
@@ -594,4 +594,8 @@ export class UserService {
return !user
}
+
+ async findUserV2(id: string, select?: (keyof User)[]) {
+ return this.usersRepository.findOne({ select, where: { id } })
+ }
}