實作:新增保護引擎
在檔案 SDK 中,類別 mip::ProtectionProfile
是所有 SDK 作業的根類別。 建立配置檔之後,我們現在可以將引擎新增至配置檔。
下列範例示範如何針對單一已驗證的使用者使用單一引擎。
實作:建立保護引擎設定
與設定檔類似,引擎也需要設定物件 mip::ProtectionEngine::Settings
此物件會儲存唯一的引擎標識碼、可用於偵錯或遙測的可自定義客戶端數據,以及選擇性地儲存地區設定。
在這裡,我們會建立 ProtectionEngine::Settings
名為 engineSettings 的物件。
ProtectionEngine::Settings engineSettings("UniqueID", "");
備註
如果使用此方法建立保護設定物件,您也必須透過 手動設定 ProtectionEngineSettings setIdentity()
上的身分識別,或透過 setCloud()
設定目標雲端環境。
最佳實踐是,第一個參數id應該是一些可讓引擎輕鬆連線至其相關聯用戶或者mip::Identity
物件的項目。 若要以mip::Identity
初始化設定:
ProtectionEngine::Settings engineSettings(mip::Identity("Bob@Contoso.com", "");
以這種方式建立 engineSettings 時,請務必透過下列方式明確設定唯一的 engineId:
engineSettings.SetEngineId(engineId);
使用使用者名稱或電子郵件有助於確保每次使用者使用服務或應用程式時,都會載入相同的引擎。
實作:新增保護引擎
若要新增引擎,我們將回到用於載入設定檔的 Future/Promise 模式。 我們不會為 mip::ProtectionProfile
建立承諾,而是使用 mip::ProtectionEngine
。
//auto profile will be std::shared_ptr<mip::ProtectionProfile>
auto profile = profileFuture.get();
//Create the ProtectionEngine::Settings object
ProtectionEngine::Settings engineSettings("UniqueID", "");
//Create a promise for std::shared_ptr<mip::ProtectionEngine>
auto enginePromise = std::make_shared<std::promise<std::shared_ptr<mip::ProtectionEngine>>>();
//Instantiate the future from the promise
auto engineFuture = enginePromise->get_future();
//Add the engine using AddEngineAsync, passing in the engine settings and the promise
profile->AddEngineAsync(engineSettings, enginePromise);
//get the future value and store in std::shared_ptr<mip::ProtectionEngine>
auto engine = engineFuture.get();
上述程式代碼的最終結果是,我們已成功將已驗證用戶的引擎新增至配置檔。
實作:列出範本
使用新增的引擎,現在可以藉由呼叫 engine->GetTemplatesAsync()
來列出已驗證使用者可用的所有敏感度範本。
GetTemplatesAsync()
將會擷取範本標識碼的清單。 結果會儲存在一個std::shared_ptr<std::string>
的向量中。
實作:ListSensitivityTemplates()
auto loadPromise = std::make_shared<std::promise<shared_ptr<vector<string>>>>();
std::future<std::shared_ptr<std::vector<std::string>>> loadFuture = loadPromise->get_future();
mEngine->GetTemplatesAsync(engineObserver, loadPromise);
auto templates = loadFuture.get();
實作:列印範本標識碼
//Iterate through all template IDs in the vector
for (const auto& temp : *templates) {
cout << "Template:" << "\n\tId: " << temp << endl;
}
列印名稱是顯示我們已成功地從服務端取得政策並取得範本的簡單方式。 若要套用範本,則需要範本標識碼。
只有透過原則 SDK,並檢查 ComputeActions()
的結果,才能將範本對應到標籤。
後續步驟
現在已載入配置檔,引擎已新增,而且我們有範本,我們可以新增處理程式,開始從檔案讀取、寫入或移除範本。 請參閱 保護處理程式概念。