Skip to content

Commit

Permalink
chore(API): Refactor authority check functions in API (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
szabodaniel995 authored Apr 22, 2024
1 parent 4d3bbe4 commit e9d710d
Show file tree
Hide file tree
Showing 37 changed files with 879 additions and 709 deletions.
7 changes: 5 additions & 2 deletions apps/api/src/approval/controller/approval.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import { REDIS_CLIENT } from '../../provider/redis.provider'
import { RedisClientType } from 'redis'
import { mockDeep } from 'jest-mock-extended'
import { ProviderModule } from '../../provider/provider.module'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { CommonModule } from '../../common/common.module'

describe('ApprovalController', () => {
let controller: ApprovalController

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ProviderModule],
imports: [ProviderModule, CommonModule],
controllers: [ApprovalController],
providers: [
ApprovalService,
Expand All @@ -34,7 +36,8 @@ describe('ApprovalController', () => {
{
provide: MAIL_SERVICE,
useClass: MockMailService
}
},
AuthorityCheckerService
]
})
.overrideProvider(REDIS_CLIENT)
Expand Down
7 changes: 5 additions & 2 deletions apps/api/src/approval/service/approval.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import { REDIS_CLIENT } from '../../provider/redis.provider'
import { RedisClientType } from 'redis'
import { mockDeep } from 'jest-mock-extended'
import { ProviderModule } from '../../provider/provider.module'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { CommonModule } from '../../common/common.module'

describe('ApprovalService', () => {
let service: ApprovalService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ProviderModule],
imports: [ProviderModule, CommonModule],
providers: [
ApprovalService,
PrismaService,
Expand All @@ -32,7 +34,8 @@ describe('ApprovalService', () => {
{
provide: MAIL_SERVICE,
useClass: MockMailService
}
},
AuthorityCheckerService
]
})
.overrideProvider(REDIS_CLIENT)
Expand Down
29 changes: 15 additions & 14 deletions apps/api/src/approval/service/approval.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
UpdateVariableMetadata,
UpdateWorkspaceMetadata
} from '../approval.types'
import getWorkspaceWithAuthority from '../../common/get-workspace-with-authority'
import { AuthorityCheckerService } from '../../common/authority-checker.service'

@Injectable()
export class ApprovalService {
Expand All @@ -43,7 +43,8 @@ export class ApprovalService {
private readonly projectService: ProjectService,
private readonly environmentService: EnvironmentService,
private readonly secretService: SecretService,
private readonly variableService: VariableService
private readonly variableService: VariableService,
private readonly authorityCheckerService: AuthorityCheckerService
) {}

async updateApproval(user: User, reason: string, approvalId: Approval['id']) {
Expand Down Expand Up @@ -453,12 +454,12 @@ export class ApprovalService {
actions: ApprovalAction[],
statuses: ApprovalStatus[]
) {
await getWorkspaceWithAuthority(
user.id,
workspaceId,
Authority.MANAGE_APPROVALS,
this.prisma
)
await this.authorityCheckerService.checkAuthorityOverWorkspace({
userId: user.id,
entity: { id: workspaceId },
authority: Authority.MANAGE_APPROVALS,
prisma: this.prisma
})

return await this.prisma.approval.findMany({
where: {
Expand Down Expand Up @@ -493,12 +494,12 @@ export class ApprovalService {
actions: ApprovalAction[],
statuses: ApprovalStatus[]
) {
await getWorkspaceWithAuthority(
user.id,
workspaceId,
Authority.READ_WORKSPACE,
this.prisma
)
await this.authorityCheckerService.checkAuthorityOverWorkspace({
userId: user.id,
entity: { id: workspaceId },
authority: Authority.READ_WORKSPACE,
prisma: this.prisma
})

return this.prisma.approval.findMany({
where: {
Expand Down
Loading

0 comments on commit e9d710d

Please sign in to comment.