My customer wants that the visitor to their nopcommerce based website should see the product catalog only after they log in.
How to enforce a login before one can view the products?
public partial class BaseNopController
{
private class AllowedRoutes
{
public string Controller { get; set; }
public string Action { get; set; }
}
protected override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
var currentAction = filterContext.RouteData.Values["action"].ToString();
var currentControler = filterContext.RouteData.Values["controller"].ToString();
var allowedActions = new List<AllowedRoutes>
{
new AllowedRoutes {Controller = "Home", Action = "Invite"},
new AllowedRoutes {Controller = "Customer", Action = "Login"},
new AllowedRoutes {Controller = "Topic", Action = "TopicBlock"}
};
var query = from a in allowedActions
where a.Action == currentAction &&
a.Controller == currentControler
select a;
var ignorePage = (query.ToList().Count > 0);
var workContext = EngineContext.Current.Resolve<IWorkContext>();
if (!workContext.CurrentCustomer.IsRegistered() && !ignorePage)
{
filterContext.Result = new RedirectResult(Url.Action("Invite", "Home"));
return;
}
}
}
/* Nop.Web.Controllers/HomeController.myco.cs */
using System;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Services.Authentication;
using Nop.Services.Customers;
using Nop.Services.Orders;
using Nop.Web.Models.Customer;
namespace Nop.Web.Controllers
{
public partial class HomeController
{
private readonly CustomerSettings _customerSettings;
private readonly ICustomerRegistrationService _customerRegistrationService;
private readonly ICustomerService _customerService;
private readonly IShoppingCartService _shoppingCartService;
private readonly IWorkContext _workContext;
private readonly IAuthenticationService _authenticationService;
public HomeController(CustomerSettings customerSettings, ICustomerRegistrationService customerRegistrationService, ICustomerService customerService,
IShoppingCartService shoppingCartService, IWorkContext workContext, IAuthenticationService authenticationService)
{
this._customerSettings = customerSettings;
this._customerRegistrationService = customerRegistrationService;
this._customerService = customerService;
this._shoppingCartService = shoppingCartService;
this._workContext = workContext;
this._authenticationService = authenticationService;
}
public ActionResult Invite(bool? checkoutAsGuest)
{
var model = new LoginModel();
model.UsernamesEnabled = _customerSettings.UsernamesEnabled;
model.CheckoutAsGuest = checkoutAsGuest.HasValue ? checkoutAsGuest.Value : false;
return View(model);
}
[HttpPost]
public ActionResult Invite(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
//do our own little login to prevent issue with views on failed attempts.
if (_customerSettings.UsernamesEnabled && model.Username != null)
{
model.Username = model.Username.Trim();
}
if (_customerRegistrationService.ValidateCustomer(_customerSettings.UsernamesEnabled ? model.Username : model.Email, model.Password))
{
var customer = _customerSettings.UsernamesEnabled ? _customerService.GetCustomerByUsername(model.Username) : _customerService.GetCustomerByEmail(model.Email);
//migrate shopping cart
_shoppingCartService.MigrateShoppingCart(_workContext.CurrentCustomer, customer);
//sign in new customer
_authenticationService.SignIn(customer, model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Invalid login details provided.");
}
return View(model);
}
}
}
@model Nop.Web.Models.Customer.LoginModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Myco Invitation Login</title>
<link href="/Themes/myco/Content/invite.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header class="header">
<div class="top-menu">
<div class="message-error">@Html.ValidationSummary(true, T("Account.Login.Unsuccessful").Text)</div>
</div>
<div class="header-logo">
<img src="/Themes/myco/Content/images/full-logo.jpg" alt="motto!" />
</div>
<div class="login">
@using (Html.BeginForm())
{
<table class="login-table-container">
<tbody>
@if (Model.UsernamesEnabled)
{
<tr class="row">
<td class="item-name">
@Html.LabelFor(m => m.Username):
</td>
<td class="item-value">
@Html.TextBoxFor(m => m.Username)
@Html.ValidationMessageFor(m => m.Username)
</td>
</tr>
}
else
{
<tr class="row">
<td class="item-name">
@Html.LabelFor(m => m.Email):
</td>
<td class="item-value">
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</td>
</tr>
}
<tr class="row">
<td class="item-name">
@Html.LabelFor(m => m.Password):
</td>
<td class="item-value">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</td>
</tr>
<tr class="row">
<td class="item-value" colspan="2">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</td>
</tr>
<tr class="row">
<td colspan="2">
<div class="buttons">
<button class="loginbutton" type="submit">@T("Account.Login.LoginButton")</button>
</div>
</td>
</tr>
</tbody>
</table>
}
</div>
<div class="clear" />
</header>
<div class="content">
<div class="about">
<h1>Welcome</h1>
<p>
<strong>Blurb...</strong>
</p>
<p>
The site is currently by invitation only...
</p>
<p>
<strong>... </strong>
</p>
</div>
<footer class="footer clear">
</footer>
</body>
</html>
public partial class HomeController : BaseNopController
{
private readonly CustomerSettings _customerSettings;
private readonly ICustomerRegistrationService _customerRegistrationService;
private readonly ICustomerService _customerService;
private readonly IShoppingCartService _shoppingCartService;
private readonly IWorkContext _workContext;
private readonly IAuthenticationService _authenticationService;
public HomeController(CustomerSettings customerSettings, ICustomerRegistrationService customerRegistrationService, ICustomerService customerService,
IShoppingCartService shoppingCartService, IWorkContext workContext, IAuthenticationService authenticationService)
private Services.Customers.ICustomerService _customerService;
public partial classbut I'm hit with error above.. The Nop BaseController as an
public abstract class
public abstract partial class BaseNopController : Controller