

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例 2：具有已驗證許可和 Cedar 的基本 RBAC
<a name="avp-basic-rbac-examples"></a>

此範例使用 Verified Permissions 和 Cedar 來示範基本 RBAC。如前所述，Cedar 的基本建構是實體。開發人員會定義自己的實體，也可以選擇性地在實體之間建立關係。下列範例包含三種類型的實體：`Users`、 `Roles`和 `Problems`。 `Students` 和 `Teachers`可視為 類型的實體，`Role,`且每個`User`實體都可以與零或任何 相關聯`Roles`。

![使用 Amazon Verified Permissions 和 Cedar 實作 PDP 的基本 RBAC 實作範例](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/saas-multitenant-api-access-authorization/images/avp-example-2.png)


在 Cedar 中，這些關係是透過將 `Role` `Student` 連結至 `User``Bob`做為其父系來表示。此關聯會以邏輯方式將所有學生使用者分組在一個群組中。如需在 Cedar 中分組的詳細資訊，請參閱 [Cedar 文件](https://docs.cedarpolicy.com/overview/terminology.html#term-group)。

下列政策會針對連結至類型 `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": []
        }
      ]
  }
}
```