Skip to content

Commit fe41fba

Browse files
soupihasura-bot
authored andcommitted
mssql: Test upserts with permissions
PR-URL: hasura/graphql-engine-mono#3280 GitOrigin-RevId: 861d4db59f75c83daef975fb3471b3c07889c9bc
1 parent b892927 commit fe41fba

12 files changed

Lines changed: 394 additions & 27 deletions

File tree

‎server/src-lib/Hasura/Backends/MSSQL/Schema/IfMatched.hs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ ifMatchedObjectParser sourceName tableInfo maybeSelectPerms maybeUpdatePerms = r
7272
selectPerms <- hoistMaybe maybeSelectPerms
7373
updatePerms <- hoistMaybe maybeUpdatePerms
7474
matchColumnsEnum <- MaybeT $ tableInsertMatchColumnsEnum sourceName tableInfo selectPerms
75-
updateColumnsEnum <- MaybeT $ tableUpdateColumnsEnum tableInfo updatePerms
75+
updateColumnsEnum <- lift $ updateColumnsPlaceholderParser tableInfo updatePerms
7676

7777
-- The style of the above code gives me some cognitive dissonance: We could
7878
-- push the @hoistMaybe@ checks further away to callers, but not the enum
@@ -98,7 +98,10 @@ ifMatchedObjectParser sourceName tableInfo maybeSelectPerms maybeUpdatePerms = r
9898
_imMatchColumns <-
9999
P.fieldWithDefault matchColumnsName Nothing (G.VList []) (P.list matchColumnsEnum)
100100
_imUpdateColumns <-
101-
P.fieldWithDefault updateColumnsName Nothing (G.VList []) (P.list updateColumnsEnum)
101+
P.fieldWithDefault updateColumnsName Nothing (G.VList []) (P.list updateColumnsEnum) `P.bindFields` \cs ->
102+
-- this can only happen if the placeholder was used
103+
sequenceA cs `onNothing` parseError "erroneous column name"
104+
102105
pure $ IfMatched {..}
103106

104107
-- | Table insert_match_columns enum

‎server/src-lib/Hasura/Backends/Postgres/Schema/OnConflict.hs‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ conflictObjectParser ::
6565
UpdPermInfo ('Postgres pgKind) ->
6666
m (Parser 'Input n (IR.OnConflictClause ('Postgres pgKind) (UnpreparedValue ('Postgres pgKind))))
6767
conflictObjectParser sourceName tableInfo constraints selectPerms updatePerms = do
68-
updateColumnsEnum <- updateColumnsPlaceholderParser
68+
updateColumnsEnum <- updateColumnsPlaceholderParser tableInfo updatePerms
6969
constraintParser <- conflictConstraint constraints sourceName tableInfo
7070
whereExpParser <- boolExp sourceName tableInfo selectPerms
7171
tableGQLName <- getTableGQLName tableInfo
@@ -93,24 +93,6 @@ conflictObjectParser sourceName tableInfo constraints selectPerms updatePerms =
9393
where
9494
tableName = tableInfoName tableInfo
9595

96-
-- If there's no column for which the current user has "update"
97-
-- permissions, this functions returns an enum that only contains a
98-
-- placeholder, so as to still allow this type to exist in the schema.
99-
updateColumnsPlaceholderParser :: m (Parser 'Both n (Maybe (Column ('Postgres pgKind))))
100-
updateColumnsPlaceholderParser = do
101-
maybeEnum <- tableUpdateColumnsEnum tableInfo updatePerms
102-
case maybeEnum of
103-
Just e -> pure $ Just <$> e
104-
Nothing -> do
105-
tableGQLName <- getTableGQLName tableInfo
106-
enumName <- P.mkTypename $ tableGQLName <> $$(G.litName "_update_column")
107-
pure $
108-
P.enum enumName (Just $ G.Description $ "placeholder for update columns of table " <> tableName <<> " (current role has no relevant permissions)") $
109-
pure
110-
( P.Definition @P.EnumValueInfo $$(G.litName "_PLACEHOLDER") (Just $ G.Description "placeholder (do not use)") P.EnumValueInfo,
111-
Nothing
112-
)
113-
11496
-- | Constructs a Parser for the name of the constraints on a given table.
11597
--
11698
-- The TableCoreInfo of a given table contains a list of unique or primary key

