# 21. Authority 权限控制
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/03.%20%E7%BB%84%E4%BB%B6%E5%BA%93%E6%96%87%E6%A1%A3/09.%20%E5%85%B6%E4%BB%96%E7%BB%84%E4%BB%B6/23.%20Authority%20%E6%9D%83%E9%99%90%E6%8E%A7%E5%88%B6

Authority 权限控制组件使用指南


# Authority 权限控制组件

> [!info] 用于控制页面元素显示/隐藏的权限组件，根据用户权限决定是否渲染子节点。

## 基本用法

```vue
<template>
  <!-- 有权限时显示 -->
  <Authority value="user:add">
    <a-button type="primary">新增用户</a-button>
  </Authority>

  <!-- 无权限时不显示 -->
  <Authority value="user:delete">
    <a-button danger>删除用户</a-button>
  </Authority>
</template>

<script setup lang="ts">
import { Authority } from '@/components/Authority';
</script>
```

## Props 属性

| 属性 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| value | `RoleEnum \| RoleEnum[] \| string \| string[]` | `''` | 权限标识或角色标识 |

> [!note] 当权限模式为角色模式时，传入 `RoleEnum` 枚举值；当为后台模式时，传入权限代码字符串。

### 权限判断逻辑

权限检查由 `usePermission` hook 的 `hasPermission` 方法处理：
- 传入字符串：检查是否拥有该权限
- 传入数组：**OR 逻辑**，满足任一权限即返回 `true`

## 单权限控制

```vue
<template>
  <Authority value="system:user:add">
    <a-button type="primary">
      <Icon icon="ant-design:plus-outlined" />
      新增用户
    </a-button>
  </Authority>
</template>

<script setup lang="ts">
import { Authority } from '@/components/Authority';
import { Icon } from '@/components/Icon';
</script>
```

## 多权限控制

### OR 逻辑（默认）

传入数组时，满足**任一**权限即显示：

```vue
<template>
  <!-- 拥有 system:user:view 或 system:user:edit 任一权限即显示 -->
  <Authority :value="['system:user:view', 'system:user:edit']">
    <a-button>查看或编辑</a-button>
  </Authority>
</template>
```

### AND 逻辑（嵌套实现）

需要同时拥有多个权限时，使用嵌套组件：

```vue
<template>
  <!-- 必须同时拥有两个权限才显示 -->
  <Authority value="system:user:edit">
    <Authority value="system:user:check">
      <a-button>编辑（需要同时拥有编辑和检查权限）</a-button>
    </Authority>
  </Authority>
</template>
```

## 在表格中使用

```vue
<template>
  <BasicTable @register="registerTable">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'action'">
        <Authority value="system:user:edit">
          <a-button type="link" @click="handleEdit(record)">编辑</a-button>
        </Authority>

        <Authority value="system:user:delete">
          <a-divider type="vertical" />
          <a-button type="link" danger @click="handleDelete(record)">删除</a-button>
        </Authority>
      </template>
    </template>
  </BasicTable>
</template>

<script setup lang="ts">
import { Authority } from '@/components/Authority';
import { BasicTable, useTable } from '@/components/Table';
// ...
</script>
```

## 工具栏权限控制

```vue
<template>
  <BasicTable @register="registerTable">
    <template #toolbar>
      <Authority value="system:user:add">
        <a-button type="primary" @click="handleAdd">
          <Icon icon="ant-design:plus-outlined" />
          新增
        </a-button>
      </Authority>

      <Authority value="system:user:export">
        <a-button @click="handleExport">
          <Icon icon="ant-design:download-outlined" />
          导出
        </a-button>
      </Authority>
    </template>
  </BasicTable>
</template>

<script setup lang="ts">
import { Authority } from '@/components/Authority';
import { Icon } from '@/components/Icon';
// ...
</script>
```

## 与 v-auth 指令对比

```vue
<!-- 方式一：使用 Authority 组件 -->
<Authority value="user:add">
  <a-button>新增</a-button>
</Authority>

<!-- 方式二：使用 v-auth 指令 -->
<a-button v-auth="'user:add'">新增</a-button>

<!-- 方式三：v-auth 多权限 -->
<a-button v-auth="['user:add', 'user:edit']">编辑</a-button>
```

