> ## Documentation Index
> Fetch the complete documentation index at: https://translations.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Delegated Administration Extension でメンバーシップ クエリ Hook を使用する方法を説明します。

# Delegated Administration: メンバーシップ クエリ Hook

新しいユーザーを作成する際、ユーザー インターフェースには、ユーザーに割り当てるメンバーシップを選択できるドロップダウンが表示されます。これらのメンバーシップは、**メンバーシップ クエリ Hook** を使用して定義します。

<div id="hook-contract">
  ## Hookの仕様
</div>

* **ctx**: コンテキストオブジェクト。
* **callback(error, \{ createMemberships: true/false, memberships: })**: エラーと、メンバーシップ設定を含むオブジェクトを返すことができるコールバック。

<div id="sample-use">
  ## 使用例
</div>

IT 部門のユーザーは、他の部門のユーザーを作成できる必要があります。他の部門のユーザーは、自分の部門のユーザーのみを作成できるようにする必要があります。

```javascript lines theme={null}
function(ctx, callback) {
  var currentDepartment = ctx.payload.user.app_metadata && ctx.payload.user.app_metadata.department;
  if (!currentDepartment || !currentDepartment.length) {
    return callback(null, [ ]);
  }

  if (currentDepartment === 'IT') {
    return callback(null, [ 'IT', 'HR', 'Finance', 'Marketing' ]);
  }

  return callback(null, [ ctx.payload.user.app_metadata.department ]);
}
```

<div id="notes">
  ## 注記
</div>

このクエリは UI でのみ使用できるため、ユーザーの特定の部門への割り当てに関するルールを適用する必要がある場合は、**Write Hook** を使用してメンバーシップを割り当てる必要があります。

選択可能なメンバーシップグループが 1 つしかない場合、UI にメンバーシップ フィールドは表示されません。

**createMemberships** を true に設定すると、エンドユーザーが **memberships** フィールドに任意の値を入力できるようになります。

```javascript lines theme={null}
function(ctx, callback) {
  var currentDepartment = ctx.payload.user.app_metadata.department;
  if (!currentDepartment || !currentDepartment.length) {
    return callback(null, [ ]);
  }

  return callback(null, {
    createMemberships: ctx.payload.user.app_metadata.department === 'IT' ? true : false,
    memberships: [ ctx.payload.user.app_metadata.department ]
  });
}
```

<div id="learn-more">
  ## 詳しくはこちら
</div>

* [Delegated Administration: Access Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-access-hook)
* [Delegated Administration: Filter Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-filter-hook)
* [Delegated Administration: 設定クエリ Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-settings-query-hook)
* [Delegated Administration: Write Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-write-hook)
