Creating a Role and Generating Tasks

Private Sub CreateUserCreationMangerRole()
On Error GoTo errHandler

'Class which helps in getting & using all the schema classes

'in the directory

Dim objSchema As ISchema

Set objSchema = g_objNameSpace.GetSchema

'this part of the code gets all the classes available as a variant array

Dim vntSchemaClasses As Variant

vntSchemaClasses = objSchema.GetClasses(True)

'the above array contains objects which implements ISchemaObject

Dim objUserSchemaObject As ISchemaObject

Dim vntIteratorSchemaClass As Variant

'Iteraing through the array of Ischema objects we get the

'"user" Class object

For Each vntIteratorSchemaClass In vntSchemaClasses

Set objUserSchemaObject = vntIteratorSchemaClass

If objUserSchemaObject.DirectoryName = "user" Then

  Exit For

  End If


'generate tasks for a particular class by setting the class filter on the TaskGenerator
Dim objTaskGenerator As ITaskGenerator

Set objTaskGenerator = g_objNameSpace.GetTaskGenerator

'Setting the classfilter for the "user" schema object

'this helps the objTaskGenerator to generate tasks related to the specified("user") class

objTaskGenerator.ClassFilter = objUserSchemaObject

'generate tasks related to the specified("user") class

Dim objTasks As Tasks

Set objTasks = objTaskGenerator.GenerateTasks

'While creating a Role, you will need to set the Tasks property

'which is of type 'Tasks'

Dim objTasksToAdd As Tasks

Set objTasksToAdd = New Tasks

'Gets the Object Tasks of the 'user' Class. Object tasks are

'basically a superset of all the tasks of the specified Class

Dim vntObjectTasks As Variant

vntObjectTasks = objTasks.GetObjectTasks

'objTasks.GetAttributeTasks would have given all the granular sub tasks of the

'tasks "Read all properties of All objects" and "Write all properties of All objects"

'Declarations for iteration

Dim objTask As ITask

Dim vntIteratorTask As Variant

'Adds the "Create User objects" "Read all properties of All objects"

'from the vntObjectTasks, into objTasksToAdd .. a Tasks object

For Each vntIteratorTask In vntObjectTasks

Set objTask = vntIteratorTask

  If objTask.Name = "Create User objects" _

Or objTask.Name = "Read all properties of All objects" Then

  'make the task as allowed

objTask.AccessType = AccessType.kAccessTypeAllow

  objTasksToAdd.Add objTask

  Exit For

End If


'Create a Dummy Role Object in memory
Dim objRole As Role

Set objRole = New Role

'Sets Name, description & Tasks properties

objRole.Name = "UserCreationManager"

objRole.Description = "Manages the creation of users."

objRole.Tasks = objTasksToAdd

'Inorder to Persist/Update the Role onto the database, you need to get the

'IRoleContainer object from the INameSpace object

'based on the credentials you have supplied

Dim objRoleContainer As IRoleContainer

Set objRoleContainer = g_objNameSpace.GetRoleContainer

'Adds the given role to Database

objRoleContainer.AddRole objRole

Exit Sub

  MsgBox "Error creating UserCreationManager Role" & " Error no: " & Err.Number & " ErrorDescription: " & Err.Description

End Sub


See Also