From a9089911be0102ee8b9e2bbe902f193b879a842b Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Wed, 21 Jun 2023 09:50:18 +0200 Subject: [PATCH] Dendency injection sorce abstraction implemented --- .../AccountManager.cs | 10 +++-- .../EmployeesManager.cs | 45 ++++++++++++++++--- .../LaaProduction.Personalization.csproj | 7 ++- .../Models/Employee.cs | 4 ++ .../Models/EmployeeFunction.cs | 9 ++++ .../Models/EmployeeRole.cs | 9 ++++ .../PersonalizationExtensions.cs | 25 +++++------ .../Repositories/EmployeesRepository.cs | 12 ++--- .../Repositories/FunctionsRepository.cs | 12 +++++ .../Interfaces/IFunctionsRepository.cs | 2 + .../Interfaces/IRolesRepository.cs | 2 + .../Repositories/RolesRepository.cs | 12 ++++- .../ServiceProvider.cs | 28 ------------ .../SoftwareManager.cs | 3 +- .../LaaProduction.Search.csproj | 5 ++- .../LaaProduction.Search/SearchExtensions.cs | 14 +++--- .../LaaProduction.Search/SearchManager.cs | 3 +- .../LaaProduction.Search/ServiceProvider.cs | 24 ---------- .../App_Infrastructure/AuthorizationFilter.cs | 6 ++- .../AuthorizeBearerAttribute.cs | 3 +- .../App_Infrastructure/HttpExtensions.cs | 30 +++++++------ .../App_Infrastructure/ServiceProvider.cs | 39 ---------------- .../Controllers/EmployeesController.cs | 5 ++- .../Controllers/SoftwareController.cs | 4 +- .../Views/Employees/Edit.cshtml | 4 +- .../Views/Employees/Index.cshtml | 2 + .../Software/Controllers/AppIdController.cs | 5 ++- .../Controllers/FunctionsController.cs | 7 +-- .../Personalization/EmployeesController.cs | 5 ++- .../API/Personalization/LoginController.cs | 3 +- .../API/Personalization/RolesController.cs | 3 +- .../API/Personalization/SoftwareController.cs | 3 +- .../API/Search/WildcardController.cs | 3 +- .../Controllers/ApprovalsController.cs | 6 +-- .../Controllers/HomeController.cs | 11 ++--- .../Controllers/ProtocolController.cs | 6 +-- .../Controllers/ReportController.cs | 6 +-- .../Controllers/SearchController.cs | 7 +-- .../Controllers/ShipmentsController.cs | 7 +-- .../LaaProduction.Web/Global.asax.cs | 28 +++++++++++- .../LaaProduction.Web.csproj | 4 +- .../Views/Shared/_Layout.cshtml | 1 + 42 files changed, 235 insertions(+), 189 deletions(-) create mode 100644 LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeFunction.cs create mode 100644 LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeRole.cs delete mode 100644 LaaProductionWeb/LaaProduction.Personalization/ServiceProvider.cs delete mode 100644 LaaProductionWeb/LaaProduction.Search/ServiceProvider.cs delete mode 100644 LaaProductionWeb/LaaProduction.Web/App_Infrastructure/ServiceProvider.cs diff --git a/LaaProductionWeb/LaaProduction.Personalization/AccountManager.cs b/LaaProductionWeb/LaaProduction.Personalization/AccountManager.cs index 4ec775b3..c3d8e53c 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/AccountManager.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/AccountManager.cs @@ -3,9 +3,11 @@ using LaaProduction.Personalization.Interfaces; using LaaProduction.Personalization.Models; using LaaProduction.Personalization.Repositories.Interfaces; + using LaaProductionDI; using System; using System.Collections.Generic; + using System.Linq; using System.Security.Claims; using System.Security.Principal; @@ -23,8 +25,8 @@ public AccountManager() { - this.employeesRepository = ServiceProvider.GetService(); - this.softwareRepository = ServiceProvider.GetService(); + this.employeesRepository = LaaServiceProvider.GetService(); + this.softwareRepository = LaaServiceProvider.GetService(); } public string GetAuthToken(string username, string password, string software) @@ -66,7 +68,7 @@ var userRoles = this.employeesRepository .GetUserRoles(token) - .Intersect(typeof(UsersRoles)); + .Intersect(Enum.GetNames(typeof(UsersRoles))); foreach (var role in userRoles) { @@ -75,7 +77,7 @@ var functions = this.softwareRepository .GetSoftwareFunctions(token) - .Intersect(typeof(SoftwareFunctions)); + .Intersect(Enum.GetNames(typeof(SoftwareFunctions))); foreach (var func in functions) { diff --git a/LaaProductionWeb/LaaProduction.Personalization/EmployeesManager.cs b/LaaProductionWeb/LaaProduction.Personalization/EmployeesManager.cs index 754dd052..2a397d1d 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/EmployeesManager.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/EmployeesManager.cs @@ -3,9 +3,11 @@ using LaaProduction.Personalization.Interfaces; using LaaProduction.Personalization.Models; using LaaProduction.Personalization.Repositories.Interfaces; + using LaaProductionDI; using System; using System.Collections.Generic; + using System.Linq; public class EmployeesManager : IEmployeesManager { @@ -15,9 +17,9 @@ public EmployeesManager() { - this.employeesRepository = ServiceProvider.GetService(); - this.functionsRepository = ServiceProvider.GetService(); - this.rolesRepository = ServiceProvider.GetService(); + this.employeesRepository = LaaServiceProvider.GetService(); + this.functionsRepository = LaaServiceProvider.GetService(); + this.rolesRepository = LaaServiceProvider.GetService(); } public object GetEmployee(short employeeId) @@ -33,9 +35,40 @@ return employee ?? new object { }; } - public IEnumerable GetEmployees() - => this.employeesRepository.All() - ?? Array.Empty(); + public IEnumerable GetEmployees() + { + var employees = this.employeesRepository.All() ?? Array.Empty(); + + if (employees.Any()) + { + employees = employees.ToList(); + + var countFunctionsGroupedByEmployee = this.functionsRepository + .UserFunctionsCount() + .ToDictionary(x => x.Key, x => x.Value); + + var countRolesGroupedByEmployee = this.rolesRepository + .UsersRolesCount() + .ToDictionary(x => x.Key, x => x.Value); + + foreach (var employee in employees) + { + var employeeId = employee.EmployeeId; + + if (countRolesGroupedByEmployee.ContainsKey(employeeId)) + { + employee.RolesCount = countRolesGroupedByEmployee[employeeId]; + } + + if (countFunctionsGroupedByEmployee.ContainsKey(employeeId)) + { + employee.FunctionsCount = countFunctionsGroupedByEmployee[employeeId]; + } + } + } + + return employees; + } public IEnumerable GetRoles() => this.rolesRepository.All() ?? Array.Empty(); diff --git a/LaaProductionWeb/LaaProduction.Personalization/LaaProduction.Personalization.csproj b/LaaProductionWeb/LaaProduction.Personalization/LaaProduction.Personalization.csproj index afaef5f9..87449e9b 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/LaaProduction.Personalization.csproj +++ b/LaaProductionWeb/LaaProduction.Personalization/LaaProduction.Personalization.csproj @@ -31,6 +31,9 @@ 4 + + ..\packages\LaaProductionDI.1.0.0\lib\netstandard2.0\LaaProductionDI.dll + ..\packages\LaaProductionSQL.1.0.0\lib\netstandard2.0\LaaProductionSQL.dll @@ -65,6 +68,8 @@ + + @@ -77,7 +82,6 @@ - @@ -87,7 +91,6 @@ - diff --git a/LaaProductionWeb/LaaProduction.Personalization/Models/Employee.cs b/LaaProductionWeb/LaaProduction.Personalization/Models/Employee.cs index 91d97732..9f992320 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/Models/Employee.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/Models/Employee.cs @@ -12,8 +12,12 @@ public bool HasPassword { get; set; } + public int RolesCount { get; set; } + public IEnumerable Roles { get; set; } + public int FunctionsCount { get; set; } + public IEnumerable Functions { get; set; } } } diff --git a/LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeFunction.cs b/LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeFunction.cs new file mode 100644 index 00000000..1a0636bb --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeFunction.cs @@ -0,0 +1,9 @@ +namespace LaaProduction.Personalization.Models +{ + internal class EmployeeFunction + { + public short EmployeeId { get; set; } + + public string Function { get; set; } + } +} diff --git a/LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeRole.cs b/LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeRole.cs new file mode 100644 index 00000000..3ede2672 --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Personalization/Models/EmployeeRole.cs @@ -0,0 +1,9 @@ +namespace LaaProduction.Personalization.Models +{ + internal class EmployeeRole + { + public int EmployeeId { get; set; } + + public string Role { get; set; } + } +} diff --git a/LaaProductionWeb/LaaProduction.Personalization/PersonalizationExtensions.cs b/LaaProductionWeb/LaaProduction.Personalization/PersonalizationExtensions.cs index 60dd262e..68df9f74 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/PersonalizationExtensions.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/PersonalizationExtensions.cs @@ -1,26 +1,21 @@ namespace LaaProduction.Personalization { using LaaProduction.Personalization.Interfaces; - - using Microsoft.Extensions.DependencyInjection; - - using System; - using System.Collections.Generic; - using System.Linq; + using LaaProduction.Personalization.Repositories; + using LaaProduction.Personalization.Repositories.Interfaces; + using LaaProductionDI; public static class PersonalizationExtensions { - internal static IEnumerable Intersect(this IEnumerable source, Type enumType) - => Enum.GetNames(enumType).Intersect(source); - - public static IServiceCollection AddAdministration(this IServiceCollection services, string sqlConnectionString) - { - ServiceProvider.RegisterInternalServices(sqlConnectionString); - - return services + public static LaaServiceCollection AddAdministration(this LaaServiceCollection services) + => services + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() .AddScoped() .AddScoped() .AddScoped(); - } + } } diff --git a/LaaProductionWeb/LaaProduction.Personalization/Repositories/EmployeesRepository.cs b/LaaProductionWeb/LaaProduction.Personalization/Repositories/EmployeesRepository.cs index 72e5ee28..649b36f5 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/Repositories/EmployeesRepository.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/Repositories/EmployeesRepository.cs @@ -17,16 +17,18 @@ public IEnumerable All() => this.sqlConnection .CreateCommand($@" - SELECT [M].[MitarbeiterNr] AS [EmployeeId] - , [M].[Vorname] + ' ' + [M].[Name] AS [FullName] - , [M].[Benutzername] AS [Username] - FROM [Mitarbeiter] AS [M] + SELECT [M].[MitarbeiterNr] AS [EmployeeId] + , [M].[Vorname] + ' ' + [M].[Name] AS [FullName] + , [M].[Benutzername] AS [Username] + , [M].[PasswordHash] AS [Password] + FROM [Mitarbeiter] AS [M] ORDER BY [FullName], [Username]") .ExecuteReader(x => new Employee { EmployeeId = x.GetSmallint(), FullName = x.GetString(), Username = x.GetString(), + HasPassword = !string.IsNullOrWhiteSpace(x.GetString()) }); public void CreateToken(short appId, short userId) @@ -67,7 +69,7 @@ EmployeeId = x.GetSmallint(), FullName = x.GetString(), Username = x.GetString(), - HasPassword = x.GetString() != null + HasPassword = !string.IsNullOrWhiteSpace(x.GetString()) }); public Employee GetEmployee(string token) diff --git a/LaaProductionWeb/LaaProduction.Personalization/Repositories/FunctionsRepository.cs b/LaaProductionWeb/LaaProduction.Personalization/Repositories/FunctionsRepository.cs index 0cdc6736..3bb82843 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/Repositories/FunctionsRepository.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/Repositories/FunctionsRepository.cs @@ -60,5 +60,17 @@ WHERE [Function] IN ({string.Join(", ", functions.Select(x => $"'{x}'"))})") .SetParameter(nameof(employeeId), employeeId) .ExecuteNonQuery(); + + public IEnumerable> UserFunctionsCount() + => this.sqlConnection + .CreateCommand($@" + SELECT DISTINCT + [UF].[UserId] AS [EmployeeId] + , COUNT([SF].[Function]) AS [Functions] + FROM [UsersFunctions] AS [UF] + JOIN [SoftwareFunctions] AS [SF] + ON [SF].[Id] = [UF].[SoftwareFunctionId] + GROUP BY [UF].[UserId]") + .ExecuteReader(x => new KeyValuePair(x.GetSmallint(), x.GetInt())); } } diff --git a/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IFunctionsRepository.cs b/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IFunctionsRepository.cs index 205ef615..ada06678 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IFunctionsRepository.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IFunctionsRepository.cs @@ -13,5 +13,7 @@ IEnumerable EmployeeAll(short employeeId); void Insert(short employeeId, IEnumerable functions); + + IEnumerable> UserFunctionsCount(); } } diff --git a/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IRolesRepository.cs b/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IRolesRepository.cs index 56de3c7b..4ccddea9 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IRolesRepository.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/Repositories/Interfaces/IRolesRepository.cs @@ -11,5 +11,7 @@ void Delete(short employeeId); void Insert(short employeeId, IEnumerable roles); + + IEnumerable> UsersRolesCount(); } } diff --git a/LaaProductionWeb/LaaProduction.Personalization/Repositories/RolesRepository.cs b/LaaProductionWeb/LaaProduction.Personalization/Repositories/RolesRepository.cs index 0d5fe51b..d06d64f8 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/Repositories/RolesRepository.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/Repositories/RolesRepository.cs @@ -7,7 +7,7 @@ using System.Data.SqlClient; using System.Linq; - public class RolesRepository : IRolesRepository + internal class RolesRepository : IRolesRepository { private readonly ISQLConnection sqlConnection; @@ -44,5 +44,15 @@ INSERT INTO [MitarbeiterRechte] ([MitarbeiterNr], [Recht]) VALUES {string.Join(", ", roles.Select((x, i) => $"('{employeeId}', '{new SqlParameter(i.ToString(), x).SqlValue}')"))}") .ExecuteNonQuery(); + + public IEnumerable> UsersRolesCount() + => this.sqlConnection + .CreateCommand($@" + SELECT DISTINCT + [MitarbeiterNr] + , COUNT([Recht]) + FROM [MitarbeiterRechte] + GROUP BY [MitarbeiterNr]") + .ExecuteReader(x => new KeyValuePair(x.GetInt(), x.GetInt())); } } diff --git a/LaaProductionWeb/LaaProduction.Personalization/ServiceProvider.cs b/LaaProductionWeb/LaaProduction.Personalization/ServiceProvider.cs deleted file mode 100644 index 3182b2f5..00000000 --- a/LaaProductionWeb/LaaProduction.Personalization/ServiceProvider.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace LaaProduction.Personalization -{ - using LaaProduction.Personalization.Repositories; - using LaaProduction.Personalization.Repositories.Interfaces; - - using LaaProductionSQL; - - using Microsoft.Extensions.DependencyInjection; - - using System; - - internal static class ServiceProvider - { - private static IServiceProvider serviceProvider; - - internal static T GetService() - => serviceProvider.GetService(); - - internal static void RegisterInternalServices(string sqlConnectionString) - => serviceProvider = new ServiceCollection() - .AddSingleton(_ => SQLConnection.CreateConnection(sqlConnectionString)) - .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() - .BuildServiceProvider(); - } -} diff --git a/LaaProductionWeb/LaaProduction.Personalization/SoftwareManager.cs b/LaaProductionWeb/LaaProduction.Personalization/SoftwareManager.cs index 72243a7e..8fc1ce2d 100644 --- a/LaaProductionWeb/LaaProduction.Personalization/SoftwareManager.cs +++ b/LaaProductionWeb/LaaProduction.Personalization/SoftwareManager.cs @@ -2,6 +2,7 @@ { using LaaProduction.Personalization.Interfaces; using LaaProduction.Personalization.Repositories.Interfaces; + using LaaProductionDI; using System; using System.Collections.Generic; @@ -11,7 +12,7 @@ private readonly IFunctionsRepository functionsRepository; public SoftwareManager() - => this.functionsRepository = ServiceProvider.GetService(); + => this.functionsRepository = LaaServiceProvider.GetService(); public IEnumerable GetFunctions() => this.functionsRepository.All() diff --git a/LaaProductionWeb/LaaProduction.Search/LaaProduction.Search.csproj b/LaaProductionWeb/LaaProduction.Search/LaaProduction.Search.csproj index 99a60b75..0f5a2e57 100644 --- a/LaaProductionWeb/LaaProduction.Search/LaaProduction.Search.csproj +++ b/LaaProductionWeb/LaaProduction.Search/LaaProduction.Search.csproj @@ -31,6 +31,9 @@ 4 + + ..\packages\LaaProductionDI.1.0.0\lib\netstandard2.0\LaaProductionDI.dll + ..\packages\LaaProductionSQL.1.0.0\lib\netstandard2.0\LaaProductionSQL.dll @@ -71,9 +74,7 @@ - - diff --git a/LaaProductionWeb/LaaProduction.Search/SearchExtensions.cs b/LaaProductionWeb/LaaProduction.Search/SearchExtensions.cs index 764a8d45..56aadada 100644 --- a/LaaProductionWeb/LaaProduction.Search/SearchExtensions.cs +++ b/LaaProductionWeb/LaaProduction.Search/SearchExtensions.cs @@ -1,14 +1,14 @@ namespace LaaProduction.Search { - using Microsoft.Extensions.DependencyInjection; + using LaaProduction.Search.Data; + using LaaProduction.Search.Data.Interfaces; + using LaaProductionDI; public static class SearchExtensions { - public static IServiceCollection AddSearching(this IServiceCollection services, string sqlConnectionString) - { - ServiceProvider.RegisterInternalServices(sqlConnectionString); - - return services.AddScoped(); - } + public static LaaServiceCollection AddSearching(this LaaServiceCollection services) + => services + .AddScoped() + .AddScoped(); } } diff --git a/LaaProductionWeb/LaaProduction.Search/SearchManager.cs b/LaaProductionWeb/LaaProduction.Search/SearchManager.cs index 77f55c31..8e6da00c 100644 --- a/LaaProductionWeb/LaaProduction.Search/SearchManager.cs +++ b/LaaProductionWeb/LaaProduction.Search/SearchManager.cs @@ -1,6 +1,7 @@ namespace LaaProduction.Search { using LaaProduction.Search.Data.Interfaces; + using LaaProductionDI; using System; using System.Collections.Generic; @@ -11,7 +12,7 @@ private readonly IWildcardRepository wildcardRepository; public SearchManager() - => this.wildcardRepository = ServiceProvider.GetService(); + => this.wildcardRepository = LaaServiceProvider.GetService(); public IEnumerable> SearchWildcard(string token, IEnumerable selectedFields) { diff --git a/LaaProductionWeb/LaaProduction.Search/ServiceProvider.cs b/LaaProductionWeb/LaaProduction.Search/ServiceProvider.cs deleted file mode 100644 index 64c05f06..00000000 --- a/LaaProductionWeb/LaaProduction.Search/ServiceProvider.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace LaaProduction.Search -{ - using LaaProduction.Search.Data; - using LaaProduction.Search.Data.Interfaces; - using LaaProductionSQL; - - using Microsoft.Extensions.DependencyInjection; - - using System; - - internal static class ServiceProvider - { - private static IServiceProvider serviceProvider; - - internal static T GetService() - => serviceProvider.GetService(); - - internal static void RegisterInternalServices(string sqlConnectionString) - => serviceProvider = new ServiceCollection() - .AddSingleton(_ => SQLConnection.CreateConnection(sqlConnectionString)) - .AddScoped() - .BuildServiceProvider(); - } -} diff --git a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizationFilter.cs b/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizationFilter.cs index 03aa871d..5a7bdbe1 100644 --- a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizationFilter.cs +++ b/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizationFilter.cs @@ -1,6 +1,8 @@ namespace LaaProduction.Web.App_Infrastructure { using LaaProduction.Personalization.Interfaces; + using LaaProduction.Web.Controllers; + using LaaProductionDI; using IAuthorizationFilter = System.Web.Mvc.IAuthorizationFilter; using AuthorizationContext = System.Web.Mvc.AuthorizationContext; @@ -11,10 +13,10 @@ { var httpContext = filterContext.HttpContext; var sessionUser = httpContext.GetSessionUser(); - var accountManagement = ServiceProvider.GetService(); + var accountManagement = LaaServiceProvider.GetService(); httpContext.User = accountManagement.GetClaimsPrincipal(sessionUser); - if (httpContext.User.Identity.IsAuthenticated) + if (httpContext.User.Identity.IsAuthenticated && !httpContext.Request.Url.ToString().Contains(nameof(HomeController.Logout))) { httpContext.SetUrlReferer(); } diff --git a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizeBearerAttribute.cs b/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizeBearerAttribute.cs index 89e1d0cb..435d6d6a 100644 --- a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizeBearerAttribute.cs +++ b/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/AuthorizeBearerAttribute.cs @@ -1,6 +1,7 @@ namespace LaaProduction.Web.App_Infrastructure { using LaaProduction.Personalization.Interfaces; + using LaaProductionDI; using System.Web.Http; using System.Web.Http.Controllers; @@ -11,7 +12,7 @@ { if (actionContext.Request.Headers.Authorization?.Scheme == "Bearer") { - var accountManager = ServiceProvider.GetService(); + var accountManager = LaaServiceProvider.GetService(); var bearer = actionContext.Request.Headers.Authorization.Parameter; var principal = accountManager.GetClaimsPrincipal(bearer); diff --git a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/HttpExtensions.cs b/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/HttpExtensions.cs index 4f46b529..97f918b9 100644 --- a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/HttpExtensions.cs +++ b/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/HttpExtensions.cs @@ -1,6 +1,7 @@ namespace LaaProduction.Web.App_Infrastructure { using LaaProductionHttp.Interfaces; + using Newtonsoft.Json; using System; @@ -78,7 +79,7 @@ .Request .Cookies .Get(URL_REFERER) - ?.Value ?? "~/"; + ?.Value ?? "/"; public static void SetUrlReferer(this HttpContextBase httpContext) => httpContext @@ -127,35 +128,38 @@ public static string GetSessionUser(this HttpContextBase httpContext) { - var requestCookies = httpContext.Request.Cookies; - var sessionUser = requestCookies.Get(LAA_USER); + var sessionUser = httpContext.Request.Cookies.Get(LAA_USER); if (sessionUser is null) { - sessionUser = new HttpCookie(LAA_USER, string.Empty); - requestCookies.Add(sessionUser); + return default(string); } - sessionUser.Expires = DateTime.Now.AddSeconds(Appsettings.LoginTimeout); + sessionUser.Expires = DateTime.UtcNow.AddMinutes(Appsettings.LoginTimeout); - requestCookies.Set(sessionUser); + httpContext.Response.Cookies.Set(sessionUser); return sessionUser.Value; } - public static void SignIn(this HttpContextBase httpContext, string sessionUser) + public static void SignIn(this HttpContextBase httpContext, string token) { - httpContext.SignOut(); - httpContext.Response.AppendCookie(new HttpCookie(LAA_USER, sessionUser) + httpContext.Response.Cookies.Add(new HttpCookie(LAA_USER, token) { - Expires = DateTime.Now.AddSeconds(Appsettings.LoginTimeout) + Expires = DateTime.UtcNow.AddMinutes(Appsettings.LoginTimeout) }); } public static void SignOut(this HttpContextBase httpContext) { - httpContext.Request.Cookies.Remove(LAA_USER); - httpContext.Response.Cookies.Remove(LAA_USER); + var sessionUser = httpContext.Request.Cookies.Get(LAA_USER); + + if (sessionUser != null) + { + sessionUser.Expires = DateTime.UtcNow.AddMinutes(Appsettings.LoginTimeout * -1); + + httpContext.Response.Cookies.Set(sessionUser); + } } public static string ComputeHash(this string value) diff --git a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/ServiceProvider.cs b/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/ServiceProvider.cs deleted file mode 100644 index 47678d74..00000000 --- a/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/ServiceProvider.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace LaaProduction -{ - using LaaProduction.Personalization; - using LaaProduction.Search; - using LaaProductionHttp; - using LaaProductionSQL; - using LaaProduction.Web.App_Infrastructure; - using LaaProduction.Services; - using LaaProduction.Services.Interfaces; - - using Microsoft.Extensions.DependencyInjection; - - using System; - - public class ServiceProvider - { - private static IServiceProvider serviceProvider; - - public static T GetService() where T : class - => serviceProvider.GetService(); - - public static void RegisterServices() - => serviceProvider = new ServiceCollection() - .AddSingleton(_ => SQLConnection.CreateConnection(Appsettings.ConnectionString)) - .AddSingleton(_ => HttpClient.CreateHttpClient(Appsettings.APIURL)) - .AddAdministration(Appsettings.ConnectionString) - .AddSearching(Appsettings.ConnectionString) - .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() - .AddScoped() - .BuildServiceProvider(); - } -} \ No newline at end of file diff --git a/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/EmployeesController.cs b/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/EmployeesController.cs index 6db72a9d..fea72544 100644 --- a/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/EmployeesController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/EmployeesController.cs @@ -4,6 +4,7 @@ using LaaProductionHttp.Interfaces; using LaaProduction.Web.App_Infrastructure; using LaaProduction.Web.Areas.Personalization.Models.Employees; + using LaaProductionDI; using System; using System.Collections.Generic; @@ -18,7 +19,7 @@ private readonly IHttpClient httpClient; public EmployeesController() - => this.httpClient = ServiceProvider.GetService(); + => this.httpClient = LaaServiceProvider.GetService(); [HttpGet] public async Task Index() @@ -31,6 +32,8 @@ if (employeesResponse.Succeeded) { + var count = employeesResponse.Value.Count(x => x.RolesCount > 0); + employeeysModel.Employees = employeesResponse.Value ?? Array.Empty(); } else diff --git a/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/SoftwareController.cs b/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/SoftwareController.cs index de28e1b7..09a41a25 100644 --- a/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/SoftwareController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/SoftwareController.cs @@ -1,10 +1,10 @@ namespace LaaProduction.Web.Areas.Personalization.Controllers { using LaaProduction.Personalization; - using LaaProduction.Web.App_Infrastructure; using LaaProduction.Web.Areas.Personalization.Models.Software; using LaaProduction.Services.Interfaces; + using LaaProductionDI; using System; using System.Linq; @@ -17,7 +17,7 @@ private readonly ISoftwareService softwareService; public SoftwareController() - => this.softwareService = ServiceProvider.GetService(); + => this.softwareService = LaaServiceProvider.GetService(); [HttpGet] public ActionResult Index() diff --git a/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Views/Employees/Edit.cshtml b/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Views/Employees/Edit.cshtml index b9c2a9f1..f759e8da 100644 --- a/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Views/Employees/Edit.cshtml +++ b/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Views/Employees/Edit.cshtml @@ -66,7 +66,7 @@ { var id = $"r_{r++}"; -
+
+
Mitarbeitername Bunutzername + Kennwort Anzahl Rechte Anzahl Funktionen @@ -33,6 +34,7 @@ @employee.Username + @employee.RolesCount @employee.FunctionsCount diff --git a/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/AppIdController.cs b/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/AppIdController.cs index 503936ee..7b112d83 100644 --- a/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/AppIdController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/AppIdController.cs @@ -1,6 +1,7 @@ namespace LaaProduction.Web.Areas.Software.Controllers { using LaaProduction.Services.Interfaces; + using LaaProductionDI; using System; using System.Collections.Generic; @@ -13,8 +14,8 @@ public AppIdController() { - this.softwareService = ServiceProvider.GetService(); - this.loggerService = ServiceProvider.GetService(); + this.softwareService = LaaServiceProvider.GetService(); + this.loggerService = LaaServiceProvider.GetService(); } [HttpPost] diff --git a/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/FunctionsController.cs b/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/FunctionsController.cs index f7ebcbe3..2030ae83 100644 --- a/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/FunctionsController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Areas/Software/Controllers/FunctionsController.cs @@ -1,6 +1,7 @@ namespace LaaProduction.Web.Areas.Software.Controllers { using LaaProduction.Services.Interfaces; + using LaaProductionDI; using System; using System.Web.Http; @@ -13,9 +14,9 @@ public FunctionsController() { - this.accountService = ServiceProvider.GetService(); - this.softwareService = ServiceProvider.GetService(); - this.loggerService = ServiceProvider.GetService(); + this.accountService = LaaServiceProvider.GetService(); + this.softwareService = LaaServiceProvider.GetService(); + this.loggerService = LaaServiceProvider.GetService(); } [HttpPost] diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/EmployeesController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/EmployeesController.cs index a705ab4f..310c79f1 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/EmployeesController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/EmployeesController.cs @@ -2,6 +2,7 @@ { using LaaProduction.Personalization.Interfaces; using LaaProduction.Web.App_Infrastructure; + using LaaProductionDI; using System; using System.ComponentModel.DataAnnotations; @@ -16,8 +17,8 @@ public EmployeesController() { - this.accountManager = ServiceProvider.GetService(); - this.employeesManager = ServiceProvider.GetService(); + this.accountManager = LaaServiceProvider.GetService(); + this.employeesManager = LaaServiceProvider.GetService(); } [HttpGet] diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/LoginController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/LoginController.cs index 517e1a60..83d8294a 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/LoginController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/LoginController.cs @@ -1,6 +1,7 @@ namespace LaaProduction.Web.Controllers.API.Account { using LaaProduction.Personalization.Interfaces; + using LaaProductionDI; using System.ComponentModel.DataAnnotations; using System.Web.Http; @@ -10,7 +11,7 @@ private readonly IAccountManager accountManagement; public LoginController() - => this.accountManagement = ServiceProvider.GetService(); + => this.accountManagement = LaaServiceProvider.GetService(); [HttpPost] public IHttpActionResult Post(LoginPostModel model) diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/RolesController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/RolesController.cs index b09eb29e..d7ac0a4b 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/RolesController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/RolesController.cs @@ -2,6 +2,7 @@ { using LaaProduction.Personalization.Interfaces; using LaaProduction.Web.App_Infrastructure; + using LaaProductionDI; using System.Web.Http; @@ -11,7 +12,7 @@ private readonly IEmployeesManager employeesManager; public RolesController() - => this.employeesManager = ServiceProvider.GetService(); + => this.employeesManager = LaaServiceProvider.GetService(); [HttpGet] public IHttpActionResult Get() diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/SoftwareController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/SoftwareController.cs index 00c996ca..74de4b56 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/SoftwareController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Personalization/SoftwareController.cs @@ -2,6 +2,7 @@ { using LaaProduction.Personalization.Interfaces; using LaaProduction.Web.App_Infrastructure; + using LaaProductionDI; using System.Web.Http; @@ -11,7 +12,7 @@ private readonly ISoftwareManager softwareManager; public SoftwareController() - => this.softwareManager = ServiceProvider.GetService(); + => this.softwareManager = LaaServiceProvider.GetService(); [HttpGet] public IHttpActionResult Functions() diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Search/WildcardController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Search/WildcardController.cs index 8ba256af..b47bacb4 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/API/Search/WildcardController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/API/Search/WildcardController.cs @@ -2,6 +2,7 @@ { using LaaProduction.Search; using LaaProduction.Web.App_Infrastructure; + using LaaProductionDI; using System; using System.Collections.Generic; @@ -17,7 +18,7 @@ private readonly ISearchManager searchManager; public WildcardController() - => this.searchManager = ServiceProvider.GetService(); + => this.searchManager = LaaServiceProvider.GetService(); [HttpGet] public IHttpActionResult Get() diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/ApprovalsController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/ApprovalsController.cs index ab94b863..6745e52e 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/ApprovalsController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/ApprovalsController.cs @@ -1,21 +1,21 @@ namespace LaaProduction.Web.Controllers { using LaaProduction.Personalization; - using LaaProduction.Web.App_Infrastructure; using LaaProduction.Services.Interfaces; using LaaProduction.Services.Models; + using LaaProductionDI; using System; using System.Web.Mvc; - + [AllowedRoles(UsersRoles.WEB_ProdApproval)] public class ApprovalsController : Controller { private readonly IApprovalsService approvals; public ApprovalsController() - => this.approvals = ServiceProvider.GetService(); + => this.approvals = LaaServiceProvider.GetService(); [HttpGet] public ActionResult Add() diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/HomeController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/HomeController.cs index 5f3b6f5b..e1588ae5 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/HomeController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/HomeController.cs @@ -3,10 +3,11 @@ using LaaProductionHttp.Interfaces; using LaaProduction.Web.App_Infrastructure; using LaaProduction.Services.Interfaces; - + using LaaProductionDI; + using System.Threading.Tasks; using System.Web.Mvc; - + [AllowAnonymous] public class HomeController : Controller { @@ -15,8 +16,8 @@ public HomeController() { - this.accountService = ServiceProvider.GetService(); - this.httpClient = ServiceProvider.GetService(); + this.accountService = LaaServiceProvider.GetService(); + this.httpClient = LaaServiceProvider.GetService(); } [HttpGet] @@ -74,7 +75,7 @@ { this.HttpContext.SignOut(); - return this.View(nameof(Index)); + return this.Redirect("/"); } } } \ No newline at end of file diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/ProtocolController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/ProtocolController.cs index f8e3489b..f8729fc7 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/ProtocolController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/ProtocolController.cs @@ -1,13 +1,13 @@ namespace LaaProduction.Web.Controllers { using LaaProduction.Personalization; - using LaaProduction.Web.App_Infrastructure; using LaaProduction.Services.Interfaces; using LaaProduction.Services.Models; + using LaaProductionDI; using System.Web.Mvc; - + [AllowedRoles(UsersRoles.WEB_PuneProtokoll)] public class ProtocolController : Controller { @@ -16,7 +16,7 @@ private readonly IProtocolService protocolService; public ProtocolController() - => this.protocolService = ServiceProvider.GetService(); + => this.protocolService = LaaServiceProvider.GetService(); [HttpGet] public ActionResult Index(int orderId = 0) diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/ReportController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/ReportController.cs index d8944176..08766ddc 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/ReportController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/ReportController.cs @@ -1,21 +1,21 @@ namespace LaaProduction.Web.Controllers { using LaaProduction.Personalization; - using LaaProduction.Web.App_Infrastructure; using LaaProduction.Services.Interfaces; using LaaProduction.Services.Models.Reports; + using LaaProductionDI; using System; using System.Web.Mvc; - + [AllowedRoles(UsersRoles.WEB_HeReport)] public class ReportController : Controller { private readonly IReportService reportService; public ReportController() - => this.reportService = ServiceProvider.GetService(); + => this.reportService = LaaServiceProvider.GetService(); [HttpGet] public ActionResult Helium() diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/SearchController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/SearchController.cs index 74c634bf..c3a73df9 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/SearchController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/SearchController.cs @@ -5,6 +5,7 @@ using LaaProduction.Web.App_Infrastructure; using LaaProduction.Services.Interfaces; using LaaProduction.Services.Models.Search; + using LaaProductionDI; using System; using System.Collections.Generic; @@ -12,7 +13,7 @@ using System.Net; using System.Threading.Tasks; using System.Web.Mvc; - + public class SearchController : Controller { private readonly IOrdersService ordersService; @@ -20,8 +21,8 @@ public SearchController() { - this.ordersService = ServiceProvider.GetService(); - this.httpClient = ServiceProvider.GetService(); + this.ordersService = LaaServiceProvider.GetService(); + this.httpClient = LaaServiceProvider.GetService(); } [HttpGet] diff --git a/LaaProductionWeb/LaaProduction.Web/Controllers/ShipmentsController.cs b/LaaProductionWeb/LaaProduction.Web/Controllers/ShipmentsController.cs index 71090829..a717348b 100644 --- a/LaaProductionWeb/LaaProduction.Web/Controllers/ShipmentsController.cs +++ b/LaaProductionWeb/LaaProduction.Web/Controllers/ShipmentsController.cs @@ -5,6 +5,7 @@ using LaaProduction.Web.App_Infrastructure; using LaaProduction.Services.Interfaces; using LaaProduction.Services.Models; + using LaaProductionDI; using Newtonsoft.Json; @@ -16,7 +17,7 @@ using System.Web.Mvc; using SystemFile = System.IO.File; - + [AllowedRoles(UsersRoles.WEB_PalettenScan)] public class ShipmentsController : Controller { @@ -25,8 +26,8 @@ public ShipmentsController() { - this.shipmentsService = ServiceProvider.GetService(); - this.httpService = ServiceProvider.GetService(); + this.shipmentsService = LaaServiceProvider.GetService(); + this.httpService = LaaServiceProvider.GetService(); } [HttpGet] diff --git a/LaaProductionWeb/LaaProduction.Web/Global.asax.cs b/LaaProductionWeb/LaaProduction.Web/Global.asax.cs index 8a3b12cb..f3197031 100644 --- a/LaaProductionWeb/LaaProduction.Web/Global.asax.cs +++ b/LaaProductionWeb/LaaProduction.Web/Global.asax.cs @@ -1,5 +1,14 @@ namespace LaaProduction.Web { + using LaaProduction.Personalization; + using LaaProduction.Search; + using LaaProduction.Services; + using LaaProduction.Services.Interfaces; + using LaaProduction.Web.App_Infrastructure; + using LaaProductionDI; + using LaaProductionHttp; + using LaaProductionSQL; + using System.Web; using System.Web.Http; using System.Web.Mvc; @@ -10,12 +19,29 @@ { protected void Application_Start() { - ServiceProvider.RegisterServices(); + LaaServiceProvider.Configure(ConfigureServices); + LaaServiceProvider.BuildServiceProvider(); AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } + + private void ConfigureServices(LaaServiceCollection services) + => services + .AddSingleton(SQLConnection.CreateConnection(Appsettings.ConnectionString)) + .AddSingleton(HttpClient.CreateHttpClient(Appsettings.APIURL)) + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddAdministration() + .AddSearching(); } } diff --git a/LaaProductionWeb/LaaProduction.Web/LaaProduction.Web.csproj b/LaaProductionWeb/LaaProduction.Web/LaaProduction.Web.csproj index 3d0a62d4..db024351 100644 --- a/LaaProductionWeb/LaaProduction.Web/LaaProduction.Web.csproj +++ b/LaaProductionWeb/LaaProduction.Web/LaaProduction.Web.csproj @@ -44,6 +44,9 @@ 4 + + ..\packages\LaaProductionDI.1.0.0\lib\netstandard2.0\LaaProductionDI.dll + ..\packages\LaaProductionHttp.1.0.0\lib\netstandard2.0\LaaProductionHttp.dll @@ -144,7 +147,6 @@ - diff --git a/LaaProductionWeb/LaaProduction.Web/Views/Shared/_Layout.cshtml b/LaaProductionWeb/LaaProduction.Web/Views/Shared/_Layout.cshtml index d0b4acda..c6052ea5 100644 --- a/LaaProductionWeb/LaaProduction.Web/Views/Shared/_Layout.cshtml +++ b/LaaProductionWeb/LaaProduction.Web/Views/Shared/_Layout.cshtml @@ -64,6 +64,7 @@ }