Kentico CMS 7.0 Developer's Guide

Managing permissions

Managing permissions

Previous topic Next topic Mail us feedback on this topic!  

Managing permissions

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a permission.

 

private bool CreatePermission()
{
  // Get the resource
  ResourceInfo module = ResourceInfoProvider.GetResourceInfo("MyNewModule");
  if (module != null)
   {
      // Create new permission object
      PermissionNameInfo newPermission = new PermissionNameInfo();

 
      // Set the properties
       newPermission.PermissionDisplayName = "My new permission";
       newPermission.PermissionName = "MyNewPermission";
       newPermission.ResourceId = module.ResourceId;

 
      // Save the permission
      PermissionNameInfoProvider.SetPermissionInfo(newPermission);

 
      return true;
   }

 
  return false;
}

 

The following example gets and updates a permission.

 

private bool 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);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates permissions.

 

private bool 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 = new PermissionNameInfo(permissionDr);

 
          // Update the properties
           modifyPermission.PermissionDisplayName = modifyPermission.PermissionDisplayName.ToUpper();

 
          // Save the changes
          PermissionNameInfoProvider.SetPermissionInfo(modifyPermission);
       }

 
      return true;
   }

 
  return false;
}

 

The following example adds a permission to role.

 

private bool 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 = new RolePermissionInfo();

 
      // Set the properties
       newRolePermission.PermissionID = permission.PermissionId;
       newRolePermission.RoleID = role.RoleID;

 
      // Add permission to role
      RolePermissionInfoProvider.SetRolePermissionInfo(newRolePermission);

 
      return true;
   }

 
  return false;
}

 

The following example removes a permission from role.

 

private bool 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);

 
      return true;
   }

 
  return false;
}

 

The following example deletes a permission.

 

private bool DeletePermission()
{
  // Get the permission
  PermissionNameInfo deletePermission = PermissionNameInfoProvider.GetPermissionNameInfo("MyNewPermission", "MyNewModule", null);

 
  // Delete the permission
  PermissionNameInfoProvider.DeletePermissionInfo(deletePermission);

 
  return (deletePermission != null);
}