Skip to content

Instantly share code, notes, and snippets.

@kwestground
Last active October 26, 2016 12:50
Show Gist options
  • Save kwestground/668092b8179145796c57c166eb2a1279 to your computer and use it in GitHub Desktop.
Save kwestground/668092b8179145796c57c166eb2a1279 to your computer and use it in GitHub Desktop.
PermissionRecord validation attribute for NopCommerce
namespace Nop.Web.Framework.Controllers
{
using System;
using System.Web.Mvc;
using Core.Domain.Security;
using Core.Infrastructure;
using Services.Security;
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple=true)]
public class PermissionRecordValidation : FilterAttribute, IAuthorizationFilter
{
private readonly PermissionRecord _permissionRecord;
public PermissionRecordValidation(PermissionRecord permissionRecord)
{
this._permissionRecord = permissionRecord;
}
public virtual void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
throw new ArgumentNullException(nameof(filterContext));
var permissionService = EngineContext.Current.Resolve<IPermissionService>();
if (!permissionService.Authorize(_permissionRecord))
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment