3
votes

I am using Sitecore 7.2. I wanted to show or hide certain pages based on a condition on the user.

For example, Show/Give Read access to page A only to those users whose profile field name country = 'USA' just like we do for individual component in Sitecore item.

Is there a way to do this in Sitecore?

2

2 Answers

5
votes

This sounds like the Rules Engine might be a good option for you. You could add a Rules field to your page template to drive the logic.

The rule conditions to check the profile fields already exist within Sitecore, you'd just need to create your own custom logic to deny access to the page

enter image description here

To evaluate the rules on your page you can put some code into a pipeline processor to check the rules apply for each page. Here's an example of evaluating the rules.

 public bool EvaluateRule(string fieldName, Item item)
 {
        var ruleContext = new RuleContext();
        foreach (Rule<RuleContext> rule in RuleFactory.GetRules<RuleContext>(new[] { item }, fieldName).Rules)
        {
            if (rule.Condition != null)
            {
                var stack = new RuleStack();
                rule.Condition.Evaluate(ruleContext, stack);

                if (ruleContext.IsAborted)
                {
                    continue;
                }
                if ((stack.Count != 0) && ((bool)stack.Pop()))
                {
                    return true;
                }
            }
        }

        return false;
    }  

You could combine this with this code to deny access to the page

 public class RulesProcessor : HttpRequestProcessor
 {
     public override void Process(HttpRequestArgs args)
     {
        Assert.ArgumentNotNull((object)args, "args");

        if (!UserCanAccess())
        {
            Sitecore.Context.Item=null;
            args.PermissionDenied = true;
        }
    }

    private bool UserCanAccess()
    {
       EvaluateRulesLogic();
    }
}
0
votes

Why not using the role based permission that Sitecore supports?

So for example of each different Countries, you have the respective roles inside Sitecore and use that to define the permission to the items.

Next step would be to update the roles assigned to the user if the profile field country is updated.