Looking for how to use this in your app?See Frontend Libraries →
複数ユーザーのデータアクセス
ownersDefinedIn ルールは、owners フィールドを自動的に作成して許可されたレコード所有者を保存することで、ユーザーのセットにレコードへのアクセスを付与します。inField を指定することで、デフォルトの owners フィールド名をオーバーライドして、所有者情報を保存する目的のフィールド名を指定できます。所有者フィールドを更新することで、レコードにアクセスできるユーザーを動的に管理できます。
マルチユーザー認可ルールの追加
ユーザーのセットにレコードへのアクセスを付与したい場合は、ownersDefinedIn ルールを使用します。これにより、許可されたオーナーを保存するための owners: a.string().array() フィールドが自動的に作成されます。
amplify/data/resource.ts
const schema = a.schema({ Todo: a .model({ content: a.string(), owners: a.string().array(), }) .authorization(allow => [allow.ownersDefinedIn('owners')]),});アプリケーションでは、amazonCognitoUserPools 認証モードでモデルに対して CRUD 操作を実行できます。
do { let todo = Todo(content: "My new todo") let createdTodo = try await Amplify.API.mutate(request: .create( todo, authMode: .amazonCognitoUserPools)).get()} catch { print("Failed to create todo", error)}別のユーザーをオーナーとして追加
do { createdTodo.owners?.append(otherUserId) let updatedTodo = try await Amplify.API.mutate(request: .update( createdTodo, authMode: .amazonCognitoUserPools)).get()} catch { print("Failed to update todo", error)}オーナーのリストにオーバーライド
inField をオーナーのリストにオーバーライドできます。レコードへのアクセスを持つユーザーの動的なセットが必要な場合は、これを使用します。以下の例では、authors リストはレコード作成時にレコードの作成者によって入力されます。作成者はその後、authors フィールドを追加のユーザーで更新できます。authors フィールドにリストされているユーザーは、レコードにアクセスできます。
const schema = a.schema({ Todo: a .model({ content: a.string(), authors: a.string().array(), // record owner information now stored in "authors" field }) .authorization(allow => [allow.ownersDefinedIn('authors')]),});