Skip to content

Commit

Permalink
patch: improved query of otp
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Jan 25, 2024
1 parent 09805a5 commit d9d9260
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
10 changes: 5 additions & 5 deletions apps/api/src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ export class AuthService {
}

const isOtpValid =
(await this.prisma.otp.count({
(await this.prisma.otp.findUnique({
where: {
code: otp,
user: {
email
userCode: {
code: otp,
userId: user.id
},
expiresAt: {
gt: new Date()
}
}
})) > 0
})) !== null

if (!isOtpValid) {
this.logger.error(`Invalid login code for ${email}: ${otp}`)
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ model User {
projects Project[] // Stores the projects the user updated
environments Environment[] // Stores the environments the user updated
secretVersion SecretVersion[]
@@index([email], name: "email")
}

model Subscription {
Expand Down Expand Up @@ -212,6 +214,9 @@ model Otp {
userId String
createdAt DateTime @default(now())
expiresAt DateTime
@@unique([userId, code], name: "userCode")
@@index([expiresAt], name: "expiresAt")
}

model Workspace {
Expand Down
71 changes: 38 additions & 33 deletions apps/api/src/workspace/misc/workspace.permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ export class WorkspacePermission {
if (user.isAdmin) Promise.resolve()

// Else, check if the user is a workspace admin
const memberships = await this.resolveWorkspacesOfUser(user)
const membership = memberships.find(
(membership) => membership.workspaceId === workspaceId
)
if (!membership) {
throw new UnauthorizedException('User is not a member of the workspace')
}
const membership = await this.getMembership(workspaceId, user.id)

if (membership.role !== WorkspaceRole.OWNER) {
throw new UnauthorizedException('Atleast OWNER role is required')
}
Expand All @@ -34,13 +29,9 @@ export class WorkspacePermission {
if (user.isAdmin) Promise.resolve()

// Else, check if the user is a workspace admin
const memberships = await this.resolveWorkspacesOfUser(user)
const membership = memberships.find(
(membership) => membership.workspaceId === workspaceId
)
if (!membership) {
throw new UnauthorizedException('User is not a member of the workspace')
}
// const memberships = await this.resolveWorkspacesOfUser(user)
const membership = await this.getMembership(workspaceId, user.id)

if (
membership.role !== WorkspaceRole.OWNER &&
membership.role !== WorkspaceRole.MAINTAINER
Expand All @@ -57,29 +48,43 @@ export class WorkspacePermission {
if (user.isAdmin) Promise.resolve()

// Else, check if the user is a workspace admin
const memberships = await this.resolveWorkspacesOfUser(user)
const membership = memberships.find(
(membership) => membership.workspaceId === workspaceId
)
if (!membership) {
throw new UnauthorizedException('User is not a member of the workspace')
}
this.getMembership(workspaceId, user.id)
}

private async resolveWorkspacesOfUser(
user: User
): Promise<{ workspaceId: Workspace['id']; role: WorkspaceRole }[]> {
// const memberships = await this.repository.getWorkspaceMembershipsOfUser(
// user.id
// )
const memberships = await this.prisma.workspaceMember.findMany({
private async getMembership(
workspaceId: Workspace['id'],
userId: User['id']
) {
const membership = await this.prisma.workspaceMember.findUnique({
where: {
userId: user.id
workspaceId_userId: {
workspaceId,
userId
}
}
})
return memberships.map((membership) => ({
workspaceId: membership.workspaceId,
role: membership.role
}))

if (!membership) {
throw new UnauthorizedException('User is not a member of the workspace')
}

return membership
}

// private async resolveWorkspacesOfUser(
// user: User
// ): Promise<{ workspaceId: Workspace['id']; role: WorkspaceRole }[]> {
// // const memberships = await this.repository.getWorkspaceMembershipsOfUser(
// // user.id
// // )
// const memberships = await this.prisma.workspaceMember.findMany({
// where: {
// userId: user.id
// }
// })
// return memberships.map((membership) => ({
// workspaceId: membership.workspaceId,
// role: membership.role
// }))
// }
}

0 comments on commit d9d9260

Please sign in to comment.