在 C++ 中将任务分组为角色
在授权管理器中,角色表示用户类别以及这些用户有权执行的任务。 任务组合在一起并分配给角色定义,该定义由 IAzTask 对象表示,其 IsRoleDefinition 属性设置为 TRUE。 然后,可以将角色定义分配给 IAzRole 对象,然后将用户或用户组分配给该对象。 有关任务和角色的详细信息,请参阅 角色。
以下示例演示如何将任务分配给角色定义、创建角色对象以及将角色定义分配给角色对象。 该示例假定驱动器 C 的根目录中有一个名为 MyStore.xml 的现有 XML 策略存储,该存储区包含名为 Expense 的应用程序,并且此应用程序包含名为“提交费用”和“批准支出”的任务。
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0502
#endif
#pragma comment(lib, "duser.lib")
#include <windows.h>
#include <stdio.h>
#include <azroles.h>
#include <objbase.h>
void main(void){
IAzAuthorizationStore* pStore = NULL;
IAzApplication* pApp = NULL;
IAzTask* pTaskRoleDef = NULL;
IAzRole* pRole = NULL;
HRESULT hr;
void MyHandleError(char *s);
BSTR storeName = NULL;
BSTR appName = NULL;
BSTR taskNameSubmit = NULL;
BSTR taskNameApprove = NULL;
BSTR roleDefName = NULL;
BSTR roleName = NULL;
// Initialize COM.
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not initialize COM.");
// Create the AzAuthorizationStore object.
hr = CoCreateInstance(
/*"b2bcff59-a757-4b0b-a1bc-ea69981da69e"*/
__uuidof(AzAuthorizationStore),
NULL,
CLSCTX_ALL,
/*"edbd9ca9-9b82-4f6a-9e8b-98301e450f14"*/
__uuidof(IAzAuthorizationStore),
(void**)&pStore);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not create AzAuthorizationStore object.");
// Create null VARIANT for parameters.
VARIANT myVar;
VariantInit(&myVar);
// Allocate a string for the name of the policy store.
storeName = SysAllocString(L"msxml://c:\\myStore.xml");
if (!storeName)
MyHandleError("Could not allocate string.");
// Initialize the store.
hr = pStore->Initialize(AZ_AZSTORE_FLAG_MANAGE_STORE_ONLY, storeName, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not initialize store.");
// Create an application object.
appName = SysAllocString(L"Expense");
if (!appName)
MyHandleError("Could not allocate application name string.");
hr = pStore->OpenApplication(appName, myVar, &pApp);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not open application.");
// Allocate strings for the task names.
taskNameSubmit = SysAllocString(L"Submit Expense");
if (!taskNameSubmit)
MyHandleError("Could not allocate first task name string.");
taskNameApprove = SysAllocString(L"Approve Expense");
if (!taskNameApprove)
MyHandleError("Could not allocate second task name string.");
// Create a third task object to act as a role definition.
roleDefName = SysAllocString(L"Expense Admin");
if (!roleDefName)
MyHandleError("Could not allocate role definition name.");
hr = pApp->CreateTask(roleDefName, myVar, &pTaskRoleDef);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not create role definition.");
// Set the IsRoleDefinition property of pTaskRoleDef to TRUE.
hr = pTaskRoleDef->put_IsRoleDefinition(true);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not set role definition property.");
// Add two tasks to the role definition.
hr = pTaskRoleDef->AddTask(taskNameSubmit, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not add submit task.");
hr = pTaskRoleDef->AddTask(taskNameApprove, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not add approve task.");
// Save information to the store.
hr = pTaskRoleDef->Submit(0, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not save task data to the store.");
// Create an IAzRole object.
roleName = SysAllocString(L"Expense Administrator");
if (!roleName)
MyHandleError("Could not allocate role name.");
hr = pApp->CreateRole(roleName, myVar, &pRole);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not create a role object.");
// Add the role definition to the role object.
hr = pRole->AddTask(roleDefName, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could add role definition to the role.");
// Save information to the store.
hr = pRole->Submit(0, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not save role data to the store.");
// Clean up resources.
pStore->Release();
pApp->Release();
pTaskRoleDef->Release();
pRole->Release();
SysFreeString(storeName);
SysFreeString(appName);
SysFreeString(taskNameSubmit);
SysFreeString(taskNameApprove);
SysFreeString(roleDefName);
SysFreeString(roleName);
VariantClear(&myVar);
CoUninitialize();
}
void MyHandleError(char *s)
{
printf("An error occurred in running the program.\n");
printf("%s\n",s);
printf("Error number %x\n.",GetLastError());
printf("Program terminating.\n");
exit(1);
}