Skip to content

Commit d4cc9d7

Browse files
author
Rene Brandel
committed
Updated authorization rules to use the new syntax
1 parent 65a8ce2 commit d4cc9d7

File tree

20 files changed

+92
-92
lines changed

20 files changed

+92
-92
lines changed

src/fragments/gen2/quickstart/build-a-backend.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const schema = a.schema({
2727
.model({
2828
content: a.string()
2929
})
30-
.authorization([a.allow.owner(), a.allow.public().to(['read'])])
30+
.authorization(allow => [allow.owner(), allow.publicApiKey().to(['read'])])
3131
});
3232

3333
export type Schema = ClientSchema<typeof schema>;
@@ -36,7 +36,7 @@ export const data = defineData({
3636
schema,
3737
authorizationModes: {
3838
defaultAuthorizationMode: 'apiKey',
39-
// API Key is used for a.allow.public() rules
39+
// API Key is used for allow.publicApiKey() rules
4040
apiKeyAuthorizationMode: {
4141
expiresInDays: 30
4242
}
@@ -99,7 +99,7 @@ const schema = a.schema({
9999
priority: a.enum(['low', 'medium', 'high'])
100100
// highlight-end
101101
})
102-
.authorization([a.allow.owner(), a.allow.public().to(['read'])]),
102+
.authorization(allow => [allow.owner(), allow.publicApiKey().to(['read'])]),
103103
});
104104

105105
// ...
@@ -114,12 +114,12 @@ The `Todo` data model is defined with authorization rules to allow the person wh
114114
**Note:** These authorization rules can be modified using a chain of methods as defined by default. For example, we could remove the `.to(['read'])` and allow all visitors to perform all actions on data or add permissions for signed-in users or users who belong to user groups such as `Admin`. You can learn more about all options for authorization in the [Customize your auth rules](/gen2/build-a-backend/data/customize-authz/) section of the docs.
115115
</Callout>
116116

117-
<b>Step 2:</b> Remove public access by deleting the `a.allow.public().to(['read'])` authorization rule. Your authorization rule will look like the code below:
117+
<b>Step 2:</b> Remove public access by deleting the `allow.publicApiKey().to(['read'])` authorization rule. Your authorization rule will look like the code below:
118118

119119
```js title="amplify/data/resource.ts"
120120
// ...
121121

122-
.authorization([a.allow.owner()]),
122+
.authorization(allow => [allow.owner()]),
123123

124124
// ...
125125
```

src/pages/gen2/build-a-backend/data/connect-existing-data/index.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ import { schema as rdsSchema } from './schema.rds.ts'
9292

9393
// Add an authorization rule to the schema
9494
// highlight-next-line
95-
rdsSchema.models.Todo.authorization([a.allow.public()])
95+
rdsSchema.models.Todo.authorization(allow => [allow.publicApiKey()])
9696

9797
const schema = a.schema({
9898
Todo: a.model({
@@ -129,7 +129,7 @@ const schema = a.schema({
129129
content: a.string()
130130
isDone: a.boolean()
131131
})
132-
}).authorization([a.allow.public()])
132+
}).authorization(allow => [allow.publicApiKey()])
133133

134134
const combinedSchema = a.schema.combine([
135135
schema,
@@ -166,7 +166,7 @@ rdsSchema.addQueries({
166166
ST_MakePoint(:lat, :long)
167167
) <= :radiusInMeters
168168
`)
169-
.authorization([a.allow.public()])
169+
.authorization(allow => [allow.publicApiKey()])
170170
})
171171
// highlight-end
172172

src/pages/gen2/build-a-backend/data/custom-business-logic/index.mdx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const schema = a.schema({
5353
// return type of the query
5454
.returns(a.ref('EchoResponse'))
5555
// only allow signed-in users to call this API
56-
.authorization([a.allow.private()])
56+
.authorization(allow => [allow.authenticated()])
5757
});
5858

5959
export type Schema = ClientSchema<typeof schema>;
@@ -88,7 +88,7 @@ const schema = a.schema({
8888
// return type of the query
8989
.returns(a.ref('Post'))
9090
// only allow signed-in users to call this API
91-
.authorization([a.allow.private()])
91+
.authorization(allow => [allow.authenticated()])
9292
});
9393

9494
export type Schema = ClientSchema<typeof schema>;
@@ -149,7 +149,7 @@ const schema = a.schema({
149149
.query()
150150
.arguments({ content: a.string() })
151151
.returns(a.ref('EchoResponse'))
152-
.authorization([a.allow.public()])
152+
.authorization(allow => [allow.publicApiKey()])
153153
// 3. set the function has the handler
154154
.handler(a.handler.function(echoHandler))
155155
});
@@ -185,17 +185,17 @@ const schema = a.schema({
185185
Post: a.model({
186186
content: a.string(),
187187
likes: a.integer()
188-
.authorization([a.allow.private().to(['read'])])
188+
.authorization(allow => [allow.authenticated().to(['read'])])
189189
}).authorization([
190-
a.allow.owner(),
191-
a.allow.private().to(['read'])
190+
allow.owner(),
191+
allow.authenticated().to(['read'])
192192
]),
193193

194194
likePost: a
195195
.mutation()
196196
.arguments({ postId: a.id() })
197197
.returns(a.ref('Post'))
198-
.authorization([a.allow.private()])
198+
.authorization(allow => [allow.authenticated()])
199199
.handler(a.handler.custom({
200200
dataSource: a.ref('Post'),
201201
entry: './increment-like.js'
@@ -278,17 +278,17 @@ const schema = a.schema({
278278
Post: a.model({
279279
content: a.string(),
280280
likes: a.integer()
281-
.authorization([a.allow.private().to(['read'])])
281+
.authorization(allow => [allow.authenticated().to(['read'])])
282282
}).authorization([
283-
a.allow.owner(),
284-
a.allow.private().to(['read'])
283+
allow.owner(),
284+
allow.authenticated().to(['read'])
285285
]),
286286

287287
likePost: a
288288
.mutation()
289289
.arguments({ postId: a.id() })
290290
.returns(a.ref('Post'))
291-
.authorization([a.allow.private()])
291+
.authorization(allow => [allow.authenticated()])
292292
.handler(a.handler.custom({
293293
// highlight-next-line
294294
dataSource: "ExternalTableDataSource",

src/pages/gen2/build-a-backend/data/custom-subscription/index.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const schema = a.schema({
4343
})
4444
.returns(a.ref('Message'))
4545
.handler(a.handler.custom({ entry: './publish.js' }))
46-
.authorization([a.allow.public()]),
46+
.authorization(allow => [allow.publicApiKey()]),
4747

4848
// highlight-start
4949
// Subscribe to incoming messages
@@ -55,13 +55,13 @@ const schema = a.schema({
5555
// subscription handler to set custom filters
5656
.handler(a.handler.custom({entry: './receive.js'}))
5757
// authorization rules as to who can subscribe to the data
58-
.authorization([a.allow.public()]),
58+
.authorization(allow => [allow.publicApiKey()]),
5959
// highlight-end
6060

6161
// A data model to manage channels
6262
Channel: a.model({
6363
name: a.string(),
64-
}).authorization([a.allow.public()]),
64+
}).authorization(allow => [allow.publicApiKey()]),
6565
});
6666

6767
export type Schema = ClientSchema<typeof schema>;
@@ -166,14 +166,14 @@ const schema = a.schema({
166166
.handler(a.handler.custom({
167167
entry: "./publish.js"
168168
}))
169-
.authorization([a.allow.private()]),
169+
.authorization(allow => [allow.authenticated()]),
170170
171171
// Subscribe to all events from the "publish" mutation
172172
receive: a.subscription(['publish'])
173173
// highlight-next-line
174174
.arguments({ name: a.string() })
175175
.returns(a.ref('Channel'))
176-
.authorization([a.allow.public()])
176+
.authorization(allow => [allow.publicApiKey()])
177177
});
178178
179179
export type Schema = ClientSchema<typeof schema>;
@@ -193,7 +193,7 @@ import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
193193
const schema = a.schema({
194194
Channel: a.model({
195195
name: a.string(),
196-
}).authorization([a.allow.public()]),
196+
}).authorization(allow => [allow.publicApiKey()]),
197197

198198
Message: a.customType({
199199
content: a.string().required(),
@@ -207,15 +207,15 @@ const schema = a.schema({
207207
})
208208
.returns(a.ref('Message'))
209209
.handler(a.handler.custom({ entry: './publish.js' }))
210-
.authorization([a.allow.public()]),
210+
.authorization(allow => [allow.publicApiKey()]),
211211

212212
receive: a.subscription()
213213
.for(a.ref('publish'))
214214
// highlight-next-line
215215
.arguments({ namePrefix: a.string() })
216216
.returns(a.ref('Message'))
217217
.handler(a.handler.custom({entry: './receive.js'}))
218-
.authorization([a.allow.public()])
218+
.authorization(allow => [allow.publicApiKey()])
219219
});
220220

221221
export type Schema = ClientSchema<typeof schema>;

src/pages/gen2/build-a-backend/data/customize-authz/configure-custom-identity-and-group-claim/index.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const schema = a.schema({
2626
postname: a.string(),
2727
content: a.string(),
2828
})
29-
.authorization([
30-
a.allow.owner().identityClaim('user_id'),
31-
a.allow.specificGroups(['Moderator']).withClaimIn('user_groups'),
29+
.authorization(allow => [
30+
allow.owner().identityClaim('user_id'),
31+
allow.groups(['Moderator']).withClaimIn('user_groups'),
3232
]),
3333
});
3434

src/pages/gen2/build-a-backend/data/customize-authz/custom-data-access-patterns/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const schema = a.schema({
2828
})
2929
// STEP 1
3030
// Indicate which models / fields should use a custom authorization rule
31-
.authorization([a.allow.custom()]),
31+
.authorization(allow => [allow.custom()]),
3232
});
3333

3434
export type Schema = ClientSchema<typeof schema>;

src/pages/gen2/build-a-backend/data/customize-authz/grant-lambda-function-access-to-api/index.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const schema = a
3434
})
3535
})
3636
// highlight-next-line
37-
.authorization([a.allow.resource(functionWithDataAccess)]);
37+
.authorization(allow => [allow.resource(functionWithDataAccess)]);
3838

3939
export type Schema = ClientSchema<typeof schema>;
4040

@@ -43,7 +43,7 @@ export const data = defineData({
4343
});
4444
```
4545

46-
The object returned from `defineFunction` can be passed directly to `a.allow.resource()` in the schema authorization rules. This will grant the function the ability to execute Query, Mutation, and Subscription operations against the GraphQL API. Use the `.to()` method to narrow down access to one or more operations.
46+
The object returned from `defineFunction` can be passed directly to `allow.resource()` in the schema authorization rules. This will grant the function the ability to execute Query, Mutation, and Subscription operations against the GraphQL API. Use the `.to()` method to narrow down access to one or more operations.
4747

4848
```ts
4949
const schema = a
@@ -55,7 +55,7 @@ const schema = a
5555
})
5656
// highlight-start
5757
.authorization([
58-
a.allow.resource(functionWithDataAccess).to(['query', 'listen'])
58+
allow.resource(functionWithDataAccess).to(['query', 'listen'])
5959
]); // allow query and subscription operations but not mutations
6060
// highlight-end
6161
```

src/pages/gen2/build-a-backend/data/customize-authz/index.mdx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ Use the `.authorization()` modifier to configure authorization rules for public,
2323
const schema = a.schema({
2424
Post: a.model({
2525
content: a.string()
26-
}).authorization([
26+
}).authorization(allow => [
2727
// Allow anyone auth'd with an API key to read everyone's posts.
28-
a.allow.public().to(['read']),
28+
allow.publicApiKey().to(['read']),
2929
// Allow signed-in user to create, read, update,
3030
// and delete their __OWN__ posts.
31-
a.allow.owner(),
31+
allow.owner(),
3232
])
3333
})
3434
```
@@ -59,7 +59,7 @@ If there are multiple authorization rules present, they will be logically OR'ed.
5959

6060
To help you get started, you can define an authorization rule on the data schema that will be applied to all data models that **do not** have a model-level authorization rule. Instead of having a global authorization rule for all production environments, we recommend creating specific authorization rules for each model or field.
6161

62-
The global authorization rule below uses `a.allow.public()`. This example allows anyone to create, read, update, and delete and is applied to every data model.
62+
The global authorization rule below uses `allow.publicApiKey()`. This example allows anyone to create, read, update, and delete and is applied to every data model.
6363

6464
```ts
6565
const schema = a.schema({
@@ -73,11 +73,11 @@ const schema = a.schema({
7373
Notes: a.model({
7474
content: a.string()
7575
// [Model-level authorization rule]
76-
}).authorization([a.allow.public().to(['read'])])
76+
}).authorization(allow => [allow.publicApiKey().to(['read'])])
7777

7878
// [Global authorization rule]
79-
}).authorization([
80-
a.allow.public()
79+
}).authorization(allow => [
80+
allow.publicApiKey()
8181
])
8282
```
8383

@@ -93,9 +93,9 @@ const schema = a.schema({
9393
// [Model-level authorization rule]
9494
// All fields (content, createdBy) will be protected by
9595
// this authorization rule
96-
}).authorization([
97-
a.allow.public().to(['read']),
98-
a.allow.owner(),
96+
}).authorization(allow => [
97+
allow.publicApiKey().to(['read']),
98+
allow.owner(),
9999
])
100100
})
101101
```
@@ -117,13 +117,13 @@ const schema = a.schema({
117117
// [Field-level authorization rule]
118118
// This auth rule will be used for the "ssn" field
119119
// All other fields will use the model-level auth rule
120-
ssn: a.string().authorization([a.allow.owner()]),
120+
ssn: a.string().authorization(allow => [allow.owner()]),
121121
})
122122

123123
// [Model-level authorization rule]
124-
.authorization([
125-
a.allow.private().to(["read"]),
126-
a.allow.owner()
124+
.authorization(allow => [
125+
allow.authenticated().to(["read"]),
126+
allow.owner()
127127
]),
128128
});
129129
```
@@ -139,9 +139,9 @@ const schema = a.schema({
139139
Post: a.model({
140140
title: a.string(),
141141
content: a.string()
142-
}).authorization([
143-
a.allow.public("identityPool").to(["read"]),
144-
a.allow.owner()
142+
}).authorization(allow => [
143+
allow.guest().to(["read"]),
144+
allow.owner()
145145
])
146146
})
147147
```

src/pages/gen2/build-a-backend/data/customize-authz/multi-user-data-access/index.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const meta = {
22
title: 'Multi-user data access',
3-
description: "The `multipleOwners` rule grants a set of users access to a record by automatically creating an `owners` field to store the allowed record owners. You can override the default owners field name by specifying `inField` with the desired field name to store the owner information. You can dynamically manage which users can access a record by updating the owner field."
3+
description: "The `ownersDefinedIn` rule grants a set of users access to a record by automatically creating an `owners` field to store the allowed record owners. You can override the default owners field name by specifying `inField` with the desired field name to store the owner information. You can dynamically manage which users can access a record by updating the owner field."
44
};
55

66

@@ -12,19 +12,19 @@ export function getStaticProps(context) {
1212
};
1313
}
1414

15-
The `multipleOwners` rule grants a set of users access to a record by automatically creating an `owners` field to store the allowed record owners. You can override the default owners field name by specifying `inField` with the desired field name to store the owner information. You can dynamically manage which users can access a record by updating the owner field.
15+
The `ownersDefinedIn` rule grants a set of users access to a record by automatically creating an `owners` field to store the allowed record owners. You can override the default owners field name by specifying `inField` with the desired field name to store the owner information. You can dynamically manage which users can access a record by updating the owner field.
1616

1717
## Add multi-user authorization rule
1818

19-
If you want to grant a set of users access to a record, you use the `multipleOwners` rule. This automatically creates a `owner: a.string().array()` field to store the allowed owners.
19+
If you want to grant a set of users access to a record, you use the `ownersDefinedIn` rule. This automatically creates a `owner: a.string().array()` field to store the allowed owners.
2020

2121
```ts title="amplify/data/resource.ts"
2222
const schema = a.schema({
2323
Todo: a
2424
.model({
2525
content: a.string(),
2626
})
27-
.authorization([a.allow.multipleOwners()]),
27+
.authorization(allow => [allow.ownersDefinedIn('owners')]),
2828
});
2929
```
3030

@@ -74,6 +74,6 @@ const schema = a.schema({
7474
content: a.string(),
7575
authors: a.string().array(), // record owner information now stored in "authors" field
7676
})
77-
.authorization([a.allow.multipleOwners().inField('authors')]),
77+
.authorization(allow => [allow.ownersDefinedIn('authors')]),
7878
});
7979
```

0 commit comments

Comments
 (0)