Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(improvement) Shared projects admin rights #220

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 70 additions & 34 deletions apps/production/src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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'],
},
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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,
Expand Down Expand Up @@ -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'],
},
)

Expand Down
8 changes: 7 additions & 1 deletion apps/production/src/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>({
Expand Down
Loading