> ## 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 で Access フック を使用する方法を説明します。

# Delegated Administration: Access Hook

Filter フック はフィルタリング ロジックを適用するだけなので、現在のユーザー (または管理者として操作しているユーザー) が特定のユーザーにアクセスできるかどうかを判断するには、追加のロジック層が必要です。

Filter フック の詳細については、[Delegated Administration: Filter Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-filter-hook) を参照してください。

**Access フック** を使用すると、現在のユーザーに特定のユーザーの読み取り、削除、ブロック、ブロック解除、更新を許可するかどうかを判断できます。

<div id="hook-contract">
  ## フック の仕様
</div>

* **ctx**: コンテキストオブジェクト。

  * **payload**: ペイロードオブジェクト。

    * **action**: 現在実行中のアクション (例: **delete:user**) 。
    * **user**: アクションの実行対象となるユーザー。
* **callback(error)**: アクセスが拒否された場合にエラーを返せるコールバック。

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

Kelly は財務部門を管理しており、自分の部門内のユーザーのみを閲覧できるようにする必要があります。

```javascript lines theme={null}
function(ctx, callback) {
  if (ctx.payload.action === 'delete:user') {
    return callback(new Error('You are not allowed to delete users.'));
  }

  // 現在のユーザーのメタデータから部署を取得する。
  var department = ctx.request.user.app_metadata && ctx.request.user.app_metadata.department;
  if (!department || !department.length) {
    return callback(new Error('The current user is not part of any department.'));
  }

  // IT部門はすべてのユーザーにアクセスできる。
  if (department === 'IT') {
    return callback();
  }

  ctx.log('Verifying access:', ctx.payload.user.app_metadata.department, department);

  if (!ctx.payload.user.app_metadata.department || ctx.payload.user.app_metadata.department !== department) {
    return callback(new Error('You can only access users within your own department.'));
  }

  return callback();
}
```

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

このフックが設定されていない場合、現在のユーザーはすべてのユーザーにアクセスできます。

フック では、次のアクション名をサポートしています (**ctx.payload.action** の値として設定します) 。

* read:user
* delete:user
* reset:password
* change:password
* change:username
* change:email
* read:devices
* read:logs
* remove:multifactor-provider
* block:user
* unblock:user
* send:verification-email

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

* [Delegated Administration: Filter Hook](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-filter-hook)
* [Delegated Administration: メンバーシップ クエリ フック](/docs/ja-jp/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-memberships-query-hook)
* [Delegated Administration: 設定クエリ フック](/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)
