Kentico CMS 7.0 Developer's Guide

Managing widget security

Managing widget security

Previous topic Next topic Mail us feedback on this topic!  

Managing widget security

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 assigns a specific role to a widget (the widget will be usable by users belonging to the given role).

 

private bool AddWidgetToRole()
{

  // Get role, widget and permission object
  RoleInfo role = RoleInfoProvider.GetRoleInfo("CMSDeskAdmin", CMSContext.CurrentSiteID);
  WidgetInfo widget = WidgetInfoProvider.GetWidgetInfo("MyNewWidget");
  PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("AllowedFor", "Widgets", null);

 
    // If all exist
    if ((role != null) && (widget != null) && (permission != null))
    {

      // Add role to widget
      WidgetRoleInfoProvider.AddRoleToWidget(role.RoleID, widget.WidgetID, permission.PermissionId);

            
        return true;
    }
 
    return false;
}

 

The following example removes the relationship between a specific role and a widget.

 

private bool RemoveWidgetFromRole()
{

  // Get role, widget and permission object
  RoleInfo role = RoleInfoProvider.GetRoleInfo("CMSDeskAdmin", CMSContext.CurrentSiteID);
  WidgetInfo widget = WidgetInfoProvider.GetWidgetInfo("MyNewWidget");
  PermissionNameInfo permission = PermissionNameInfoProvider.GetPermissionNameInfo("AllowedFor", "Widgets", null);

 
    // If all exist
    if ((role != null) && (widget != null) && (permission != null))
    {

      // Remove role from widget
      WidgetRoleInfoProvider.RemoveRoleFromWidget(role.RoleID, widget.WidgetID, permission.PermissionId);

 
        return true;
    }
 
    return false;
}

 

The following example sets the security level for a widget (allows it to be used by all authenticated users).

 

private bool SetSecurityLevel()
{
    // Get widget object
    WidgetInfo widget = WidgetInfoProvider.GetWidgetInfo("MyNewWidget");
        
    // If widget exists
    if (widget != null)
    {
        // Set security access type
        widget.AllowedFor = SecurityAccessEnum.AuthenticatedUsers;
 
        WidgetInfoProvider.SetWidgetInfo(widget);
 
        return true;
    }
 
    return false;
}