Created
March 19, 2014 21:08
-
-
Save scottlaw1/9651350 to your computer and use it in GitHub Desktop.
An example of a program written in LINQPad for code generation of classes used in an ASP.NET MVC project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
string aclTemplates = @" | |
using System; | |
using Acme.Web.Areas.Area1.Controllers.{0}; | |
using Acme.Web.Infrastructure.Acl; | |
namespace Ltss.Web.Areas.Acme.Acl.{0} | |
{{ | |
public class Acme{0}AclRegistry : AbstractAccessControlRegistry<{0}Controller> | |
{{ | |
private readonly Func<Acme{0}DetailsAclHandler> acme{0}DetailsAclHandler; | |
public Acme{0}AclRegistry(Func<Acme{0}DetailsAclHandler> acme{0}DetailsAclHandler) | |
{{ | |
this.acme{0}DetailsAclHandler = acme{0}DetailsAclHandler; | |
}} | |
public override void Register(IAccessControlContainer container) | |
{{ | |
With(container) | |
.ForArea(new AcmeAreaRegistration()) | |
.Register(ctrl => ctrl.Details("""", """", """"), Acme{0}DetailsAclHandler); | |
}} | |
}} | |
public class Acme{0}DetailsAclHandler : IAccessControlHandler | |
{{ | |
private readonly IClaimsIdentityService identityService; | |
private readonly IAcmeAclHelper AcmeAclHelper; | |
public Acme{0}DetailsAclHandler(IClaimsIdentityService identityService, | |
IAcmeAclHelper acmeAclHelper) | |
{{ | |
this.identityService = identityService; | |
this.acmeAclHelper = acmeAclHelper; | |
}} | |
public bool IsAccessible(IResourceContext context) | |
{{ | |
var currentUserRoles = identityService.GetCurrentIdentity().Roles(); | |
if (!acmeAclHelper.IsAcmeApplicable(currentUserRoles)) return true; | |
var clientId = context.ArgumentAs<string>(""clientId""); | |
return AcmeAclHelper.IsAccessible(currentUserRoles, clientId); | |
}} | |
}} | |
}}"; | |
var controllers = new List<string>{"ControllerOne","ControllerTwo","Controller3"}; | |
foreach(var controller in controllers) | |
{ | |
var aclRegistryClass = string.Format(aclTemplates,controller); | |
aclRegistryClass.Dump(); | |
var fileName = string.Format("{0}AclRegistry.cs",controller); | |
File.WriteAllText(fileName,aclRegistryClass); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment