Authing 知识库
🛠 开发资源🥂 集成案例🔭 常见问题🖥 控制台
1.0.0
1.0.0
  • 欢迎
  • Authing 概述
  • 快速开始
    • 第一个应用
    • 基础概念
    • 实现单点登录
    • 控制台概览
    • 部署模型
  • 进阶指南
    • 理解认证和授权
      • 用户名 - 密码认证
      • 邮箱 - 密码认证
      • 手机 - 验证码认证
      • JWT Token 释义及使用
      • 验证 JWT Token
    • 配置用户权限
    • 最佳开发实践
    • 接入小程序扫码登录
      • 接入私有化小程序
    • 接入社会化登录
    • 接入 OAuth 2.0
      • 创建 OAuth 应用
      • 使用 OAuth 授权
    • 接入 OpenID Connect
      • 创建 OIDC 应用
      • 使用 OIDC 授权
      • 理解 OIDC 流程
      • OIDC 常见问题
    • 配置 LDAP 服务
    • 使用 Authing 的 LDAP 用户目录
    • 接入 SAML
      • 创建 SAML Identity Provider 应用
      • 创建 SAML Service Provider 应用
      • 理解 SAML 流程
      • 同时使用 Authing 作为 SP 和 IdP
      • 使用 SAML Identity Provider
        • 在阿里云访问管理中使用
        • 在腾讯云访问管理中使用
        • 在 Auth0 中使用
      • 使用 SAML Service Provider
        • 与 Auth0 SAML IdP 对接
        • 与 SSOCircle SAML IdP 对接
    • 使用 Webhook
    • 错误代码
  • 开发资源
    • 开发资源
    • API(GraphQL)
    • Guard for Web
    • Guard VS 自定义 UI
    • SDK for Web
      • 读取/修改用户权限
      • 绑定社会化登录
      • 管理 MFA 口令
      • 自定义请求链接
    • SDK for 微信小程序
    • SDK for Java
    • SDK for Objective-C
    • SDK for Python
    • SDK for Go
    • SDK for PHP
    • 函数计算(FaaS)
  • 通信
    • 邮件
    • SMS
  • MFA
    • 配置 MFA 安全口令
    • 接入 MFA
  • 安全
    • 配置 Web 安全域
    • 配置用户池密码强度
    • 配置密码加密函数
  • 其他
    • 常见问题
    • 集成案例
      • 使用 Authing 补充 AWS 中国区 Cognito User Pool
    • 社交互联数据
      • Authing 与社交互联数据
    • 为 Authing 贡献 SDK
      • 了解 Authing 的模块
Powered by GitBook
On this page
  • 获取用户权限和角色
  • 获取应用下所有角色
  • 创建用户角色
  • 修改角色权限
  • 指派用户到某角色
  • 将用户从某角色中移除

Was this helpful?

  1. 开发资源
  2. SDK for Web

读取/修改用户权限

PreviousSDK for WebNext绑定社会化登录

Last updated 5 years ago

Was this helpful?

你可以在控制台中创建角色、为角色配置权限和为角色指派用户(配置方式请参考:),然后使用本页的 API 根据用户 ID 在程序中读取或修改用户角色和权限。

获取用户权限和角色

Authing.queryPermissions(userId)

  • 参数:

    • {String} userId

  • 使用方法:

    • Authing.queryPermissions('USER_ID');
      (async function() {
        const authing = await new Authing({
          clientId: 'your_client_id',
          timestamp: Math.round(new Date() / 1000),
          nonce: Math.ceil(Math.random() * Math.pow(10, 6)),
        });
        
        const groupList = await authing.queryPermissions('USER_ID')
          .catch((error) => { ... });;
      })();
  • 返回数据:

    • {
        "totalCount": 2,
        "list": [
          {
            "group": {
              "name": "管理员",
              "permissions": "{\"routes\": \"/api/v1\"}"
            }
          },
          {
            "group": {
              "name": "读者",
              "permissions": ""
            }
          }
        ]
      }

获取应用下所有角色

Authing.queryRoles(options)

  • 参数:

    • {Object} options

      • page: 第几页,选填,默认为 1

      • count: 总数,选填,默认为 10

  • 使用方法:

    • (async function() {
        const authing = await new Authing({
          clientId: 'your_client_id',
          timestamp: Math.round(new Date() / 1000),
          nonce: Math.ceil(Math.random() * Math.pow(10, 6)),
        });
        
        const rolesList = await authing.queryPermissions({
          page: 1,
          count: 10
        })
          .catch((error) => { ... });;
      })();
  • 返回数据:

    • {
        "totalCount": 1,
        "list": [
          {
            "_id": "5ca5c3a88a61c7304fb1299a",
            "name": "管理员",
            "client": "5c9c659cb9440b05cb2570e6",
            "descriptions": null,
            "createdAt": "Thu Apr 04 2019 16:43:20 GMT+0800 (CST)",
            "permissions": "{\"routes\": \"/api/v1\"}"
          }
        ]
      }

创建用户角色

Authing.createRole(options)

  • 参数:

    • {Object} options

      • name: 角色名称,必填

      • descriptions: 角色描述,必填

  • 使用方法:

    • (async function() {
        const authing = await new Authing({
          clientId: 'your_client_id',
          timestamp: Math.round(new Date() / 1000),
          nonce: Math.ceil(Math.random() * Math.pow(10, 6)),
        });
        
        const rolesList = await authing.createRole({
          name: '测试角色',
          descriptions: '测试角色的描述'
        })
          .catch((error) => { ... });;
      })();
  • 返回数据:

    • {
        "_id": "5ca5c5dd8a61c7ffbfb129a9",
        "name": "测试角色",
        "client": "5c9c659cb9440b05cb2570e6",
        "descriptions": "测试角色的描述"
      }

修改角色权限

Authing.updateRolePermissions(options)

  • 参数:

    • {Object} options

      • name: 角色名称,必填

      • roleId: 角色 ID,必填

      • permissions: 角色权限,必填。输入自定义的权限字符串,可以是 JSON 或数组;之后可以通过 API 获取此处设置的权限既而实现自己的业务逻辑。

  • 使用方法:

    • (async function() {
        const authing = await new Authing({
          clientId: 'your_client_id',
          timestamp: Math.round(new Date() / 1000),
          nonce: Math.ceil(Math.random() * Math.pow(10, 6)),
        });
        
        const rolesList = await authing.updateRolePermissions({
          name: '测试角色',
          roleId: '5ca5c5dd8a61c7ffbfb129a9',
          permissions: 'route:all'
        })
          .catch((error) => { ... });;
      })();
  • 返回数据:

    • {
        "_id": "5ca5c5dd8a61c7ffbfb129a9",
        "name": "测试角色",
        "client": "5c9c659cb9440b05cb2570e6",
        "descriptions": "测试角色的描述",
        "permissions": "route:all"
      }

指派用户到某角色

Authing.assignUserToRole(userId)

  • 参数:

    • {String} userId

  • 使用方法:

    • (async function() {
        const authing = await new Authing({
          clientId: 'your_client_id',
          timestamp: Math.round(new Date() / 1000),
          nonce: Math.ceil(Math.random() * Math.pow(10, 6)),
        });
        
        const rolesList = await authing.assignUserToRole('USER_ID')
          .catch((error) => { ... });;
      })();
  • 返回数据:

    • {
        "totalCount":3,
        "list":[
          {
            "_id":"5d21bb48ec626c076d82c31a",
            "client":{
              "_id":null,
            },
            "user":{
              "_id":null,
            },
            "createdAt":"Sun Jul 07 2019 17:28:40 GMT+0800 (CST)",
          }
        ],
      }

将用户从某角色中移除

Authing.removeUserFromRole(options)

  • 参数:

    • {Object} options

      • roleId: 角色 ID,必填

      • user: 要指派的用户 ID,必填

  • 使用方法:

    • (async function() {
        const authing = await new Authing({
          clientId: 'your_client_id',
          timestamp: Math.round(new Date() / 1000),
          nonce: Math.ceil(Math.random() * Math.pow(10, 6)),
        });
        
        const rolesList = await authing.removeUserFromRole({
          user: '5ca332d265520f3d751b0f63',
          roleId: '5ca5c5dd8a61c7ffbfb129a9',
        })
          .catch((error) => { ... });;
      })();
  • 返回数据:

    • {
        "_id": null,
        "client": null,
        "group": null,
        "user": null
      }
配置用户角色和权限