Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Add new AuthorizeAttribute (CustomerGroupAuthorizeAttribute)

support55040884bc5a
diff --git a/src/Business/Grand.Business.Common/Services/Directory/GroupService.cs b/src/Business/Grand.Business.Common/Services/Directory/GroupService.cs
index 13a49254e..8de4022c1 100644
--- a/src/Business/Grand.Business.Common/Services/Directory/GroupService.cs
+++ b/src/Business/Grand.Business.Common/Services/Directory/GroupService.cs
@@ -16,42 +16,6 @@ namespace Grand.Business.Common.Services.Directory
private readonly ICacheBase _cacheBase;
private readonly IMediator _mediator;
- #region Utilities
-
- /// <summary>
- /// Gets a value indicating whether customer is in a certain customer group
- /// </summary>
- /// <param name="customer">Customer</param>
- /// <param name="customerGroupSystemName">Customer group system name</param>
- /// <param name="onlyActiveCustomerGroups">A value indicating whether we should look only in active customer groups</param>
- /// <param name="isSystem">A value indicating whether we should look only in system groups</param>
- /// <returns>Result</returns>
- private async Task<bool> IsInCustomerGroup(Customer customer,
- string customerGroupSystemName,
- bool onlyActiveCustomerGroups = true,
- bool? isSystem = null)
- {
- if (customer == null)
- throw new ArgumentNullException(nameof(customer));
-
- if (string.IsNullOrEmpty(customerGroupSystemName))
- throw new ArgumentNullException(nameof(customerGroupSystemName));
-
- var customerGroup = await GetCustomerGroupBySystemName(customerGroupSystemName);
- if (customerGroup == null)
- return false;
-
- var result =
- customer.Groups.Contains(customerGroup.Id)
- && (!onlyActiveCustomerGroups || customerGroup.Active)
- && customerGroup.SystemName == customerGroupSystemName
- && (!isSystem.HasValue || customerGroup.IsSystem == isSystem);
-
- return result;
- }
-
- #endregion
-
public GroupService(IRepository<CustomerGroup> customerGroupRepository,
ICacheBase cacheBase,
IMediator mediator)
@@ -169,6 +133,38 @@ namespace Grand.Business.Common.Services.Directory
await _mediator.EntityDeleted(customerGroup);
}
+ /// <summary>
+ /// Gets a value indicating whether customer is in a certain customer group
+ /// </summary>
+ /// <param name="customer">Customer</param>
+ /// <param name="customerGroupSystemName">Customer group system name</param>
+ /// <param name="onlyActiveCustomerGroups">A value indicating whether we should look only in active customer groups</param>
+ /// <param name="isSystem">A value indicating whether we should look only in system groups</param>
+ /// <returns>Result</returns>
+ public virtual async Task<bool> IsInCustomerGroup(Customer customer,
+ string customerGroupSystemName,
+ bool onlyActiveCustomerGroups = true,
+ bool? isSystem = null)
+ {
+ if (customer == null)
+ throw new ArgumentNullException(nameof(customer));
+
+ if (string.IsNullOrEmpty(customerGroupSystemName))
+ throw new ArgumentNullException(nameof(customerGroupSystemName));
+
+ var customerGroup = await GetCustomerGroupBySystemName(customerGroupSystemName);
+ if (customerGroup == null)
+ return false;
+
+ var result =
+ customer.Groups.Contains(customerGroup.Id)
+ && (!onlyActiveCustomerGroups || customerGroup.Active)
+ && customerGroup.SystemName == customerGroupSystemName
+ && (!isSystem.HasValue || customerGroup.IsSystem == isSystem);
+
+ return result;
+ }
+
public Task<bool> IsStaff(Customer customer)
{
return IsInCustomerGroup(customer, SystemCustomerGroupNames.Staff, true, true);
diff --git a/src/Business/Grand.Business.Core/Interfaces/Common/Directory/IGroupService.cs b/src/Business/Grand.Business.Core/Interfaces/Common/Directory/IGroupService.cs
index 2fd9e1ff7..078ff86f8 100644
--- a/src/Business/Grand.Business.Core/Interfaces/Common/Directory/IGroupService.cs
+++ b/src/Business/Grand.Business.Core/Interfaces/Common/Directory/IGroupService.cs
@@ -5,6 +5,19 @@ namespace Grand.Business.Core.Interfaces.Common.Directory
{
public interface IGroupService
{
+ /// <summary>
+ /// Gets a value indicating whether customer is in a certain customer group
+ /// </summary>
+ /// <param name="customer">Customer</param>
+ /// <param name="customerGroupSystemName">Customer group system name</param>
+ /// <param name="onlyActiveCustomerGroups">A value indicating whether we should look only in active customer groups</param>
+ /// <param name="isSystem">A value indicating whether we should look only in system groups</param>
+ /// <returns>Result</returns>
+ Task<bool> IsInCustomerGroup(Customer customer,
+ string customerGroupSystemName,
+ bool onlyActiveCustomerGroups = true,
+ bool? isSystem = null);
+
/// <summary>
/// Gets a value indicating whether customer is staff
/// </summary>
diff --git a/src/Core/Grand.Domain/Customers/SystemCustomerGroupNames.cs b/src/Core/Grand.Domain/Customers/SystemCustomerGroupNames.cs
index 9e2cabb88..d0b66d3a6 100644
--- a/src/Core/Grand.Domain/Customers/SystemCustomerGroupNames.cs
+++ b/src/Core/Grand.Domain/Customers/SystemCustomerGroupNames.cs
@@ -2,16 +2,16 @@ namespace Grand.Domain.Customers
{
public static partial class SystemCustomerGroupNames
{
- public static string Administrators { get { return "Administrators"; } }
-
- public static string Registered { get { return "Registered"; } }
+ public const string Administrators = "Administrators";
- public static string Guests { get { return "Guests"; } }
+ public const string Registered = "Registered";
- public static string Vendors { get { return "Vendors"; } }
+ public const string Guests = "Guests";
- public static string Staff { get { return "Staff"; } }
+ public const string Vendors = "Vendors";
- public static string SalesManager { get { return "SalesManager"; } }
+ public const string Staff = "Staff";
+
+ public const string SalesManager = "SalesManager";
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web.Common/Security/Authorization/CustomerGroupAuthorizeAttribute.cs b/src/Web/Grand.Web.Common/Security/Authorization/CustomerGroupAuthorizeAttribute.cs
new file mode 100644
index 000000000..f7a884c19
--- /dev/null
+++ b/src/Web/Grand.Web.Common/Security/Authorization/CustomerGroupAuthorizeAttribute.cs
@@ -0,0 +1,44 @@
+using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Infrastructure;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Filters;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Grand.Web.Common.Security.Authorization
+{
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
+ public class CustomerGroupAuthorizeAttribute : Attribute, IAsyncAuthorizationFilter
+ {
+ public CustomerGroupAuthorizeAttribute(string group)
+ {
+ GroupName = group;
+ }
+
+ public string GroupName { get; set; }
+
+ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
+ {
+ if (context == null)
+ throw new ArgumentNullException(nameof(context));
+
+ if (string.IsNullOrEmpty(GroupName))
+ return;
+
+ var groupService = context.HttpContext.RequestServices.GetRequiredService<IGroupService>();
+ var workContext = context.HttpContext.RequestServices.GetRequiredService<IWorkContext>();
+
+ if (await groupService.IsInCustomerGroup(workContext.CurrentCustomer, GroupName, true))
+ return;
+
+ var result = (context.HttpContext.Request.ContentType?.Contains("application/json") ?? false) ||
+ context.HttpContext.Request.Headers.Accept.ToString().Contains("application/json", StringComparison.InvariantCultureIgnoreCase) ||
+ context.HttpContext.Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase);
+
+ if (result)
+ context.Result = new ObjectResult("") { StatusCode = 403 };
+ else
+ context.Result = new RedirectToRouteResult("Login", new { returnUrl = context.HttpContext.Request.Path });
+
+ }
+ }
+}
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index ebbaf857e..cf35f65c2 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -12,17 +12,17 @@ using Grand.Domain.Customers;
using Grand.Domain.Stores;
using Grand.Infrastructure;
using Grand.Infrastructure.Extensions;
-using Grand.Web.Commands.Models.Customers;
using Grand.SharedKernel.Attributes;
+using Grand.Web.Commands.Models.Customers;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Filters;
+using Grand.Web.Common.Security.Authorization;
using Grand.Web.Common.Security.Captcha;
using Grand.Web.Extensions;
using Grand.Web.Features.Models.Common;
using Grand.Web.Features.Models.Customers;
using Grand.Web.Models.Customer;
using MediatR;
-using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@@ -112,19 +112,19 @@ namespace Grand.Web.Controllers
switch (loginResult)
{
case CustomerLoginResults.Successful:
- {
- var customer = _customerSettings.UsernamesEnabled
- ? await _customerService.GetCustomerByUsername(model.Username)
- : await _customerService.GetCustomerByEmail(model.Email);
- //sign in
- return await SignInAction(customer, model.RememberMe, returnUrl);
- }
+ {
+ var customer = _customerSettings.UsernamesEnabled
+ ? await _customerService.GetCustomerByUsername(model.Username)
+ : await _customerService.GetCustomerByEmail(model.Email);
+ //sign in
+ return await SignInAction(customer, model.RememberMe, returnUrl);
+ }
case CustomerLoginResults.RequiresTwoFactor:
- {
- var userName = _customerSettings.UsernamesEnabled ? model.Username : model.Email;
- HttpContext.Session.SetString("RequiresTwoFactor", userName);
- return RedirectToRoute("TwoFactorAuthorization");
- }
+ {
+ var userName = _customerSettings.UsernamesEnabled ? model.Username : model.Email;
+ HttpContext.Session.SetString("RequiresTwoFactor", userName);
+ return RedirectToRoute("TwoFactorAuthorization");
+ }
}
}
@@ -282,7 +282,9 @@ namespace Grand.Web.Controllers
var customer = await _customerService.GetCustomerByEmail(model.Email);
await _mediator.Send(new PasswordRecoverySendCommand {
- Customer = customer, Store = _workContext.CurrentStore, Language = _workContext.WorkingLanguage,
+ Customer = customer,
+ Store = _workContext.CurrentStore,
+ Language = _workContext.WorkingLanguage,
Model = model
});
@@ -383,8 +385,7 @@ namespace Grand.Web.Controllers
_customerSettings.DefaultPasswordFormat, _workContext.CurrentStore.Id, isApproved);
await _customerManagerService.RegisterCustomer(registrationRequest);
- var customerAttributes = await _mediator.Send(new GetParseCustomAttributes
- { SelectedAttributes = model.SelectedAttributes });
+ var customerAttributes = await _mediator.Send(new GetParseCustomAttributes { SelectedAttributes = model.SelectedAttributes });
await _mediator.Send(new CustomerRegisteredCommand {
Customer = _workContext.CurrentCustomer,
@@ -403,42 +404,42 @@ namespace Grand.Web.Controllers
switch (_customerSettings.UserRegistrationType)
{
case UserRegistrationType.EmailValidation:
- {
- //email validation message
- await _userFieldService.SaveField(_workContext.CurrentCustomer,
- SystemCustomerFieldNames.AccountActivationToken, Guid.NewGuid().ToString());
- await _messageProviderService.SendCustomerEmailValidationMessage(
- _workContext.CurrentCustomer, _workContext.CurrentStore,
- _workContext.WorkingLanguage.Id);
-
- //result
- return RedirectToRoute("RegisterResult",
- new { resultId = (int)UserRegistrationType.EmailValidation });
- }
+ {
+ //email validation message
+ await _userFieldService.SaveField(_workContext.CurrentCustomer,
+ SystemCustomerFieldNames.AccountActivationToken, Guid.NewGuid().ToString());
+ await _messageProviderService.SendCustomerEmailValidationMessage(
+ _workContext.CurrentCustomer, _workContext.CurrentStore,
+ _workContext.WorkingLanguage.Id);
+
+ //result
+ return RedirectToRoute("RegisterResult",
+ new { resultId = (int)UserRegistrationType.EmailValidation });
+ }
case UserRegistrationType.AdminApproval:
- {
- return RedirectToRoute("RegisterResult",
- new { resultId = (int)UserRegistrationType.AdminApproval });
- }
+ {
+ return RedirectToRoute("RegisterResult",
+ new { resultId = (int)UserRegistrationType.AdminApproval });
+ }
case UserRegistrationType.Standard:
- {
- //send customer welcome message
- await _messageProviderService.SendCustomerWelcomeMessage(_workContext.CurrentCustomer,
- _workContext.CurrentStore, _workContext.WorkingLanguage.Id);
-
- var redirectUrl = Url.RouteUrl("RegisterResult",
- new { resultId = (int)UserRegistrationType.Standard }, HttpContext.Request.Scheme);
- if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
{
- redirectUrl = CommonExtensions.ModifyQueryString(redirectUrl, "returnurl", returnUrl);
+ //send customer welcome message
+ await _messageProviderService.SendCustomerWelcomeMessage(_workContext.CurrentCustomer,
+ _workContext.CurrentStore, _workContext.WorkingLanguage.Id);
+
+ var redirectUrl = Url.RouteUrl("RegisterResult",
+ new { resultId = (int)UserRegistrationType.Standard }, HttpContext.Request.Scheme);
+ if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
+ {
+ redirectUrl = CommonExtensions.ModifyQueryString(redirectUrl, "returnurl", returnUrl);
+ }
+
+ return Redirect(redirectUrl);
}
-
- return Redirect(redirectUrl);
- }
default:
- {
- return RedirectToRoute("HomePage");
- }
+ {
+ return RedirectToRoute("HomePage");
+ }
}
}
@@ -449,8 +450,7 @@ namespace Grand.Web.Controllers
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
Model = model,
- OverrideCustomCustomerAttributes = await _mediator.Send(new GetParseCustomAttributes
- { SelectedAttributes = model.SelectedAttributes })
+ OverrideCustomCustomerAttributes = await _mediator.Send(new GetParseCustomAttributes { SelectedAttributes = model.SelectedAttributes })
});
return View(model);
@@ -549,14 +549,11 @@ namespace Grand.Web.Controllers
#endregion
#region My account / Info
-
+
[HttpGet]
- [Authorize]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Info()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var model = await _mediator.Send(new GetInfo {
Customer = _workContext.CurrentCustomer,
ExcludeProperties = false,
@@ -568,11 +565,9 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Info(CustomerInfoModel model)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (ModelState.IsValid)
{
await _mediator.Send(new UpdateCustomerInfoCommand {
@@ -606,35 +601,33 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> RemoveExternalAssociation(string id, [FromServices] IExternalAuthenticationService openAuthenticationService)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
//ensure it's our record
var ear = (await openAuthenticationService.GetExternalIdentifiers(_workContext.CurrentCustomer))
.FirstOrDefault(x => x.Id == id);
if (ear == null)
{
- return Json(new {
+ return Json(new
+ {
redirect = Url.Action("Info")
});
}
await openAuthenticationService.DeleteExternalAuthentication(ear);
- return Json(new {
+ return Json(new
+ {
redirect = Url.Action("Info")
});
}
-
+
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Export()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!_customerSettings.AllowUsersToExportData)
return Challenge();
@@ -648,11 +641,9 @@ namespace Grand.Web.Controllers
#region My account / Addresses
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Addresses()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var model = await _mediator.Send(new GetAddressList {
Customer = _workContext.CurrentCustomer,
Language = _workContext.WorkingLanguage,
@@ -663,34 +654,31 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> AddressDelete(string addressId)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var customer = _workContext.CurrentCustomer;
//find address (ensure that it belongs to the current customer)
var address = customer.Addresses.FirstOrDefault(a => a.Id == addressId);
if (address == null)
- return Json(new {
+ return Json(new
+ {
redirect = Url.RouteUrl("CustomerAddresses")
});
customer.RemoveAddress(address);
await _customerService.DeleteAddress(address, customer.Id);
- return Json(new {
+ return Json(new
+ {
redirect = Url.RouteUrl("CustomerAddresses")
});
}
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> AddressAdd()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
- var countries =
- await _countryService.GetAllCountries(_workContext.WorkingLanguage.Id, _workContext.CurrentStore.Id);
+ var countries = await _countryService.GetAllCountries(_workContext.WorkingLanguage.Id, _workContext.CurrentStore.Id);
var model = new CustomerAddressEditModel {
Address = await _mediator.Send(new GetAddressModel {
Language = _workContext.WorkingLanguage,
@@ -709,19 +697,16 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> AddressAdd(CustomerAddressEditModel model,
[FromServices] AddressSettings addressSettings)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var customer = _workContext.CurrentCustomer;
if (ModelState.IsValid)
{
var address = model.Address.ToEntity(_workContext.CurrentCustomer, addressSettings);
- address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes });
+ address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.Address.SelectedAttributes });
address.CreatedOnUtc = DateTime.UtcNow;
customer.Addresses.Add(address);
@@ -740,18 +725,15 @@ namespace Grand.Web.Controllers
ExcludeProperties = true,
Customer = _workContext.CurrentCustomer,
LoadCountries = () => countries,
- OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes })
+ OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.Address.SelectedAttributes })
});
return View(model);
}
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> AddressEdit(string addressId)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var customer = _workContext.CurrentCustomer;
//find address (ensure that it belongs to the current customer)
var address = customer.Addresses.FirstOrDefault(a => a.Id == addressId);
@@ -777,24 +759,21 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> AddressEdit(CustomerAddressEditModel model,
[FromServices] AddressSettings addressSettings)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var customer = _workContext.CurrentCustomer;
//find address (ensure that it belongs to the current customer)
var address = customer.Addresses.FirstOrDefault(a => a.Id == model.Address.Id);
if (address == null)
//address is not found
return RedirectToRoute("CustomerAddresses");
-
+
if (ModelState.IsValid)
{
address = model.Address.ToEntity(address, _workContext.CurrentCustomer, addressSettings);
- address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes });
+ address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.Address.SelectedAttributes });
await _customerService.UpdateAddress(address, customer.Id);
if (customer.BillingAddress?.Id == address.Id)
@@ -816,8 +795,7 @@ namespace Grand.Web.Controllers
ExcludeProperties = true,
Customer = _workContext.CurrentCustomer,
LoadCountries = () => countries,
- OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes })
+ OverrideAttributes = await _mediator.Send(new GetParseCustomAddressAttributes { SelectedAttributes = model.Address.SelectedAttributes })
});
return View(model);
@@ -826,21 +804,22 @@ namespace Grand.Web.Controllers
#endregion
#region My account / Downloadable products
+
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> DownloadableProducts()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_customerSettings.HideDownloadableProductsTab)
return RedirectToRoute("CustomerInfo");
var model = await _mediator.Send(new GetDownloadableProducts {
- Customer = _workContext.CurrentCustomer, Store = _workContext.CurrentStore,
+ Customer = _workContext.CurrentCustomer,
+ Store = _workContext.CurrentStore,
Language = _workContext.WorkingLanguage
});
return View(model);
}
+
[HttpGet]
public virtual async Task<IActionResult> UserAgreement(Guid orderItemId)
{
@@ -852,14 +831,11 @@ namespace Grand.Web.Controllers
#region My account / Change password
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> ChangePassword()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var model = new ChangePasswordModel {
- PasswordIsExpired = await _mediator.Send(new GetPasswordIsExpiredQuery
- { Customer = _workContext.CurrentCustomer })
+ PasswordIsExpired = await _mediator.Send(new GetPasswordIsExpiredQuery { Customer = _workContext.CurrentCustomer })
};
return View(model);
@@ -867,11 +843,9 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> ChangePassword(ChangePasswordModel model)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!ModelState.IsValid) return View(model);
var changePasswordRequest = new ChangePasswordRequest(_workContext.CurrentCustomer.Email,
@@ -890,11 +864,9 @@ namespace Grand.Web.Controllers
#region My account / Delete account
[HttpGet]
- public virtual async Task<IActionResult> DeleteAccount()
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
+ public virtual IActionResult DeleteAccount()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!_customerSettings.AllowUsersToDeleteAccount)
return RedirectToRoute("CustomerInfo");
@@ -905,11 +877,9 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> DeleteAccount(DeleteAccountModel model)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!_customerSettings.AllowUsersToDeleteAccount)
return RedirectToRoute("CustomerInfo");
@@ -917,7 +887,8 @@ namespace Grand.Web.Controllers
//delete account
await _mediator.Send(new DeleteAccountCommand {
- Customer = _workContext.CurrentCustomer, Store = _workContext.CurrentStore,
+ Customer = _workContext.CurrentCustomer,
+ Store = _workContext.CurrentStore,
IpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString()
});
@@ -935,16 +906,13 @@ namespace Grand.Web.Controllers
#region My account / Auctions
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Auctions()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_customerSettings.HideAuctionsTab)
return RedirectToRoute("CustomerInfo");
- var model = await _mediator.Send(new GetAuctions
- { Customer = _workContext.CurrentCustomer, Language = _workContext.WorkingLanguage });
+ var model = await _mediator.Send(new GetAuctions { Customer = _workContext.CurrentCustomer, Language = _workContext.WorkingLanguage });
return View(model);
}
@@ -953,11 +921,9 @@ namespace Grand.Web.Controllers
#region My account / Notes
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Notes()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_customerSettings.HideNotesTab)
return RedirectToRoute("CustomerInfo");
@@ -970,11 +936,9 @@ namespace Grand.Web.Controllers
#region My account / Documents
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Documents(DocumentPagingModel command)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_customerSettings.HideDocumentsTab)
return RedirectToRoute("CustomerInfo");
@@ -991,16 +955,13 @@ namespace Grand.Web.Controllers
#region My account / Reviews
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Reviews()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_customerSettings.HideReviewsTab)
return RedirectToRoute("CustomerInfo");
- var model = await _mediator.Send(new GetReviews
- { Customer = _workContext.CurrentCustomer, Language = _workContext.WorkingLanguage });
+ var model = await _mediator.Send(new GetReviews { Customer = _workContext.CurrentCustomer, Language = _workContext.WorkingLanguage });
return View(model);
}
@@ -1010,11 +971,9 @@ namespace Grand.Web.Controllers
#region My account / TwoFactorAuth
[IgnoreApi]
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public async Task<IActionResult> EnableTwoFactorAuthenticator()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!_customerSettings.TwoFactorAuthenticationEnabled)
return RedirectToRoute("CustomerInfo");
@@ -1030,13 +989,11 @@ namespace Grand.Web.Controllers
}
[IgnoreApi]
[HttpPost]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public async Task<IActionResult> EnableTwoFactorAuthenticator(
CustomerInfoModel.TwoFactorAuthenticationModel model,
[FromServices] ITwoFactorAuthenticationService twoFactorAuthenticationService)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!_customerSettings.TwoFactorAuthenticationEnabled)
return RedirectToRoute("CustomerInfo");
@@ -1071,11 +1028,9 @@ namespace Grand.Web.Controllers
}
[IgnoreApi]
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public async Task<IActionResult> DisableTwoFactorAuthenticator()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!_customerSettings.TwoFactorAuthenticationEnabled)
return RedirectToRoute("CustomerInfo");
@@ -1096,13 +1051,11 @@ namespace Grand.Web.Controllers
[IgnoreApi]
[HttpPost]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public async Task<IActionResult> DisableTwoFactorAuthenticator(
CustomerInfoModel.TwoFactorAuthorizationModel model,
[FromServices] ITwoFactorAuthenticationService twoFactorAuthenticationService)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!_customerSettings.TwoFactorAuthenticationEnabled)
return RedirectToRoute("CustomerInfo");
@@ -1143,16 +1096,13 @@ namespace Grand.Web.Controllers
#region My account / Courses
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Courses()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_customerSettings.HideCoursesTab)
return RedirectToRoute("CustomerInfo");
- var model = await _mediator.Send(new GetCourses
- { Customer = _workContext.CurrentCustomer, Store = _workContext.CurrentStore });
+ var model = await _mediator.Send(new GetCourses { Customer = _workContext.CurrentCustomer, Store = _workContext.CurrentStore });
return View(model);
}
@@ -1161,11 +1111,9 @@ namespace Grand.Web.Controllers
#region My account / Sub accounts
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> SubAccounts()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!await _groupService.IsOwner(_workContext.CurrentCustomer))
return Challenge();
@@ -1177,11 +1125,9 @@ namespace Grand.Web.Controllers
return View(model);
}
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> SubAccountAdd()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!await _groupService.IsOwner(_workContext.CurrentCustomer))
return Challenge();
@@ -1193,11 +1139,9 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> SubAccountAdd(SubAccountCreateModel model)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!await _groupService.IsOwner(_workContext.CurrentCustomer))
return Challenge();
@@ -1212,27 +1156,22 @@ namespace Grand.Web.Controllers
return RedirectToRoute("CustomerSubAccounts");
}
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> SubAccountEdit(string id)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!await _groupService.IsOwner(_workContext.CurrentCustomer))
return Challenge();
- var model = await _mediator.Send(new GetSubAccount
- { CustomerId = id, CurrentCustomer = _workContext.CurrentCustomer });
+ var model = await _mediator.Send(new GetSubAccount { CustomerId = id, CurrentCustomer = _workContext.CurrentCustomer });
return View(model);
}
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> SubAccountEdit(SubAccountEditModel model)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!await _groupService.IsOwner(_workContext.CurrentCustomer))
return Challenge();
@@ -1249,11 +1188,9 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> SubAccountDelete(string id)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!await _groupService.IsOwner(_workContext.CurrentCustomer))
return Challenge();
@@ -1264,7 +1201,8 @@ namespace Grand.Web.Controllers
if (result.success)
{
- return Json(new {
+ return Json(new
+ {
redirect = Url.RouteUrl("CustomerSubAccounts"),
success = true
});
@@ -1272,7 +1210,8 @@ namespace Grand.Web.Controllers
//errors
ModelState.AddModelError("", result.error);
- return Json(new {
+ return Json(new
+ {
redirect = Url.RouteUrl("CustomerSubAccounts"),
success = false,
error = string.Join("; ", ModelState.Values
diff --git a/src/Web/Grand.Web/Controllers/OrderController.cs b/src/Web/Grand.Web/Controllers/OrderController.cs
index d745b9e8b..2b675bc00 100644
--- a/src/Web/Grand.Web/Controllers/OrderController.cs
+++ b/src/Web/Grand.Web/Controllers/OrderController.cs
@@ -20,6 +20,8 @@ using Grand.Web.Features.Models.Orders;
using Grand.Web.Models.Orders;
using MediatR;
using Microsoft.AspNetCore.Mvc;
+using Grand.Web.Common.Security.Authorization;
+using Grand.Domain.Customers;
namespace Grand.Web.Controllers
{
@@ -82,11 +84,9 @@ namespace Grand.Web.Controllers
//My account / Orders
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> CustomerOrders(OrderPagingModel command)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var model = await _mediator.Send(new GetCustomerOrderList {
Customer = _workContext.CurrentCustomer,
Language = _workContext.WorkingLanguage,
@@ -252,11 +252,9 @@ namespace Grand.Web.Controllers
//My account / Loyalty points
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> CustomerLoyaltyPoints([FromServices] LoyaltyPointsSettings loyaltyPointsSettings)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (!loyaltyPointsSettings.Enabled)
return RedirectToRoute("CustomerInfo");
diff --git a/src/Web/Grand.Web/Controllers/VendorController.cs b/src/Web/Grand.Web/Controllers/VendorController.cs
index 2bd7ecafd..5a2620f36 100644
--- a/src/Web/Grand.Web/Controllers/VendorController.cs
+++ b/src/Web/Grand.Web/Controllers/VendorController.cs
@@ -6,6 +6,7 @@ using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Core.Interfaces.Messages;
using Grand.Business.Core.Interfaces.Storage;
using Grand.Domain.Common;
+using Grand.Domain.Customers;
using Grand.Domain.Localization;
using Grand.Domain.Seo;
using Grand.Domain.Vendors;
@@ -15,6 +16,7 @@ using Grand.Web.Commands.Models.Vendors;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Extensions;
using Grand.Web.Common.Filters;
+using Grand.Web.Common.Security.Authorization;
using Grand.Web.Common.Security.Captcha;
using Grand.Web.Extensions;
using Grand.Web.Features.Models.Common;
@@ -96,14 +98,12 @@ namespace Grand.Web.Controllers
#region Methods
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> ApplyVendor()
{
if (!_vendorSettings.AllowCustomersToApplyForVendorAccount)
return RedirectToRoute("HomePage");
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var model = new ApplyVendorModel();
if (!string.IsNullOrEmpty(_workContext.CurrentCustomer.VendorId))
{
@@ -134,14 +134,12 @@ namespace Grand.Web.Controllers
[HttpPost, ActionName("ApplyVendor")]
[AutoValidateAntiforgeryToken]
[DenySystemAccount]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> ApplyVendorSubmit(ApplyVendorModel model, IFormFile uploadedFile)
{
if (!_vendorSettings.AllowCustomersToApplyForVendorAccount)
return RedirectToRoute("HomePage");
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
var contentType = string.Empty;
byte[] vendorPictureBinary = null;
@@ -227,12 +225,11 @@ namespace Grand.Web.Controllers
});
return View(model);
}
+
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Info()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_workContext.CurrentVendor == null || !_vendorSettings.AllowVendorsToEditInfo)
return RedirectToRoute("CustomerInfo");
@@ -257,11 +254,9 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> Info(VendorInfoModel model, IFormFile uploadedFile)
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_workContext.CurrentVendor == null || !_vendorSettings.AllowVendorsToEditInfo)
return RedirectToRoute("CustomerInfo");
@@ -334,11 +329,9 @@ namespace Grand.Web.Controllers
}
[HttpGet]
+ [CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> RemovePicture()
{
- if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
- return Challenge();
-
if (_workContext.CurrentVendor == null || !_vendorSettings.AllowVendorsToEditInfo)
return RedirectToRoute("CustomerInfo");