How to Check Visitor Group Criteria In Code
If you want to check if a contentAreaItem is match current user all visitor group criteria you can use the following Methods:
public static bool IsMatchCriteria(ContentAreaItem contentAreaItem)
{
var result = true;
contentAreaItem.AllowedRoles.ForEach(role =>
{
result = result && IsMatchCriteria(role);
});
return result;
}
public static bool IsMatchCriteria(string guid)
{
var visitorGroupGuidId = new Guid(guid);
var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
var user = HttpContext.Current.User;
var vgHelper = new VisitorGroupHelper();
var visitorGroup = visitorGroupRepository.Load(visitorGroupGuidId);
return vgHelper.IsPrincipalInGroup(user, visitorGroup.Name);
}
[Pasting files is not allowed][Pasting files is not allowed][Pasting files is not allowed][Pasting files is not allowed]
Great job mate!
just my 2 cent here:
a) why not convert "role" from IsMatchCriteria method to Guid already and pass in that, instead of convert string to Guid?
b) I would try to avoid using ServiceLocator in this context, but instead - would require from "above" - somebody has to inject that dependency in order to test for visitor group match.