> [!tip] 推荐使用 Authority 组件，更符合 React/Vue 通用写法。

## 表单中的权限控制

```vue
<template>
  <BasicForm @register="registerForm">
    <template #userGroup="{ model, field }">
      <Authority value="system:user:assign-group">
        <JSelectDept
          v-model:value="model[field]"
          placeholder="选择部门"
        />
      </Authority>
    </template>
  </BasicForm>
</template>

<script setup lang="ts">
import { Authority } from '@/components/Authority';
import { JSelectDept } from '@/components/Form';
// ...
</script>
```

## 嵌套使用

```vue
<template>
  <!-- 外层：同时满足 view 和 edit 权限 -->
  <Authority value="system:user:view">
    <Authority value="system:user:edit">
      <a-button>编辑用户</a-button>
    </Authority>

    <!-- 内层：需要额外的 delete 权限 -->
    <Authority value="system:user:delete">
      <a-divider type="vertical" />
      <a-button danger>删除用户</a-button>
    </Authority>
  </Authority>
</template>

<script setup lang="ts">
import { Authority } from '@/components/Authority';
</script>
```

## 权限标识规范

```
格式：模块:功能:操作

示例：
- system:user:add       - 系统模块-用户管理-新增
- system:user:edit      - 系统模块-用户管理-编辑
- system:user:delete    - 系统模块-用户管理-删除
- system:user:view      - 系统模块-用户管理-查看
- system:user:export    - 系统模块-用户管理-导出
- system:user:import    - 系统模块-用户管理-导入
```

## 动态权限

```vue
<template>
  <Authority :value="requiredPermission">
    <a-button>需要权限的按钮</a-button>
  </Authority>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { Authority } from '@/components/Authority';

const isAdmin = ref(true);

const requiredPermission = computed(() =>
  isAdmin.value ? 'system:user:admin' : 'system:user:normal'
);
</script>
```

## 完整示例：用户管理页面

```vue
<template>
  <div class="user-management">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <Authority value="system:user:add">
          <a-button type="primary" @click="handleAdd">
            <Icon icon="ant-design:plus-outlined" />
            新增用户
          </a-button>
        </Authority>

        <Authority value="system:user:import">
          <a-button @click="handleImport">
            <Icon icon="ant-design:upload-outlined" />
            导入
          </a-button>
        </Authority>

        <Authority value="system:user:export">
          <a-button @click="handleExport">
            <Icon icon="ant-design:download-outlined" />
            导出
          </a-button>
        </Authority>
      </template>

      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'action'">
          <Authority value="system:user:edit">
            <a-button type="link" @click="handleEdit(record)">
              编辑
            </a-button>
          </Authority>

          <Authority :value="['system:user:reset']">
            <a-divider type="vertical" />
            <a-button type="link" @click="handleResetPwd(record)">
              重置密码
            </a-button>
          </Authority>

          <Authority value="system:user:delete">
            <a-divider type="vertical" />
            <a-button type="link" danger @click="handleDelete(record)">
              删除
            </a-button>
          </Authority>
        </template>
      </template>
    </BasicTable>
  </div>
</template>

<script setup lang="ts">
import { Authority } from '@/components/Authority';
import { Icon } from '@/components/Icon';
import { BasicTable, useTable } from '@/components/Table';

const [registerTable] = useTable({
  columns: [
    { title: '用户名', dataIndex: 'username' },
    { title: '姓名', dataIndex: 'realname' },
    { title: '操作', dataIndex: 'action' },
  ],
  api: getUserList,
});

function handleAdd() {
  console.log('新增用户');
}

function handleEdit(record) {
  console.log('编辑用户', record);
}

function handleResetPwd(record) {
  console.log('重置密码', record);
}

function handleDelete(record) {
  console.log('删除用户', record);
}

async function getUserList() {
  return [];
}
</script>
```

## 相关文件

- 组件路径：`@/components/Authority/src/Authority.vue`
- 指令：`@/directives/auth/auth.ts`
- 权限 Store：`@/store/modules/permission.ts`

## 相关文档

- [[05. 权限控制/01. 前端权限控制]] - 前端权限控制详解
