瀏覽代碼

Added base Admin area.

Added direct routing for error view pages to fix them not loading correctly.
Added handling of authorized, but invalid roles.
tags/2.0.6
Teknikode 3 年之前
父節點
當前提交
f472c4f5d7

+ 52
- 0
Teknik/Areas/Admin/AdminAreaRegistration.cs 查看文件

@@ -0,0 +1,52 @@
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Optimization;
using Teknik.Configuration;
using Teknik.Helpers;

namespace Teknik.Areas.Admin
{
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
Config config = Config.Load();
context.MapSubdomainRoute(
"Admin.Dashboard", // Route name
new List<string>() { "admin" }, // Subdomains
new List<string>() { config.Host },
"", // URL with parameters
new { controller = "Admin", action = "Dashboard", username = string.Empty }, // Parameter defaults
new[] { typeof(Controllers.AdminController).Namespace }
);
context.MapSubdomainRoute(
"Admin.Search", // Route name
new List<string>() { "admin" }, // Subdomains
new List<string>() { config.Host },
"Search", // URL with parameters
new { controller = "Admin", action = "Search", username = string.Empty }, // Parameter defaults
new[] { typeof(Controllers.AdminController).Namespace }
);
context.MapSubdomainRoute(
"Admin.Action", // Route name
new List<string>() { "blog" }, // Subdomains
new List<string>() { config.Host },
"Action/{controller}/{action}", // URL with parameters
new { controller = "Admin", action = "Dashboard" }, // Parameter defaults
new[] { typeof(Controllers.AdminController).Namespace }
);

// Register Script Bundles
BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/Search", config.CdnHost).Include(
"~/Areas/Admin/Scripts/Search.js"));
}
}
}

+ 28
- 0
Teknik/Areas/Admin/Controllers/AdminController.cs 查看文件

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teknik.Areas.Admin.ViewModels;
using Teknik.Attributes;
using Teknik.Controllers;
using Teknik.ViewModels;

namespace Teknik.Areas.Admin.Controllers
{
[TeknikAuthorize(Roles = "Admin")]
public class AdminController : DefaultController
{
public ActionResult Dashboard()
{
DashboardViewModel model = new DashboardViewModel();
return View(model);
}

public ActionResult Search()
{
SearchViewModel model = new SearchViewModel();
return View(model);
}
}
}

+ 1
- 0
Teknik/Areas/Admin/Scripts/Search.js 查看文件

@@ -0,0 +1 @@


+ 12
- 0
Teknik/Areas/Admin/ViewModels/DashboardViewModel.cs 查看文件

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Teknik.ViewModels;

namespace Teknik.Areas.Admin.ViewModels
{
public class DashboardViewModel : ViewModelBase
{
}
}

+ 13
- 0
Teknik/Areas/Admin/ViewModels/SearchResultViewModel.cs 查看文件

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Teknik.ViewModels;

namespace Teknik.Areas.Admin.ViewModels
{
public class SearchResultViewModel : ViewModelBase
{
public string Username { get; set; }
}
}

+ 12
- 0
Teknik/Areas/Admin/ViewModels/SearchViewModel.cs 查看文件

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Teknik.ViewModels;

namespace Teknik.Areas.Admin.ViewModels
{
public class SearchViewModel : ViewModelBase
{
}
}

+ 11
- 0
Teknik/Areas/Admin/Views/Admin/Dashboard.cshtml 查看文件

@@ -0,0 +1,11 @@
@model Teknik.Areas.Admin.ViewModels.DashboardViewModel

@using Teknik.Helpers

<div class="container">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<a href="@Url.SubRouteUrl("admin", "Admin.Search")">Search</a>
</div>
</div>
</div>

+ 25
- 0
Teknik/Areas/Admin/Views/Admin/Search.cshtml 查看文件

@@ -0,0 +1,25 @@
@model Teknik.Areas.Admin.ViewModels.SearchViewModel

