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!

(improvement) Shared projects admin rights (#220)

Andrii Romasiundeb1d6252500
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index e2892aec0..fe8eb66b5 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -34,6 +34,7 @@ import * as _omit from 'lodash/omit'
import * as _split from 'lodash/split'
import * as _head from 'lodash/head'
import * as _filter from 'lodash/filter'
+import * as _find from 'lodash/find'
import * as dayjs from 'dayjs'
import { hash } from 'bcrypt'
@@ -249,16 +250,20 @@ export class ProjectController {
_map(paginated.results, ({ project }) => project.id),
)
- paginated.results = _map(paginated.results, share => ({
- ...share,
- project: {
- ...share.project,
- admin: undefined,
- passwordHash: undefined,
- isLocked: !!share?.project?.admin?.dashboardBlockReason,
- isDataExists: _includes(pidsWithData, share?.project?.id),
- },
- }))
+ paginated.results = _map(paginated.results, share => {
+ const project = processProjectUser(share.project)
+
+ return {
+ ...share,
+ project: {
+ ...project,
+ admin: undefined,
+ passwordHash: undefined,
+ isLocked: !!share?.project?.admin?.dashboardBlockReason,
+ isDataExists: _includes(pidsWithData, share?.project?.id),
+ },
+ }
+ })
return paginated
}
@@ -973,7 +978,7 @@ export class ProjectController {
const project = await this.projectService.findOneWhere(
{ id: pid },
{
- relations: ['admin', 'share'],
+ relations: ['admin', 'share', 'share.user'],
select: ['id', 'admin', 'share'],
},
)
@@ -1087,19 +1092,29 @@ export class ProjectController {
const user = await this.userService.findOne(uid)
const share = await this.projectService.findOneShare(shareId, {
- relations: ['project', 'project.admin'],
+ relations: [
+ 'project',
+ 'project.admin',
+ 'project.share',
+ 'project.share.user',
+ ],
})
if (_isEmpty(share)) {
throw new NotFoundException(`Share with ID ${shareId} does not exist`)
}
- if (share.project?.admin?.id !== uid) {
- throw new NotFoundException(`You are not allowed to edit this share`)
- }
-
this.projectService.allowedToManage(share.project, uid, user.roles)
+ const adminShare = _find(
+ share.project.share,
+ (_share: ProjectShare) => _share.user?.id === uid,
+ )
+
+ if (adminShare?.id === shareId) {
+ throw new NotFoundException('You cannot edit your own role')
+ }
+
const { role } = shareDTO
await this.projectService.updateShare(shareId, {
role,
@@ -1255,15 +1270,21 @@ export class ProjectController {
'DELETE /project/:projectId/subscribers/:subscriberId',
)
- const project = await this.projectService.getProject(
- params.projectId,
- userId,
- )
+ const project = await this.projectService.findOne(params.projectId, {
+ relations: ['share', 'share.user', 'admin'],
+ })
if (!project) {
throw new NotFoundException('Project not found.')
}
+ this.projectService.allowedToManage(
+ project,
+ userId,
+ [],
+ "You are not allowed to manage this project's subscribers",
+ )
+
const subscriber = await this.projectService.getSubscriber(
params.projectId,
params.subscriberId,
@@ -1288,15 +1309,22 @@ export class ProjectController {
@CurrentUserId() userId: string,
) {
this.logger.log({ params, body }, 'POST /project/:projectId/subscribers')
- const project = await this.projectService.getProject(
- params.projectId,
- userId,
- )
+
+ const project = await this.projectService.findOne(params.projectId, {
+ relations: ['share', 'share.user', 'admin'],
+ })
if (!project) {
throw new NotFoundException('Project not found.')
}
+ this.projectService.allowedToManage(
+ project,
+ userId,
+ [],
+ "You are not allowed to manage this project's subscribers",
+ )
+
const user = await this.userService.getUser(userId)
if (user.email === body.email) {
@@ -1399,15 +1427,17 @@ export class ProjectController {
@CurrentUserId() userId: string,
) {
this.logger.log({ params, queries }, 'GET /project/:projectId/subscribers')
- const project = await this.projectService.getProject(
- params.projectId,
- userId,
- )
+
+ const project = await this.projectService.findOne(params.projectId, {
+ relations: ['share', 'share.user', 'admin'],
+ })
if (!project) {
throw new NotFoundException('Project not found.')
}
+ this.projectService.allowedToView(project, userId)
+
return this.projectService.getSubscribers(params.projectId, queries)
}
@@ -1423,15 +1453,21 @@ export class ProjectController {
'PATCH /project/:projectId/subscribers/:subscriberId',
)
- const project = await this.projectService.getProject(
- params.projectId,
- userId,
- )
+ const project = await this.projectService.findOne(params.projectId, {
+ relations: ['share', 'share.user', 'admin'],
+ })
if (!project) {
throw new NotFoundException('Project not found.')
}
+ this.projectService.allowedToManage(
+ project,
+ userId,
+ [],
+ "You are not allowed to manage this project's subscribers",
+ )
+
const subscriber = await this.projectService.getSubscriber(
params.projectId,
params.subscriberId,
@@ -1725,8 +1761,8 @@ export class ProjectController {
const project = await this.projectService.findOneWhere(
{ id: pid },
{
- relations: ['admin'],
- select: ['id', 'admin'],
+ relations: ['admin', 'share', 'share.user'],
+ select: ['id', 'admin', 'share'],
},
)
diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.ts
index 8cd570e78..96c43100c 100644
--- a/apps/production/src/project/project.service.ts
+++ b/apps/production/src/project/project.service.ts
@@ -368,7 +368,13 @@ export class ProjectService {
order: {
project: 'ASC',
},
- relations: ['project', 'project.admin'],
+ relations: [
+ 'project',
+ 'project.admin',
+ 'project.funnels',
+ 'project.share',
+ 'project.share.user',
+ ],
})
return new Pagination<ProjectShare>({