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!

added support for public dashboards

Blue Mouse742acf86d0fd
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index 2dd85f621..892f806a7 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -42,7 +42,7 @@ export class AnalyticsService {
if (_isEmpty(project)) {
project = await this.projectService.findOne(pid, {
relations: ['admin'],
- select: ['origins', 'active', 'admin']
+ select: ['origins', 'active', 'admin', 'public']
})
if (_isEmpty(project)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
await redis.set(pidKey, JSON.stringify(project), 'EX', redisProjectCacheTimeout)
@@ -59,7 +59,7 @@ export class AnalyticsService {
async checkProjectAccess(pid: string, uid: string): Promise<void> {
const project = await this.getRedisProject(pid)
- this.projectService.allowedToManage(project, uid)
+ this.projectService.allowedToView(project, uid)
}
// Returns amount of existing events starting from month
diff --git a/src/app.module.ts b/src/app.module.ts
index 1c4d1afcd..5e708276c 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -26,7 +26,7 @@ import { PingModule } from './ping/ping.module'
username: process.env.MYSQL_USER,
password: process.env.MYSQL_ROOT_PASSWORD,
database: process.env.MYSQL_DATABASE,
- synchronize: true,
+ synchronize: false,
entities: [User, ActionToken, Project],
}),
ScheduleModule.forRoot(),
diff --git a/src/project/dto/project.dto.ts b/src/project/dto/project.dto.ts
index 180dc283e..af7329728 100644
--- a/src/project/dto/project.dto.ts
+++ b/src/project/dto/project.dto.ts
@@ -5,7 +5,7 @@ export class ProjectDTO {
@ApiProperty({
example: 'Your awesome project',
required: true,
- description: 'A display name for your project'
+ description: 'A display name for your project',
})
@IsNotEmpty()
name: string
@@ -13,7 +13,7 @@ export class ProjectDTO {
@ApiProperty({
example: 'aUn1quEid-3',
required: true,
- description: 'A unique project ID'
+ description: 'A unique project ID',
})
@IsNotEmpty()
id: string
@@ -21,13 +21,19 @@ export class ProjectDTO {
@ApiProperty({
example: 'localhost:3000,example.com',
required: false,
- description: 'An array allowed origins'
+ description: 'An array allowed origins',
})
origins: string[]
@ApiProperty({
required: false,
- description: 'The project\'s state. If enabled - all the incoming analytics data will be saved.'
+ description: 'The project\'s state. If enabled - all the incoming analytics data will be saved.',
})
active: boolean
+
+ @ApiProperty({
+ required: false,
+ description: 'When true, anyone on the internet (including Google) would be able to see the project\'s Dashboard.',
+ })
+ public: boolean
}
diff --git a/src/project/entity/project.entity.ts b/src/project/entity/project.entity.ts
index e2ff45c3c..7202c1310 100644
--- a/src/project/entity/project.entity.ts
+++ b/src/project/entity/project.entity.ts
@@ -26,6 +26,12 @@ export class Project {
})
active: boolean
+ @ApiProperty()
+ @Column({
+ default: false,
+ })
+ public: boolean
+
@ApiProperty({ type: () => User })
@ManyToOne(() => User, user => user.projects)
admin: User
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index 3a4bd0210..bb9c8b3b0 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -38,10 +38,10 @@ export class ProjectController {
@ApiResponse({ status: 200, type: [Project] })
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- async get(@CurrentUserId() userId: string, @Query('take') take: number | undefined, @Query('skip') skip: number | undefined): Promise<Pagination<Project> | Project[]> {
- this.logger.log({ userId, take, skip }, 'GET /project')
+ async get(@CurrentUserId() uid: string, @Query('take') take: number | undefined, @Query('skip') skip: number | undefined): Promise<Pagination<Project> | Project[]> {
+ this.logger.log({ uid, take, skip }, 'GET /project')
const where = Object()
- where.admin = userId
+ where.admin = uid
return await this.projectService.paginate({ take, skip }, where)
}
@@ -50,12 +50,15 @@ export class ProjectController {
@ApiResponse({ status: 200, type: Project })
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- async getOne(@Param('id') id: string): Promise<Project> {
+ async getOne(@Param('id') id: string, @CurrentUserId() uid: string): Promise<Project> {
this.logger.log({ id }, 'GET /project/:id')
if (!isValidPID(id)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
- const project = await this.projectService.findOne(id)
+ const project = await this.projectService.findOne(id)
if (_isEmpty(project)) throw new NotFoundException('Project was not found in the database')
+
+ this.projectService.allowedToView(project, uid)
+
return project
}
@@ -63,9 +66,9 @@ export class ProjectController {
@ApiResponse({ status: 201, type: Project })
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- async create(@Body() projectDTO: ProjectDTO, @CurrentUserId() userId: string): Promise<Project> {
- this.logger.log({ projectDTO, userId }, 'POST /project')
- const user = await this.userService.findOneWithRelations(userId, ['projects'])
+ async create(@Body() projectDTO: ProjectDTO, @CurrentUserId() uid: string): Promise<Project> {
+ this.logger.log({ projectDTO, uid }, 'POST /project')
+ const user = await this.userService.findOneWithRelations(uid, ['projects'])
const maxProjects = ACCOUNT_PLANS[user.planCode].maxProjects
if (!user.isActive) {
@@ -107,8 +110,8 @@ export class ProjectController {
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
@ApiResponse({ status: 200, type: Project })
- async update(@Param('id') id: string, @Body() projectDTO: ProjectDTO, @CurrentUserId() userId: string): Promise<any> {
- this.logger.log({ projectDTO, userId, id }, 'PUT /project/:id')
+ async update(@Param('id') id: string, @Body() projectDTO: ProjectDTO, @CurrentUserId() uid: string): Promise<any> {
+ this.logger.log({ projectDTO, uid, id }, 'PUT /project/:id')
this.projectService.validateProject(projectDTO)
const project = await this.projectService.findOneWithRelations(id)
@@ -116,11 +119,12 @@ export class ProjectController {
if (_isEmpty(project)) {
throw new NotFoundException()
}
- this.projectService.allowedToManage(project, userId)
+ this.projectService.allowedToManage(project, uid)
project.active = projectDTO.active
project.origins = _map(projectDTO.origins, _trim)
project.name = projectDTO.name
+ project.public = projectDTO.public
await this.projectService.update(id, project)
@@ -140,8 +144,8 @@ export class ProjectController {
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
@ApiResponse({ status: 204, description: 'Empty body' })
- async delete(@Param('id') id: string, @CurrentUserId() userId: string): Promise<any> {
- this.logger.log({ userId, id }, 'DELETE /project/:id')
+ async delete(@Param('id') id: string, @CurrentUserId() uid: string): Promise<any> {
+ this.logger.log({ uid, id }, 'DELETE /project/:id')
if (!isValidPID(id)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
const project = await this.projectService.findOneWhere({ id }, {
@@ -152,7 +156,7 @@ export class ProjectController {
if (_isEmpty(project)) {
throw new NotFoundException(`Project with ID ${id} does not exist`)
}
- this.projectService.allowedToManage(project, userId)
+ this.projectService.allowedToManage(project, uid)
const query1 = `ALTER table analytics DELETE WHERE pid='${id}'`
const query2 = `ALTER table customEV DELETE WHERE pid='${id}'`
diff --git a/src/project/project.service.ts b/src/project/project.service.ts
index 18e3b23d3..0cfbb6c77 100644
--- a/src/project/project.service.ts
+++ b/src/project/project.service.ts
@@ -77,6 +77,14 @@ export class ProjectService {
return this.projectsRepository.findOne({ where, ...params })
}
+ allowedToView(project: Project, uid: string): void {
+ if (project.public || uid === project.admin.id) {
+ return
+ } else {
+ throw new ForbiddenException('You are not allowed to access this project')
+ }
+ }
+
allowedToManage(project: Project, uid: string): void {
if (uid === project.admin.id) {
return