Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Managing User Role Relationships via our API

Both InstantForum & InstantKB use our InstantASP.Common framework to manage core user information & user role relationships. Roles (also known as member groups) are used within both InstantForum & InstantKB to control the content accessible to your end users.

If your creating users within our database using our API you may wish to also automatically associate specific forum or knowledgebase roles with users you create via our API.

The code below shows how to add and delete user & role relationships within a separate ASP.MET web application. In the example we are using "1" as the user identity. This identity could be replaced with any genuine ser identity from the "InstantASP_Users" database table.

Before using our API please ensure you've read the following articles "Using the InstantForum.NET API" or "Using the InstantKB API"..

private void Page_Load(System.Object sender, System.EventArgs e)
{
	// userid from InstantASP_Users
	Int32 intUserID = 1;

	// get admin role
	InstantASP.Common.Components.UserRole Role = InstantASP.Common.Business.Roles.SelectRole(InstantASP.Common.Enumerations.EnumRequiredRoles.Administrator);

	// this call will associate the supplied user identity with administrator role
	InstantASP.Common.Business.UserRoles.AddUserRole(intUserID, Role.RoleID);

	// this call will delete the user and administrator role relationship
	InstantASP.Common.Business.UserRoles.DeleteUserRole(intUserID, Role.RoleID);

	// this call will delete all roles associated to the supplied user identity
	InstantASP.Common.Business.UserRoles.DeleteUserRoles(intUserID);

}

Adding New Roles

The code below will add a custom role to the InstantASP_Roles table. You can pass in a number of arguments to the InsertUpdateRole method. The RoleID argument if 0 will always add a new role. If you pass in an existing RoleID the method will attempt to update the existing role.

InstantASP.Common.Business.Roles.InsertUpdateRole(new InstantASP.Common.Components.RoleInfo()
{
	RoleID = 0, 
	RoleName = "New Role Name",
	AdministratorRole = false,
	ModeratorRole = false
});

This mehtod returns a InstantASP.Common.Components.UserRole object representing your new role. You should see your new role added to the InstantASP_Roles table within the database.

Adding User Role Relationships

To associate specific roles with users within InstantForum or InstantKB you should use the following APIā€¦

InstantASP.Common.Business.UserRoles.AddUserRole(intUserID, intRoleID)

Where intUserID is a valid UserID from the InstantASP_Users table and intRoleID is a valid RoleID from the InstantASP_Roles table.

This will add a single role relationship to the InstantASP_UserRoles table. Deleting Single User Role Relationship

Deleting A User Role

The method below will delete a single role relationship for a specific user.

InstantASP.Common.Business.UserRoles.DeleteUserRole(intUserID, intRoleID)

Where intUserID is a valid UserID from the InstantASP_Users table and intRoleID is a valid RoleID from the InstantASP_Roles table.

This will delete a single role relationship from the InstantASP_UserRoles table.

Delete All User Roles

The method below will delete all roles associated with a specific user.

InstantASP.Common.Business.UserRoles.DeleteUserRoles(intUserID)

Where intUserID is a valid UserID from the InstantASP_Users table. This will delete all role relationships for a single user.