Allow guests to view homepage only

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 years ago
I'm trying to find a solution to allow users to only be able to view the homepage, but when they click on anything it re-directs to the register page.  I tried playing around with the access control list, but if you turn navigation off for guests they can't even view the homepage.  

Any ideas would be much appreciated.
9 years ago
finalthrill wrote:
I'm trying to find a solution to allow users to only be able to view the homepage, but when they click on anything it re-directs to the register page.  I tried playing around with the access control list, but if you turn navigation off for guests they can't even view the homepage.  

Any ideas would be much appreciated.


One very easy solution is to:

1) Open Root.Head.cshtml
2) Add the following at the very top (after using statements)

@* other using statements ... *@
using Nop.Core.Domain.Customers;

@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        Request.Url.AbsolutePath != "/" &&
        Request.Url.AbsolutePath != "login") // and other pages you want to allow
    {
        Response.Redirect("~/", true);
    }
}


Not the most sophisticated solution, but it's quick and it works! :)
9 years ago
Thank you wooncherk.  I'm not sure how you got that code to work as it creates an infinite re-direct loop for me.
9 years ago
finalthrill wrote:
Thank you wooncherk.  I'm not sure how you got that code to work as it creates an infinite re-direct loop for me.


Can you check the updated code? I updated 1 minute after my original post. :)
9 years ago
Nevermind, your edit fixed it.  Thank you for the simple solution!
9 years ago
finalthrill wrote:
Nevermind, your edit fixed it.  Thank you for the simple solution!


Make sure to check all the necessary pages (register, contact us and etc). Mine is just a simplified version. :)
9 years ago
I just had to modify it a bit to make sure it hits a topic page anywhere they try to go anywhere other than the homepage and login page:

@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        Request.Url.AbsolutePath != "/" &&
        Request.Url.AbsolutePath != "/t/PageNotFound" &&
        Request.Url.AbsolutePath != "/login")
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}

Works like a charm!  Thanks again!
9 years ago
finalthrill wrote:
I just had to modify it a bit to make sure it hits a topic page anywhere they try to go anywhere other than the homepage and login page:

@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        Request.Url.AbsolutePath != "/" &&
        Request.Url.AbsolutePath != "/t/PageNotFound" &&
        Request.Url.AbsolutePath != "/login")
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}

Works like a charm!  Thanks again!


Forgot to mention, there can be a bug in my earlier code due to string comparison being case-sensitive. So we need to make sure we ignore the case. A better version is to use:

Request.Url.AbsolutePath.Equals("/login", StringComparison.InvariantCultureIgnoreCase)
9 years ago
Good catch!  I just had to add not conditions to each inequality:  


@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        !(Request.Url.AbsolutePath.Equals("/", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/t/PageNotFound", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/login", StringComparison.InvariantCultureIgnoreCase)))// and other pages you want to allow
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}
9 years ago
finalthrill wrote:
Good catch!  I just had to add not conditions to each inequality:  


@{
    if (WorkContext.CurrentCustomer.IsGuest() &&
        !(Request.Url.AbsolutePath.Equals("/", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/t/PageNotFound", StringComparison.InvariantCultureIgnoreCase)) &&
        !(Request.Url.AbsolutePath.Equals("/login", StringComparison.InvariantCultureIgnoreCase)))// and other pages you want to allow
    {
        Response.Redirect("/t/PageNotFound", true);
    }
}


Yeap! Sounds good! :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.