Security roles are used for a large number of purposes in both the e-commerce and CMS parts of your website. On occasion, you might want to re-purpose these roles to do something dynamic with your views. One example might be to only show the Add to Cart button to a specific role. We'll use that use case for this example.
First, you'll need a plan. In this plan, we're going to make the Add to Cart button available to all people that are logged in and part of the "VIP-Customer" security role. (This isn't a built-in role. It's made-up for this code sample. You can create and use whatever roles that you want.)
Add the code below to the header area of the view that you want to edit, such as the _ProductDetails.cshtml view.
@functions
{
private bool IsVipCustomer()
{
var customer = DotNetNuke.Entities.Users.UserController.Instance.GetCurrentUserInfo();
if (customer != null && customer.UserID > 0)
{
return customer.IsInRole("VIP-Customer") || customer.IsInRole("Administrators");
}
return false;
}
}
In this code sample below, we're checking to see if the role matches what we expect. If it does, we show the Add to Cart button. You can add and use this code anywhere you want, as long as it has the function we created above in the same view file.
@if (IsVipCustomer())
{
<input type="submit" id="addtocartbutton" value="@Localization.GetString("AddToCart")" class="dnnPrimaryAction largeButton fullCartButton" />
}
We hope this helps you as a baseline example for doing anything dynamic with views that involves the security roles in the CMS.
Need More Help?
Do you need more assistance with this article? Please review your support options.