@using Teknik.Helpers

<script>
// We need to define the action URLs for the script
var searchResultsURL = '@Url.SubRouteUrl("admin", "Admin.Action")';
</script>

@Scripts.Render("~/bundles/Search")

<div class="container">
<div class="row">
<form class="form-inline">
<div class="form-group">
<input type="text" class="form-control" id="Query" name="Query" placeholder="Username" />
</div>
</form>
</div>
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<div class="results" id="results"></div>
</div>
</div>
</div>

+ 7
- 0
Teknik/Areas/Admin/Views/Admin/SearchResult.cshtml 查看文件

@@ -0,0 +1,7 @@
@model Teknik.Areas.Admin.ViewModels.SearchResultViewModel

<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<a href="@Url.SubRouteUrl("admin", "Admin.UserInfo", new { username = Model.Username })">@Model.Username</a>
</div>
</div>

+ 6
- 0
Teknik/Areas/Admin/Views/Admin/SearchResults.cshtml 查看文件

@@ -0,0 +1,6 @@
@model List<Teknik.Areas.Admin.ViewModels.SearchResultViewModel>

@foreach (var post in Model)
{
@Html.Partial("SearchResult", post)
}

+ 3
- 0
Teknik/Areas/Admin/Views/_ViewStart.cshtml 查看文件

@@ -0,0 +1,3 @@
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

+ 36
- 0
Teknik/Areas/Admin/Views/web.config 查看文件

@@ -0,0 +1,36 @@
<?xml version="1.0"?>

<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization" />
<add namespace="Teknik" />
</namespaces>
</pages>
</system.web.webPages.razor>

<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>

<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>

+ 5
- 5
Teknik/Areas/Error/Controllers/ErrorController.cs 查看文件

@@ -27,7 +27,7 @@ namespace Teknik.Areas.Error.Controllers
ErrorViewModel model = new ErrorViewModel();
model.Exception = exception;

return View(model);
return View("/Areas/Error/Views/Error/Exception.cshtml", model);
}

[TrackPageView]
@@ -47,7 +47,7 @@ namespace Teknik.Areas.Error.Controllers
model.Description = exception.Message;
model.Exception = exception;

return View(model);
return View("/Areas/Error/Views/Error/General.cshtml", model);
}
[AllowAnonymous]
@@ -65,7 +65,7 @@ namespace Teknik.Areas.Error.Controllers
ErrorViewModel model = new ErrorViewModel();
model.Exception = exception;

return View(model);
return View("/Areas/Error/Views/Error/Http403.cshtml", model);
}
[AllowAnonymous]
@@ -83,7 +83,7 @@ namespace Teknik.Areas.Error.Controllers
ErrorViewModel model = new ErrorViewModel();
model.Exception = exception;

return View(model);
return View("/Areas/Error/Views/Error/Http404.cshtml", model);
}

[TrackPageView]
@@ -102,7 +102,7 @@ namespace Teknik.Areas.Error.Controllers
ErrorViewModel model = new ErrorViewModel();
model.Exception = exception;

return View(model);
return View("/Areas/Error/Views/Error/Http500.cshtml", model);
}
}
}

+ 1
- 1
Teknik/Areas/User/Views/User/_LoginPartial.cshtml 查看文件

@@ -22,7 +22,7 @@
@if (User.IsInRole("Admin"))
{
<li>
<a href="@Url.SubRouteUrl("admin", "Admin.Index")">Administration</a>
<a href="@Url.SubRouteUrl("admin", "Admin.Dashboard")">Administration</a>
</li>
}
<li>

+ 20
- 3
Teknik/Attributes/TeknikAuthorizeAttribute.cs 查看文件

@@ -4,11 +4,13 @@ using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Teknik.Areas.Error.Controllers;
using Teknik.Helpers;
using Teknik.Areas.Users.Controllers;

