privatebool CreatePermission() { // Get the resource ResourceInfo module = ResourceInfoProvider.GetResourceInfo("MyNewModule"); if (module != null) { // Create new permission object PermissionNameInfo newPermission = newPermissionNameInfo();
// Set the properties newPermission.PermissionDisplayName = "My new permission"; newPermission.PermissionName = "MyNewPermission"; newPermission.ResourceId = module.ResourceId;
// Save the permission PermissionNameInfoProvider.SetPermissionInfo(newPermission);
returntrue; }
returnfalse; }
The following example gets and updates a permission.
privatebool GetAndUpdatePermission() { // Get the permission PermissionNameInfo updatePermission = PermissionNameInfoProvider.GetPermissionNameInfo("MyNewPermission", "MyNewModule", null); if (updatePermission != null) { // Update the properties updatePermission.PermissionDisplayName = updatePermission.PermissionDisplayName.ToLower();
// Save the changes PermissionNameInfoProvider.SetPermissionInfo(updatePermission);
returntrue; }
returnfalse; }
The following example gets and bulk updates permissions.
privatebool GetAndBulkUpdatePermissions() { // Prepare the parameters string where = "PermissionName LIKE N'MyNewPermission%'";
// Get the data DataSet permissions = PermissionNameInfoProvider.GetPermissionNames(where, null, 0, null); if (!DataHelper.DataSourceIsEmpty(permissions)) { // Loop through the individual items foreach (DataRow permissionDr in permissions.Tables[0].Rows) { // Create object from DataRow PermissionNameInfo modifyPermission = newPermissionNameInfo(permissionDr);
// Update the properties modifyPermission.PermissionDisplayName = modifyPermission.PermissionDisplayName.ToUpper();
// Save the changes PermissionNameInfoProvider.SetPermissionInfo(modifyPermission); }
returntrue; }
returnfalse; }
The following example adds a permission to role.
privatebool AddPermissionToRole() { // Get the permission PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("MyNewPermission", "MyNewModule", null);
// Get the role RoleInfo role = RoleInfoProvider.GetRoleInfo("cmsdeskadmin", CMSContext.CurrentSiteID);
if ((permission != null) && (role != null)) { // Create new role permission object RolePermissionInfo newRolePermission = newRolePermissionInfo();
// Set the properties newRolePermission.PermissionID = permission.PermissionId; newRolePermission.RoleID = role.RoleID;
// Add permission to role RolePermissionInfoProvider.SetRolePermissionInfo(newRolePermission);
returntrue; }
returnfalse; }
The following example removes a permission from role.
privatebool RemovePermissionFromRole() { // Get the permission PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("MyNewPermission", "MyNewModule", null);
// Get the role RoleInfo role = RoleInfoProvider.GetRoleInfo("cmsdeskadmin", CMSContext.CurrentSiteID);
if ((permission != null) && (role != null)) { // Get the role permission RolePermissionInfo deleteRolePermission = RolePermissionInfoProvider.GetRolePermissionInfo(role.RoleID, permission.PermissionId);
// Remove permission from role RolePermissionInfoProvider.DeleteRolePermissionInfo(deleteRolePermission);
returntrue; }
returnfalse; }
The following example deletes a permission.
privatebool DeletePermission() { // Get the permission PermissionNameInfo deletePermission = PermissionNameInfoProvider.GetPermissionNameInfo("MyNewPermission", "MyNewModule", null);
// Delete the permission PermissionNameInfoProvider.DeletePermissionInfo(deletePermission);