2

I have certain pages that I want accessible only to users that are accessing the site from within a given IP range. For all other users, these pages should be inaccessible, and their respective links not visible in the menu/navigation.

I'm new to OrchardCMS, can someone provide some general guidance and point me in the right direction?

NoCarrier
  • 2,558
  • 4
  • 33
  • 44
  • Checkout [this thread](http://stackoverflow.com/questions/11879500/how-to-limit-page-access-only-to-localhost) – devqon Aug 01 '16 at 14:29
  • devqon, I know how to do this in dot.net, it's not a matter of syntax. The question is how do I extend orchardCMS to result in this behavior. thanks! – NoCarrier Aug 01 '16 at 18:12

2 Answers2

3

There two aspects to answer your question.

1. To check access to orchard content items and menu item relative to it:

To achieve this, you can implement new IAuthorizationServiceEventHandler to replace the default roles based authorization service, the best sample for you is ContentMenuItemAuthorizationEventHandler which you can find under Orchard.ContentPicker module, I included a sample code to explain the usage of this handler:

public class CustomAuthorizationEventHandler : 
    IAuthorizationServiceEventHandler{

    public ContentMenuItemAuthorizationEventHandler() {
    }

    public void Checking(CheckAccessContext context) { }
    public void Adjust(CheckAccessContext context) {
        //Here you can put your business to grant user or not
        context.Granted = true; //Roles service will look to this value to grant access to the user 
        context.Adjusted = true;
    }

    public void Complete(CheckAccessContext context) {}
}

2. To check access to some actions.

To achieve this, you can implement new IAuthorizationFilter to check access to some actions in your system:

public class CustomAuthorizationFilter : FilterProvider, IAuthorizationFilter {
    public void OnAuthorization(AuthorizationContext filterContext) {
        if (!Granted) {
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }
}
Community
  • 1
  • 1
mdameer
  • 1,540
  • 2
  • 14
  • 17
1

The solutions mentioned by @mdameer are ok, but you will run into difficulties when using containers, lists, projections and stuff.

I had a similar task but with date time ranges. See my question and answer to the task to get an idea how to tackle this via a custom part:

How to skip displaying a content item in Orchard CMS?

Community
  • 1
  • 1
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58