Created
March 22, 2011 01:41
-
-
Save rauhryan/880606 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using FieldBook.Core.Domain; | |
using FieldBook.Core.Navigation; | |
using FieldBook.Core.Persistence; | |
using FieldBook.Interface.Actions.Permissions; | |
using FieldBook.Interface.Dtos; | |
using FubuMVC.Core; | |
using FubuMVC.Core.Diagnostics; | |
using FubuMVC.Core.Diagnostics.HtmlWriting; | |
using FubuMVC.Core.Diagnostics.TextWriting; | |
using FubuMVC.Core.Registration; | |
using FubuMVC.Core.Registration.Nodes; | |
using FubuMVC.Core.UI.Navigation; | |
using FubuMVC.Core.Urls; | |
using FubuMVC.Core.UI.Tags; | |
using HtmlTags; | |
using IContainer = StructureMap.IContainer; | |
namespace FieldBook.Interface.Actions.Diagnostics | |
{ | |
[FubuDiagnostics("Fieldbook")] | |
public class FieldbookDiagnosticsWriter | |
{ | |
private readonly IUrlRegistry _urlRegistry; | |
private readonly ICrudService<Permission> _permissionService; | |
private readonly BehaviorGraph _behaviorGraph; | |
private readonly TagGenerator<PermissionModel> _tagGenerator; | |
private readonly IContainer _container; | |
public FieldbookDiagnosticsWriter(IUrlRegistry urlRegistry, | |
ICrudService<Permission> permissionService, | |
BehaviorGraph behaviorGraph, | |
TagGenerator<PermissionModel> tagGenerator, | |
IContainer container) | |
{ | |
_urlRegistry = urlRegistry; | |
_permissionService = permissionService; | |
_behaviorGraph = behaviorGraph; | |
_tagGenerator = tagGenerator; | |
_container = container; | |
} | |
[UrlPattern("_fubu/permissions"), Description("View registered permissions and what chains they apply to.")] | |
public HtmlDocument Permissions() | |
{ | |
var linkList = new HtmlTag("ul"); | |
var categories = _permissionService.FindAll() | |
.OrderBy(x => x.Name) | |
.AsEnumerable() | |
.GroupBy(x => x.Category); | |
foreach (var category in categories) | |
{ | |
linkList.Child(new HtmlTag("li").Text(category.Key)); | |
var parentList = new HtmlTag("ul"); | |
category.Each(x => parentList.Child(new HtmlTag("li") | |
.Child(new LinkTag(x.Description, "permissions/" + x.Id)))); | |
linkList.Child(parentList); | |
} | |
return DiagnosticHtml.BuildDocument(_urlRegistry, "Registered Permissions", linkList); | |
} | |
[UrlPattern("_fubu/containerContents"), Description("StructureMap: WhatDoIHave?")] | |
public string WhatDoIHave() | |
{ | |
return _container.WhatDoIHave(); | |
} | |
[UrlPattern("_fubu/permissions/{Id}")] | |
public HtmlDocument Permissions(DiagnosePermissionRequest request) | |
{ | |
var permission = _permissionService.Retrieve(request.Id); | |
var chains = _behaviorGraph.Behaviors | |
.Where(x => x.HasInput() && x.InputType().Name.StartsWith(permission.Name)); | |
if(chains.Count() == 0) | |
return DiagnosticHtml.BuildDocument(_urlRegistry,"Invalid permission key", new HtmlTag("span").Text("No behavior chain registered with permission: " + permission.Name)); | |
var table = BehaviorGraphWriter.WriteBehaviorChainTable(chains, new LinkToChain(), new RouteColumn(), new ActionColumn()); | |
var doc = DiagnosticHtml.BuildDocument(_urlRegistry, string.Format("Chains affected by permission {0}", permission.Description), table); | |
return doc; | |
} | |
[UrlPattern("_fubu/nopermissions"), Description("View behavior chains without permissions.")] | |
public HtmlDocument ChainsWithoutPermissions() | |
{ | |
var permissions = _permissionService.FindAll().AsEnumerable(); | |
var chains = _behaviorGraph | |
.Behaviors | |
.Where(x => x.HasInput() && permissions.All(permission => !x.InputTypeName.StartsWith(permission.Name))) | |
.OrderBy(x => x.RoutePattern); | |
var table = BehaviorGraphWriter.WriteBehaviorChainTable(chains, new LinkToChain(), new RouteColumn(), new ActionColumn(), new ChainPermission()); | |
var doc = DiagnosticHtml.BuildDocument(_urlRegistry, "Chains not affected by any permissions", table); | |
return doc; | |
} | |
} | |
public class DiagnosePermissionRequest | |
{ | |
[RouteInput] | |
public Guid Id { get; set; } | |
} | |
public class LinkToChain : IColumn | |
{ | |
public string Header() | |
{ | |
return "Chain"; | |
} | |
public void WriteBody(BehaviorChain chain, HtmlTag row, HtmlTag cell) | |
{ | |
cell.Child(new LinkTag(Text(chain), "/_fubu/chain/" + chain.UniqueId).AddClass("chainId")); | |
} | |
public string Text(BehaviorChain chain) | |
{ | |
return chain.UniqueId.ToString(); | |
} | |
} | |
public class ChainPermission : IColumn | |
{ | |
public string Header() | |
{ | |
return ""; | |
} | |
public void WriteBody(BehaviorChain chain, HtmlTag row, HtmlTag cell) | |
{ | |
cell.Child(new LinkTag("Create permission", "/_fubu/permissionform/" + chain.UniqueId)); | |
} | |
public string Text(BehaviorChain chain) | |
{ | |
return "Add"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment