RBAC | 使用 Authing 實現組織機構管理

藉助 Authing,能夠快速實現基於角色的訪問控制(RBAC)。簡單來講,RBAC 指的是經過用戶的角色(Role)賦予其相關權限,這實現了細粒度的訪問控制,並提供了一個相比直接授予單個用戶權限,更簡單、可控的管理方式。node

而在現實生活中,組、角色每每是分層嵌套的,呈樹狀結構,最多見的就是組織機構,如公司、學校等等。這篇文章,咱們會設想一家互聯網公司 —— 「非凡科技有限公司」,看看他們是如何使用 Authing 快速完成組織機構建模的。git

1、非凡科技有限公司背景介紹

該公司約 300 名員工,其公司架構大體以下:github

  • 一級部門有產品部、研發部、運營部、綜合管理部
  • 一級部門下面又有二級部門,如產品部中包含產品經理和設計等。

這是一個典型的樹狀數據結構,有且僅有一個根節點,以及多個分層的節點。通常而言,根節點就是一家公司、一個組織,其餘的每一個節點都對應一個部門。segmentfault

這裏有一點須要注意:在 Authing 中,這樣的節點對應的是一個 Group,好比「非凡科技有限公司」是一個 Group,「產品部」也是一個 Group。咱們要作的,是要把這些 Group 放到他在樹上所屬的位置。安全

一個 Group 能夠擁有多個角色(Role),一個角色包含一個或多個權限(Permission);而一個 Group 由若干用戶(User)組成,某個 Group 中的用戶會繼承該 Group 下的全部角色,從而具有相關權限。數據結構

瞭解如何管理 Group 中的 User、如何管理 Group 中的 Role、如何配置 Role 的 Permission,請見文檔:角色權限管理架構

瞭解如何查詢單個用戶的 Group, Role, Permission 列表,請見文檔:查詢用戶權限app

如何使用 Authing 完成組織機構管理運維

一、梳理組織機構節點

首先,咱們須要列舉出該組織結構擁有的全部節點:測試

  • 非凡科技有限公司
  • 產品部
  • 研發部
  • 運營部
  • 綜合管理部
  • 產品經理
  • 設計
  • 開發
  • 測試
  • 運維
  • 用戶運營
  • 渠道運營
  • HR
  • 財務
  • 行政

這些節點在 Authing 中都是一個 Group,因此須要先建立這些 Group:

const 非凡科技有限公司 = await createGroup("非凡科技有限公司")
const 產品部 = await createGroup('產品部')
const 產品經理 = await createGroup('產品經理')
const 設計 = await createGroup('設計')
const 研發部 = await createGroup('研發部')
const 開發 = await createGroup('開發')
const 測試 = await createGroup('測試')
const 運維 = await createGroup('運維')
const 運營部 = await createGroup('運營部')
const 用戶運營 = await createGroup('用戶運營')
const 渠道運營 = await createGroup('渠道運營')
const 綜合管理部 = await createGroup('綜合管理部')
const HR = await createGroup('HR')
const 財務 = await createGroup('財務')
const 行政 = await createGroup('行政')

二、建立組織機構

接着建立組織機構(一組樹狀的 Group),這須要指定根節點對應的 Group:

let org = await authing.org.createOrg({
 rootGroupId: 非凡科技有限公司._id
})

三、加入產品部及其子部門節點

接着插入產品部的各級節點:

這裏須要指定如下參數:

  • 組織機構 ID
  • 該節點對應的 Group ID
  • 該節點的父節點的 Group ID

如加入產品部節點時,groupId 爲 Group<產品部> 的 ID,parentGroupId 爲 Group<非凡科技有限公司> 的 ID。

await authing.org.addNode({
 orgId: org._id,
 groupId: 產品部._id,
 parentGroupId: 非凡科技有限公司._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 產品經理._id,
 parentGroupId: 產品部._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 設計._id,
 parentGroupId: 產品部._id
})

四、以此類推,加入全部節點

研發部:

// 添加研發部
await authing.org.addNode({
 orgId: org._id,
 groupId: 研發部._id,
 parentGroupId: 非凡科技有限公司._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 開發._id,
 parentGroupId: 研發部._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 測試._id,
 parentGroupId: 研發部._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 運維._id,
 parentGroupId: 研發部._id
})

運營部:

await authing.org.addNode({
 orgId: org._id,
 groupId: 運營部._id,
 parentGroupId: 非凡科技有限公司._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 用戶運營._id,
 parentGroupId: 運營部._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 渠道運營._id,
 parentGroupId: 運營部._id
})

綜合管理部:

// 添加綜合管理部
await authing.org.addNode({
 orgId: org._id,
 groupId: 綜合管理部._id,
 parentGroupId: 非凡科技有限公司._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: HR._id,
 parentGroupId: 綜合管理部._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 財務._id,
 parentGroupId: 綜合管理部._id
})
await authing.org.addNode({
 orgId: org._id,
 groupId: 行政._id,
 parentGroupId: 綜合管理部._id
})

五、查看最新組織機構結構

到如今,咱們的非凡科技有限公司組織機構見建模完成了,是時候獲取其最新的樹狀結構了:

const { tree } = await authing.org.findById(org._id)

img

2、如何向組織機構中添加用戶和配置權限

前面說過,Authing 中一個組織結構節點對應一個 Group,對此咱們提供了完整的 SDK。

一、爲組織機構節點配置權限

相關 SDK 見:角色權限管理

非凡科技有限公司的全部員工,都具有開具發票、使用公司郵箱的權限。與此對應,在此咱們建立兩個角色:Invoice Submitter 和 Corp Email User。

其中 Invoice Submitter 具有如下權限:

  • invoice:login
  • invoice:create
  • invoice:query
  • invoice:list
  • invoice:delete

Corp Email User 具有如下權限:

  • corp-email:login
  • corp-email:send
  • corp-email:receive
  • corp-email:list
  • corp-email:detail

這能夠經過如下代碼完成:

const InvoiceSubmitter = await createRole('Invoice Submitter')
let permissions = await createPermissionBatch(['invoice:login', 'invoice:create', 'invoice:query', 'invoice:list', 'invoice:delete'])
await authing.authz.addPermissionToRoleBatch({
 roleId: InvoiceSubmitter._id,
 permissionIdList: permissions.map(x => x._id)
})
const CorpEmailUser = await createRole('Corp Email Use')
permissions = await createPermissionBatch(['corp-email:login', 'corp-email:send', 'corp-email:receive', 'corp-email:list', 'corp-email:detail'])
await authing.authz.addPermissionToRoleBatch({
 roleId: CorpEmailUser._id,
 permissionIdList: permissions.map(x => x._id)
})

接着讓 Group 非凡科技有限公司具有 Invoice Submitter 和 Corp Email User 兩個角色:

await authing.authz.addRoleToGroup({
 roleId: InvoiceSubmitter._id,
 groupId: 非凡科技有限公司._id
})
await authing.authz.addRoleToGroup({
 roleId: InvoiceSubmitter._id,
 groupId: 非凡科技有限公司._id
})

如今,非凡科技有限公司這個節點將會具有 Invoice Submitter 和 Corp Email User 兩個角色

二、向組織機構節點添加用戶

相關 API 見:角色權限管理

某個 Group 內的用戶會繼承該 Group 內全部角色的權限(若是有重疊,將會取並集)。

下面咱們往用戶池中註冊新用戶,而後將其加入非凡科技有限公司 Group 中:

const user = await createUser()
await authing.authz.addUserToGroup({
 groupId: 非凡科技有限公司._id,
 userId: user._id
})

三、查詢用戶具有的權限

相關 API 見:查詢用戶權限

const { rawList: permissionList } = await authing.userPermissionList(user._id)‌

permissionList 以下:可見,此用戶已經繼承了 Invoice Submitter 和 Corp Email User 兩個角色的全部權限:

[
  'invoice:login',
  'invoice:create',
  'invoice:query',
  'invoice:list',
  'invoice:delete',
  'corp-email:login',
  'corp-email:send',
  'corp-email:receive',
  'corp-email:list',
  'corp-email:detail',
 ]

開發者拿到用戶權限列表以後,能夠在業務代碼層判斷用戶是否具有某一特定權限,如:

if "corp-email:login" not in user.permissionList:
  return "Permission Denied"

四、使用 RBAC 的優點

  • 系統性、可重複性的權限指派
  • 更方便的用戶權限審計,快速定位問題
  • 快速地添加、修改角色,甚至能夠調用 API 實現
  • 減小授予用戶權限時發生錯誤的可能性
  • 引入第三方用戶/新用戶/未登陸用戶時,賦予他們預先配置好的角色,好比 guest 分組

    更多關於 RBAC 的介紹請參考實現基於角色的訪問控制(RBAC)

3、總結

本文咱們以「非凡科技有限公司」爲例,介紹瞭如何將一棵組織機構樹轉換成一組嵌套、有層次的 Group。經過 Authing 提供的分組角色權限管理 API,能夠爲 Group 配置角色、指派成員,使得該 Group 中的用戶繼承所需的權限,從而完成組織機構建模與權限控制。

相關閱讀

  1. Authing 的故事:我爲何開發 Authing?
  2. 如何在遠程辦公中保持高效的研發效率?
  3. 一份普通人能理解的關於 Authing 的介紹
  4. Authing 是什麼以及爲何須要 Authing?
  5. 爲何身份認證值得上雲?
  6. Authing @ 2019 總結
  7. Authing 開發資源最全合集

重磅:Authing 將於2020 Q1 開源,歡迎 Star 關注 https://github.com/Authing/authing

什麼是 Authing?

Authing 提供專業的身份認證和受權服務。
咱們爲開發者和企業提供用以保證應用程序安全所需的認證模塊,這讓開發人員無需成爲安全專家。
你能夠將任意平臺的應用接入到 Authing(不管是新開發的應用仍是老應用均可以),同時你還能夠自定義應用程序的登陸方式(如:郵箱/密碼、短信/驗證碼、掃碼登陸等)。
你能夠根據你使用的技術,來選擇咱們的 SDK 或調用相關 API 來接入你的應用。當用戶發起受權請求時,Authing 會幫助你認證他們的身份和返回必要的用戶信息到你的應用中。

<div align=center>Authing 在應用交互中的位置</div>

相關文章
相關標籤/搜索