‎server/src-lib/Hasura/GraphQL/Schema/Table.hs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Hasura.GraphQL.Schema.Table
33
( getTableGQLName,
44
tableSelectColumnsEnum,
55
tableUpdateColumnsEnum,
6+
updateColumnsPlaceholderParser,
67
tablePermissions,
78
tableSelectPermissions,
89
tableSelectFields,
@@ -106,6 +107,28 @@ tableUpdateColumnsEnum tableInfo updatePermissions = do
106107
where
107108
define name = P.Definition name (Just $ G.Description "column name") P.EnumValueInfo
108109

110+
-- If there's no column for which the current user has "update"
111+
-- permissions, this functions returns an enum that only contains a
112+
-- placeholder, so as to still allow this type to exist in the schema.
113+
updateColumnsPlaceholderParser ::
114+
MonadBuildSchema backend r m n =>
115+
TableInfo backend ->
116+
UpdPermInfo backend ->
117+
m (Parser 'Both n (Maybe (Column backend)))
118+
updateColumnsPlaceholderParser tableInfo updatePerms = do
119+
maybeEnum <- tableUpdateColumnsEnum tableInfo updatePerms
120+
case maybeEnum of
121+
Just e -> pure $ Just <$> e
122+
Nothing -> do
123+
tableGQLName <- getTableGQLName tableInfo
124+
enumName <- P.mkTypename $ tableGQLName <> $$(G.litName "_update_column")
125+
pure $
126+
P.enum enumName (Just $ G.Description $ "placeholder for update columns of table " <> tableInfoName tableInfo <<> " (current role has no relevant permissions)") $
127+
pure
128+
( P.Definition @P.EnumValueInfo $$(G.litName "_PLACEHOLDER") (Just $ G.Description "placeholder (do not use)") P.EnumValueInfo,
129+
Nothing
130+
)
131+
109132
tablePermissions ::
110133
forall m n r b.
111134
(Backend b, MonadSchema n m, MonadRole r m) =>

‎server/src-lib/Hasura/RQL/IR/BoolExp.hs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ data PartialSQLExp (b :: BackendType)
195195

196196
deriving instance (Backend b) => Eq (PartialSQLExp b)
197197

198+
deriving instance (Backend b) => Show (PartialSQLExp b)
199+
198200
instance (Backend b, NFData (BooleanOperators b (PartialSQLExp b))) => NFData (PartialSQLExp b)
199201

200202
instance (Backend b, Hashable (BooleanOperators b (PartialSQLExp b))) => Hashable (PartialSQLExp b)

‎server/src-lib/Hasura/RQL/Types/Table.hs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ deriving instance
276276
) =>
277277
Eq (InsPermInfo b)
278278

279+
deriving instance
280+
( Backend b,
281+
Show (BooleanOperators b (PartialSQLExp b))
282+
) =>
283+
Show (InsPermInfo b)
284+
279285
instance
280286
( Backend b,
281287
NFData (BooleanOperators b (PartialSQLExp b))
@@ -383,6 +389,12 @@ deriving instance
383389
) =>
384390
Eq (SelPermInfo b)
385391

392+
deriving instance
393+
( Backend b,
394+
Show (BooleanOperators b (PartialSQLExp b))
395+
) =>
396+
Show (SelPermInfo b)
397+
386398
instance
387399
( Backend b,
388400
NFData (BooleanOperators b (PartialSQLExp b))
@@ -420,6 +432,12 @@ deriving instance
420432
) =>
421433
Eq (UpdPermInfo b)
422434

435+
deriving instance
436+
( Backend b,
437+
Show (BooleanOperators b (PartialSQLExp b))
438+
) =>
439+
Show (UpdPermInfo b)
440+
423441
instance
424442
( Backend b,
425443
NFData (BooleanOperators b (PartialSQLExp b))
@@ -454,6 +472,12 @@ deriving instance
454472
) =>
455473
Eq (DelPermInfo b)
456474

475+
deriving instance
476+
( Backend b,
477+
Show (BooleanOperators b (PartialSQLExp b))
478+
) =>
479+
Show (DelPermInfo b)
480+
457481
instance
458482
( Backend b,
459483
NFData (BooleanOperators b (PartialSQLExp b))
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
- description: Upserts article data via GraphQL mutation as Restricted role (no column in update permissions)
2+
url: /v1/graphql
3+
status: 200
4+
headers:
5+
X-Hasura-Role: restricted
6+
X-Hasura-User-Id: '1'
7+
response:
8+
data:
9+
insert_article:
10+
affected_rows: 0
11+
returning: []
12+
query:
13+
query: |
14+
mutation insert_article {
15+
insert_article (
16+
objects: [
17+
{
18+
id: 1
19+
content: "Updated Article 1 content"
20+
author_id: 1
21+
}
22+
],
23+
if_matched: {
24+
match_columns: id
25+
update_columns: []
26+
}
27+
) {
28+
affected_rows
29+
returning {
30+
id
31+
title
32+
content
33+
author_id
34+
}
35+
}
36+
}
37+
38+
- description: Upserting article data via GraphQL mutation as Restricted role errors when using _PLACEHOLDER
39+
url: /v1/graphql
40+
status: 200
41+
headers:
42+
X-Hasura-Role: restricted
43+
X-Hasura-User-Id: '1'
44+
response:
45+
errors:
46+
- extensions:
47+
path: $.selectionSet.insert_article.args.if_matched
48+
code: validation-failed
49+
message: erroneous column name
50+
query:
51+
query: |
52+
mutation insert_article {
53+
insert_article (
54+
objects: [
55+
{
56+
id: 1
57+
content: "Updated Article 1 content"
58+
author_id: 1
59+
}
60+
],
61+
if_matched: {
62+
match_columns: id
63+
update_columns: [_PLACEHOLDER]
64+
}
65+
) {
66+
affected_rows
67+
returning {
68+
id
69+
title
70+
content
71+
author_id
72+
}
73+
}
74+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
- description: Upserts article data via GraphQL mutation as user_with_all_perms role
2+
url: /v1/graphql
3+
status: 200
4+
headers:
5+
X-Hasura-Role: user_with_all_perms
6+
X-Hasura-User-Id: '1'
7+
response:
8+
data:
9+
insert_article:
10+
affected_rows: 1
11+
returning:
12+
- id: 1
13+
title: Article 1
14+
content: Updated Article 1 content
15+
author_id: 1
16+
query:
17+
query: |
18+
mutation insert_article {
19+
insert_article (
20+
objects: [
21+
{
22+
id: 1
23+
title: "unexpected"
24+
content: "Updated Article 1 content"
25+
author_id: 1
26+
}
27+
],
28+
if_matched: {
29+
match_columns: id
30+
update_columns: [content]
31+
}
32+
) {
33+
affected_rows
34+
returning {
35+
id
36+
title
37+
content
38+
author_id
39+
}
40+
}
41+
}
42+
43+
- description: Upserting article data via GraphQL mutation as user role fails without update permissions
44+
url: /v1/graphql
45+
status: 200
46+
headers:
47+
X-Hasura-Role: user_with_select_and_insert
48+
X-Hasura-User-Id: '1'
49+
response:
50+
errors:
51+
- extensions:
52+
path: $.selectionSet.insert_article
53+
code: validation-failed
54+
message: '"insert_article" has no argument named "if_matched"'
55+
query:
56+
query: |
57+
mutation insert_article {
58+
insert_article (
59+
objects: [
60+
{
61+
id: 1
62+
title: "unexpected"
63+
content: "Updated Article 1 content"
64+
author_id: 1
65+
}
66+
],
67+
if_matched: {
68+
match_columns: id
69+
update_columns: [content]
70+
}
71+
) {
72+
affected_rows
73+
}
74+
}
75+
76+
- description: Upserting article data via GraphQL mutation as user role fails without select permissions
77+
url: /v1/graphql
78+
status: 200
79+
headers:
80+
X-Hasura-Role: user_with_update_and_insert
81+
X-Hasura-User-Id: '1'
82+
response:
83+
errors:
84+
- extensions:
85+
path: $.selectionSet.insert_article
86+
code: validation-failed
87+
message: '"insert_article" has no argument named "if_matched"'
88+
query:
89+
query: |
90+
mutation insert_article {
91+
insert_article (
92+
objects: [
93+
{
94+
id: 1
95+
title: "unexpected"
96+
content: "Updated Article 1 content"
97+
author_id: 1
98+
}
99+
],
100+
if_matched: {
101+
match_columns: id
102+
update_columns: [content]
103+
}
104+
) {
105+
affected_rows
106+
}
107+
}

‎server/tests-py/queries/graphql_mutation/insert/permissions/schema_setup_mssql.yaml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ args:
1111
[is_registered] bit NOT NULL DEFAULT 0
1212
);
1313
14-
INSERT INTO
15-
author([name], [is_registered])
16-
VALUES
17-
('Author 1', 1),
18-
('Author 2', 0);
19-
2014
CREATE TABLE article (
2115
id int identity NOT NULL PRIMARY KEY,
2216
title TEXT,

0 commit comments

Comments
 (0)