namespace Teknik.Attributes
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class TeknikAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
@@ -56,8 +58,8 @@ namespace Teknik.Attributes
}
else
{
// uh oh, let's handle it the old way
base.HandleUnauthorizedRequest(filterContext);
// uh oh, they are authorized, but don't have access. ABORT ABORT ABORT
HandleInvalidAuthRequest(filterContext);
}
}

@@ -76,6 +78,21 @@ namespace Teknik.Attributes
filterContext.Result = new HttpUnauthorizedResult();
}

protected void HandleInvalidAuthRequest(AuthorizationContext filterContext)
{
// auth failed, redirect to login page
var request = filterContext.HttpContext.Request;
string redirectUrl = (request.Url != null) ? filterContext.HttpContext.Request.Url.AbsoluteUri.ToString() : string.Empty;

var errorController = new ErrorController();
if (errorController != null)
{
filterContext.Result = errorController.Http403(new Exception("Not Authorized"));
return;
}
filterContext.Result = new HttpUnauthorizedResult();
}

private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
{
validationStatus = base.OnCacheAuthorization(new HttpContextWrapper(context));

二進制
Teknik/Scripts/_references.js 查看文件


+ 14
- 0
Teknik/Teknik.csproj 查看文件

@@ -183,6 +183,11 @@
<Compile Include="Areas\About\AboutAreaRegistration.cs" />
<Compile Include="Areas\About\Controllers\AboutController.cs" />
<Compile Include="Areas\About\ViewModels\AboutViewModel.cs" />
<Compile Include="Areas\Admin\AdminAreaRegistration.cs" />
<Compile Include="Areas\Admin\Controllers\AdminController.cs" />
<Compile Include="Areas\Admin\ViewModels\DashboardViewModel.cs" />
<Compile Include="Areas\Admin\ViewModels\SearchResultViewModel.cs" />
<Compile Include="Areas\Admin\ViewModels\SearchViewModel.cs" />
<Compile Include="Areas\API\APIAreaRegistration.cs" />
<Compile Include="Areas\API\Controllers\APIController.cs" />
<Compile Include="Areas\API\Controllers\APIv1Controller.cs" />
@@ -338,6 +343,7 @@
<Content Include="App_Data\reservedUsernames.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Areas\Admin\Scripts\Search.js" />
<Content Include="Areas\Blog\Content\Blog.css" />
<Content Include="Areas\Blog\Scripts\Blog.js" />
<Content Include="Areas\Contact\Scripts\Contact.js" />
@@ -574,6 +580,12 @@
<Content Include="Areas\Blog\Views\Blog\EditPost.cshtml" />
<Content Include="Areas\Help\Views\Help\Markdown.cshtml" />
<Content Include="Areas\Help\Views\Help\Tools.cshtml" />
<Content Include="Areas\Admin\Views\web.config" />
<Content Include="Areas\Admin\Views\Admin\Search.cshtml" />
<Content Include="Areas\Admin\Views\Admin\Dashboard.cshtml" />
<Content Include="Areas\Admin\Views\Admin\SearchResult.cshtml" />
<Content Include="Areas\Admin\Views\Admin\SearchResults.cshtml" />
<Content Include="Areas\Admin\Views\_ViewStart.cshtml" />
<None Include="Properties\PublishProfiles\Teknik Dev.pubxml" />
<None Include="Properties\PublishProfiles\Teknik Production.pubxml" />
<None Include="Scripts\jquery-2.1.4.intellisense.js" />
@@ -646,6 +658,8 @@
<ItemGroup>
<Folder Include="Areas\About\Models\" />
<Folder Include="Areas\About\Views\Shared\" />
<Folder Include="Areas\Admin\Models\" />
<Folder Include="Areas\Admin\Views\Shared\" />
<Folder Include="Areas\API\Models\" />
<Folder Include="Areas\API\Views\APIv1\" />
<Folder Include="Areas\API\Views\API\" />

Loading…
取消
儲存