本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
範例 2:具有已驗證許可和 Cedar 的基本 RBAC
此範例使用 Verified Permissions 和 Cedar 來示範基本 RBAC。如前所述,Cedar 的基本建構是實體。開發人員會定義自己的實體,也可以選擇性地在實體之間建立關係。下列範例包含三種類型的實體:Users
、 Roles
和 Problems
。 Students
和 Teachers
可以視為 類型的實體,Role,
且每個User
實體都可以與零或任何 相關聯Roles
。

在 Cedar 中,這些關係是透過將 Role
Student
連結至 User
Bob
做為其父系來表示。此關聯會以邏輯方式將所有學生使用者分組在一個群組中。如需在 Cedar 中分組的詳細資訊,請參閱 Cedar 文件
下列政策會針對連結至類型 Students
之邏輯群組submitProblem,
的所有主體,評估 動作ALLOW
的決策Role
。
permit ( principal in ElearningApp::Role::"Students", action == ElearningApp::Action::"submitProblem", resource );
下列政策會針對連結至類型 Teachers
之邏輯群組的所有委託人answerProblem
,評估 ALLOW
動作submitProblem
或 的決策Role
。
permit ( principal in ElearningApp::Role::"Teachers", action in [ ElearningApp::Action::"submitProblem", ElearningApp::Action::"answerProblem" ], resource );
為了評估具有這些政策的請求,評估引擎需要知道授權請求中參考的委託人是否為適當群組的成員。因此,應用程式必須在授權請求中將相關的群組成員資格資訊傳遞給評估引擎。這是透過 entities
屬性完成的,可讓您為 Cedar 評估引擎提供授權呼叫所涉及之主體和資源的屬性和群組成員資格資料。在下列程式碼中,群組成員資格是透過User::"Bob"
將 定義為具有名為 的父系來表示Role::"Students"
。
{ "policyStoreId": "ELEARNING_POLICYSTOREID", "principal": { "entityType": "ElearningApp::User", "entityId": "Bob" }, "action": { "actionType": "ElearningApp::Action", "actionId": "answerProblem" }, "resource": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "entities": { "entityList": [ { "identifier": { "entityType": "ElearningApp::User", "entityId": "Bob" }, "attributes": {}, "parents": [ { "entityType": "ElearningApp::Role", "entityId": "Students" } ] }, { "identifier": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "attributes": {}, "parents": [] } ] } }
在此範例中,Bob 是提出answerProblem
請求的登入使用者。因此,Bob 是 委託人,而實體的類型為 User
。Bob 嘗試執行的動作是 answerProblem
。為了評估 Bob 是否可以執行answerProblem
動作,您需要提供將實體User
與 值連結的實體結構,Bob
並將父實體列為 來指派其群組成員資格Role::"Students"
。由於使用者群組中的實體只Role::"Students"
允許執行動作 submitProblem
,因此此授權請求會評估為 DENY
。
另一方面,如果 User
和 的類型Alice
是群組Role::"Teachers"
嘗試執行answerProblem
動作的一部分,則授權請求會評估為 ALLOW
,因為政策規定群組中的主體Role::"Teachers"
可以answerProblem
對所有資源執行動作。下列程式碼顯示評估為 的此類授權請求ALLOW
。
{ "policyStoreId": "ELEARNING_POLICYSTOREID", "principal": { "entityType": "ElearningApp::User", "entityId": "Alice" }, "action": { "actionType": "ElearningApp::Action", "actionId": "answerProblem" }, "resource": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "entities": { "entityList": [ { "identifier": { "entityType": "ElearningApp::User", "entityId": "Alice" }, "attributes": {}, "parents": [ { "entityType": "ElearningApp::Role", "entityId": "Teachers" } ] }, { "identifier": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "attributes": {}, "parents": [] } ] } }