From 5898bac86871fd9a09a29ec0ea26ad0c4964bafb Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Wed, 26 Apr 2023 12:23:50 +0200 Subject: [PATCH] Latest stable --- .../App_Infrastructure/AuthorizationFilter.cs | 6 ++++- .../App_Infrastructure/ControllerFactory.cs | 22 +++++++++++++++---- .../App_Infrastructure/UserRoles.cs | 1 + .../LaaProductionWeb/App_Start/RouteConfig.cs | 8 +++---- .../Controllers/HomeController.cs | 2 +- .../Controllers/Production/HomeController.cs | 14 ------------ .../LaaProductionWeb/LaaProductionWeb.csproj | 1 - .../LaaProductionWeb/Views/Home/Index.cshtml | 13 +++++------ .../Views/Shared/_Layout.cshtml | 2 +- LaaProductionWeb/LaaProductionWeb/Web.config | 4 +++- 10 files changed, 38 insertions(+), 35 deletions(-) delete mode 100644 LaaProductionWeb/LaaProductionWeb/Controllers/Production/HomeController.cs diff --git a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/AuthorizationFilter.cs b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/AuthorizationFilter.cs index 8569e79f..44ae0ff0 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/AuthorizationFilter.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/AuthorizationFilter.cs @@ -16,7 +16,11 @@ var employee = accountService.FindEmployee(userId); var claims = new List(); - httpContext.User = new ClaimsPrincipal(new ClaimsIdentity()); + httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Name, string.Empty), + new Claim(ClaimTypes.Role, UserRoles.WEB_Anonymous), + }, nameof(Claim), ClaimTypes.Name, ClaimTypes.Role)); if (employee.IsValid) { diff --git a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs index 4e14047a..b8ec58e8 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs @@ -1,5 +1,9 @@ namespace LaaProductionWeb.App_Infrastructure { + using LaaProductionWeb.Controllers; + + using Microsoft.Extensions.DependencyInjection; + using System; using System.Collections.Generic; using System.Linq; @@ -49,13 +53,23 @@ controllerType = Type.GetType(controllerTypeName, throwOnError: false, ignoreCase: true); - this.RedirectToDefault(controllerType, requestContext.HttpContext.Response); + if (controllerType is null) + { + controllerTypeName = controllerTypeName.Replace(innerPath, string.Empty); + controllerType = Type.GetType(controllerTypeName, throwOnError: false, ignoreCase: true); + } - var controller = this.serviceProvider.GetService(controllerType) as IController; + if (controllerType != null) + { + var controller = this.serviceProvider.GetService(controllerType) as IController; - this.RedirectToDefault(controller, requestContext.HttpContext.Response); + if (controller != null) + { + return controller; + } + } - return controller; + return this.serviceProvider.GetService() as IController; } public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) diff --git a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/UserRoles.cs b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/UserRoles.cs index 8088bf08..271952e0 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/UserRoles.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/UserRoles.cs @@ -3,6 +3,7 @@ public class UserRoles { public const string WEB_Admin = nameof(WEB_Admin); + public const string WEB_Anonymous = nameof(WEB_Anonymous); public const string WEB_HeReport = nameof(WEB_HeReport); public const string WEB_PalettenScan = nameof(WEB_PalettenScan); public const string WEB_ProdApproval = nameof(WEB_ProdApproval); diff --git a/LaaProductionWeb/LaaProductionWeb/App_Start/RouteConfig.cs b/LaaProductionWeb/LaaProductionWeb/App_Start/RouteConfig.cs index 78e9a13a..d50aa845 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Start/RouteConfig.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Start/RouteConfig.cs @@ -18,10 +18,10 @@ routes.MapMvcAttributeRoutes(); - routes.MapRoute( - name: "Production", - url: "Production/{controller}/{action}/{id}", - defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); + //routes.MapRoute( + // name: "Production", + // url: "Production/{controller}/{action}/{id}", + // defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); routes.MapRoute( name: "MVC_Default", diff --git a/LaaProductionWeb/LaaProductionWeb/Controllers/HomeController.cs b/LaaProductionWeb/LaaProductionWeb/Controllers/HomeController.cs index d5d3e48d..3d1b50f3 100644 --- a/LaaProductionWeb/LaaProductionWeb/Controllers/HomeController.cs +++ b/LaaProductionWeb/LaaProductionWeb/Controllers/HomeController.cs @@ -35,7 +35,7 @@ if (this.HttpContext.SignIn(userId)) { - return this.Redirect($"~{this.Request.UrlReferrer.LocalPath}"); + return this.RedirectToAction(nameof(Index)); } } diff --git a/LaaProductionWeb/LaaProductionWeb/Controllers/Production/HomeController.cs b/LaaProductionWeb/LaaProductionWeb/Controllers/Production/HomeController.cs deleted file mode 100644 index bf6b8ca8..00000000 --- a/LaaProductionWeb/LaaProductionWeb/Controllers/Production/HomeController.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace LaaProductionWeb.Controllers.Production -{ - using LaaProductionWeb.App_Infrastructure; - - using System.Web.Mvc; - - [AllowAnonymous] - public class HomeController : NestedController - { - [Route("Production/{viewname}/{count}")] - public ActionResult Index(string viewname, int count) - => this.NestedView(); - } -} \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj b/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj index d8c8a97c..3e1d1435 100644 --- a/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj +++ b/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj @@ -170,7 +170,6 @@ - diff --git a/LaaProductionWeb/LaaProductionWeb/Views/Home/Index.cshtml b/LaaProductionWeb/LaaProductionWeb/Views/Home/Index.cshtml index dd3fea0c..f32d4c31 100644 --- a/LaaProductionWeb/LaaProductionWeb/Views/Home/Index.cshtml +++ b/LaaProductionWeb/LaaProductionWeb/Views/Home/Index.cshtml @@ -1,4 +1,6 @@ -
+@using LaaProductionWeb.App_Infrastructure + +
@@ -30,13 +32,8 @@
-
-
-
@this.ViewContext.HttpContext.Session["UserId"]
-
@this.ViewContext.HttpContext.Session["Expires"]
-
-
- @if (!this.User.Identity.IsAuthenticated) + + @if (this.User.IsInRole(UserRoles.WEB_Anonymous)) {
diff --git a/LaaProductionWeb/LaaProductionWeb/Views/Shared/_Layout.cshtml b/LaaProductionWeb/LaaProductionWeb/Views/Shared/_Layout.cshtml index 42c1e1d5..d68fa615 100644 --- a/LaaProductionWeb/LaaProductionWeb/Views/Shared/_Layout.cshtml +++ b/LaaProductionWeb/LaaProductionWeb/Views/Shared/_Layout.cshtml @@ -15,7 +15,7 @@ Laatzen Production Web - @if (this.User.Identity.IsAuthenticated) + @if (!this.User.IsInRole(UserRoles.WEB_Anonymous)) {