mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
Added base Admin area.
Added direct routing for error view pages to fix them not loading correctly. Added handling of authorized, but invalid roles.
This commit is contained in:
parent
609dbb59eb
commit
f472c4f5d7
52
Teknik/Areas/Admin/AdminAreaRegistration.cs
Normal file
52
Teknik/Areas/Admin/AdminAreaRegistration.cs
Normal file
@ -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
Teknik/Areas/Admin/Controllers/AdminController.cs
Normal file
28
Teknik/Areas/Admin/Controllers/AdminController.cs
Normal file
@ -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
Teknik/Areas/Admin/Scripts/Search.js
Normal file
1
Teknik/Areas/Admin/Scripts/Search.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
12
Teknik/Areas/Admin/ViewModels/DashboardViewModel.cs
Normal file
12
Teknik/Areas/Admin/ViewModels/DashboardViewModel.cs
Normal file
@ -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
Teknik/Areas/Admin/ViewModels/SearchResultViewModel.cs
Normal file
13
Teknik/Areas/Admin/ViewModels/SearchResultViewModel.cs
Normal file
@ -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
Teknik/Areas/Admin/ViewModels/SearchViewModel.cs
Normal file
12
Teknik/Areas/Admin/ViewModels/SearchViewModel.cs
Normal file
@ -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
Teknik/Areas/Admin/Views/Admin/Dashboard.cshtml
Normal file
11
Teknik/Areas/Admin/Views/Admin/Dashboard.cshtml
Normal file
@ -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
Teknik/Areas/Admin/Views/Admin/Search.cshtml
Normal file
25
Teknik/Areas/Admin/Views/Admin/Search.cshtml
Normal file
@ -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
Teknik/Areas/Admin/Views/Admin/SearchResult.cshtml
Normal file
7
Teknik/Areas/Admin/Views/Admin/SearchResult.cshtml
Normal file
@ -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
Teknik/Areas/Admin/Views/Admin/SearchResults.cshtml
Normal file
6
Teknik/Areas/Admin/Views/Admin/SearchResults.cshtml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
@model List<Teknik.Areas.Admin.ViewModels.SearchResultViewModel>
|
||||||
|
|
||||||
|
@foreach (var post in Model)
|
||||||
|
{
|
||||||
|
@Html.Partial("SearchResult", post)
|
||||||
|
}
|
3
Teknik/Areas/Admin/Views/_ViewStart.cshtml
Normal file
3
Teknik/Areas/Admin/Views/_ViewStart.cshtml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
@{
|
||||||
|
Layout = "~/Views/Shared/_Layout.cshtml";
|
||||||
|
}
|
36
Teknik/Areas/Admin/Views/web.config
Normal file
36
Teknik/Areas/Admin/Views/web.config
Normal file
@ -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>
|
@ -27,7 +27,7 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
ErrorViewModel model = new ErrorViewModel();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View(model);
|
return View("/Areas/Error/Views/Error/Exception.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TrackPageView]
|
[TrackPageView]
|
||||||
@ -47,7 +47,7 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
model.Description = exception.Message;
|
model.Description = exception.Message;
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View(model);
|
return View("/Areas/Error/Views/Error/General.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
@ -65,7 +65,7 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
ErrorViewModel model = new ErrorViewModel();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View(model);
|
return View("/Areas/Error/Views/Error/Http403.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
@ -83,7 +83,7 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
ErrorViewModel model = new ErrorViewModel();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View(model);
|
return View("/Areas/Error/Views/Error/Http404.cshtml", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TrackPageView]
|
[TrackPageView]
|
||||||
@ -102,7 +102,7 @@ namespace Teknik.Areas.Error.Controllers
|
|||||||
ErrorViewModel model = new ErrorViewModel();
|
ErrorViewModel model = new ErrorViewModel();
|
||||||
model.Exception = exception;
|
model.Exception = exception;
|
||||||
|
|
||||||
return View(model);
|
return View("/Areas/Error/Views/Error/Http500.cshtml", model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -22,7 +22,7 @@
|
|||||||
@if (User.IsInRole("Admin"))
|
@if (User.IsInRole("Admin"))
|
||||||
{
|
{
|
||||||
<li>
|
<li>
|
||||||
<a href="@Url.SubRouteUrl("admin", "Admin.Index")">Administration</a>
|
<a href="@Url.SubRouteUrl("admin", "Admin.Dashboard")">Administration</a>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
<li>
|
<li>
|
||||||
|
@ -4,11 +4,13 @@ using System.Linq;
|
|||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using System.Web.Routing;
|
using System.Web.Routing;
|
||||||
|
using Teknik.Areas.Error.Controllers;
|
||||||
|
using Teknik.Helpers;
|
||||||
using Teknik.Areas.Users.Controllers;
|
using Teknik.Areas.Users.Controllers;
|
||||||
|
|
||||||
namespace Teknik.Attributes
|
namespace Teknik.Attributes
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
|
||||||
public class TeknikAuthorizeAttribute : AuthorizeAttribute
|
public class TeknikAuthorizeAttribute : AuthorizeAttribute
|
||||||
{
|
{
|
||||||
public override void OnAuthorization(AuthorizationContext filterContext)
|
public override void OnAuthorization(AuthorizationContext filterContext)
|
||||||
@ -56,8 +58,8 @@ namespace Teknik.Attributes
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// uh oh, let's handle it the old way
|
// uh oh, they are authorized, but don't have access. ABORT ABORT ABORT
|
||||||
base.HandleUnauthorizedRequest(filterContext);
|
HandleInvalidAuthRequest(filterContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,6 +78,21 @@ namespace Teknik.Attributes
|
|||||||
filterContext.Result = new HttpUnauthorizedResult();
|
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)
|
private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
|
||||||
{
|
{
|
||||||
validationStatus = base.OnCacheAuthorization(new HttpContextWrapper(context));
|
validationStatus = base.OnCacheAuthorization(new HttpContextWrapper(context));
|
||||||
|
Binary file not shown.
@ -183,6 +183,11 @@
|
|||||||
<Compile Include="Areas\About\AboutAreaRegistration.cs" />
|
<Compile Include="Areas\About\AboutAreaRegistration.cs" />
|
||||||
<Compile Include="Areas\About\Controllers\AboutController.cs" />
|
<Compile Include="Areas\About\Controllers\AboutController.cs" />
|
||||||
<Compile Include="Areas\About\ViewModels\AboutViewModel.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\APIAreaRegistration.cs" />
|
||||||
<Compile Include="Areas\API\Controllers\APIController.cs" />
|
<Compile Include="Areas\API\Controllers\APIController.cs" />
|
||||||
<Compile Include="Areas\API\Controllers\APIv1Controller.cs" />
|
<Compile Include="Areas\API\Controllers\APIv1Controller.cs" />
|
||||||
@ -338,6 +343,7 @@
|
|||||||
<Content Include="App_Data\reservedUsernames.txt">
|
<Content Include="App_Data\reservedUsernames.txt">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<Content Include="Areas\Admin\Scripts\Search.js" />
|
||||||
<Content Include="Areas\Blog\Content\Blog.css" />
|
<Content Include="Areas\Blog\Content\Blog.css" />
|
||||||
<Content Include="Areas\Blog\Scripts\Blog.js" />
|
<Content Include="Areas\Blog\Scripts\Blog.js" />
|
||||||
<Content Include="Areas\Contact\Scripts\Contact.js" />
|
<Content Include="Areas\Contact\Scripts\Contact.js" />
|
||||||
@ -574,6 +580,12 @@
|
|||||||
<Content Include="Areas\Blog\Views\Blog\EditPost.cshtml" />
|
<Content Include="Areas\Blog\Views\Blog\EditPost.cshtml" />
|
||||||
<Content Include="Areas\Help\Views\Help\Markdown.cshtml" />
|
<Content Include="Areas\Help\Views\Help\Markdown.cshtml" />
|
||||||
<Content Include="Areas\Help\Views\Help\Tools.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 Dev.pubxml" />
|
||||||
<None Include="Properties\PublishProfiles\Teknik Production.pubxml" />
|
<None Include="Properties\PublishProfiles\Teknik Production.pubxml" />
|
||||||
<None Include="Scripts\jquery-2.1.4.intellisense.js" />
|
<None Include="Scripts\jquery-2.1.4.intellisense.js" />
|
||||||
@ -646,6 +658,8 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Areas\About\Models\" />
|
<Folder Include="Areas\About\Models\" />
|
||||||
<Folder Include="Areas\About\Views\Shared\" />
|
<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\Models\" />
|
||||||
<Folder Include="Areas\API\Views\APIv1\" />
|
<Folder Include="Areas\API\Views\APIv1\" />
|
||||||
<Folder Include="Areas\API\Views\API\" />
|
<Folder Include="Areas\API\Views\API\" />
|
||||||
|
Loading…
Reference in New Issue
Block a user