@@ -13,11 +13,15 @@ import { PrismaService } from 'src/database/prisma.service';
13
13
import { AbilityAction } from 'src/ability/ability.action' ;
14
14
import { AppAbility } from 'src/ability/ability.factory' ;
15
15
import { CompanyWhereInput } from 'src/core/@generated/company/company-where.input' ;
16
- import { relationAbilityChecker } from 'src/ability/ability.util' ;
16
+ import { CompanyWhereUniqueInput } from 'src/core/@generated/company/company-where-unique.input' ;
17
+ import {
18
+ convertToWhereInput ,
19
+ relationAbilityChecker ,
20
+ } from 'src/ability/ability.util' ;
17
21
import { assert } from 'src/utils/assert' ;
18
22
19
23
class CompanyArgs {
20
- where ?: CompanyWhereInput ;
24
+ where ?: CompanyWhereUniqueInput | CompanyWhereInput ;
21
25
[ key : string ] : any ;
22
26
}
23
27
@@ -29,9 +33,18 @@ export class ManageCompanyAbilityHandler implements IAbilityHandler {
29
33
}
30
34
31
35
@Injectable ( )
32
- export class ReadCompanyAbilityHandler implements IAbilityHandler {
33
- handle ( ability : AppAbility ) {
34
- return ability . can ( AbilityAction . Read , 'Company' ) ;
36
+ export class ReadOneCompanyAbilityHandler implements IAbilityHandler {
37
+ constructor ( private readonly prismaService : PrismaService ) { }
38
+
39
+ async handle ( ability : AppAbility , context : ExecutionContext ) {
40
+ const gqlContext = GqlExecutionContext . create ( context ) ;
41
+ const args = gqlContext . getArgs < CompanyArgs > ( ) ;
42
+ const company = await this . prismaService . client . company . findFirst ( {
43
+ where : args . where ,
44
+ } ) ;
45
+ assert ( company , '' , NotFoundException ) ;
46
+
47
+ return ability . can ( AbilityAction . Read , subject ( 'Company' , company ) ) ;
35
48
}
36
49
}
37
50
@@ -65,10 +78,11 @@ export class UpdateCompanyAbilityHandler implements IAbilityHandler {
65
78
async handle ( ability : AppAbility , context : ExecutionContext ) {
66
79
const gqlContext = GqlExecutionContext . create ( context ) ;
67
80
const args = gqlContext . getArgs < CompanyArgs > ( ) ;
68
- const company = await this . prismaService . client . company . findFirst ( {
69
- where : args . where ,
81
+ const where = convertToWhereInput ( args . where ) ;
82
+ const companies = await this . prismaService . client . company . findMany ( {
83
+ where,
70
84
} ) ;
71
- assert ( company , '' , NotFoundException ) ;
85
+ assert ( companies . length , '' , NotFoundException ) ;
72
86
73
87
const allowed = await relationAbilityChecker (
74
88
'Company' ,
@@ -81,7 +95,18 @@ export class UpdateCompanyAbilityHandler implements IAbilityHandler {
81
95
return false ;
82
96
}
83
97
84
- return ability . can ( AbilityAction . Update , subject ( 'Company' , company ) ) ;
98
+ for ( const company of companies ) {
99
+ const allowed = ability . can (
100
+ AbilityAction . Delete ,
101
+ subject ( 'Company' , company ) ,
102
+ ) ;
103
+
104
+ if ( ! allowed ) {
105
+ return false ;
106
+ }
107
+ }
108
+
109
+ return true ;
85
110
}
86
111
}
87
112
@@ -92,11 +117,23 @@ export class DeleteCompanyAbilityHandler implements IAbilityHandler {
92
117
async handle ( ability : AppAbility , context : ExecutionContext ) {
93
118
const gqlContext = GqlExecutionContext . create ( context ) ;
94
119
const args = gqlContext . getArgs < CompanyArgs > ( ) ;
95
- const company = await this . prismaService . client . company . findFirst ( {
96
- where : args . where ,
120
+ const where = convertToWhereInput ( args . where ) ;
121
+ const companies = await this . prismaService . client . company . findMany ( {
122
+ where,
97
123
} ) ;
98
- assert ( company , '' , NotFoundException ) ;
124
+ assert ( companies . length , '' , NotFoundException ) ;
125
+
126
+ for ( const company of companies ) {
127
+ const allowed = ability . can (
128
+ AbilityAction . Delete ,
129
+ subject ( 'Company' , company ) ,
130
+ ) ;
131
+
132
+ if ( ! allowed ) {
133
+ return false ;
134
+ }
135
+ }
99
136
100
- return ability . can ( AbilityAction . Delete , subject ( 'Company' , company ) ) ;
137
+ return true ;
101
138
}
102
139
}
0 commit comments