employees edit form

This commit is contained in:
Stoyan Zlatev
2023-06-16 11:29:11 +02:00
parent 6f9b448b33
commit bdd730271d
28 changed files with 582 additions and 129 deletions
@@ -1,6 +1,7 @@
namespace LaaProduction.Personalization
{
using LaaProduction.Personalization.Interfaces;
using LaaProduction.Personalization.Models;
using LaaProduction.Personalization.Repositories.Interfaces;
using System;
@@ -49,12 +50,14 @@
public IPrincipal GetClaimsPrincipal(string token)
{
var claimsIdentity = anonymous;
var tokenData = this.employeesRepository.GetTokenData(token);
if (this.employeesRepository.TokenExists(token))
if (tokenData != null)
{
var employee = this.employeesRepository.GetEmployee(token);
var employee = this.employeesRepository.Find(tokenData.EmployeeId);
var claims = new List<Claim>
{
new Claim(nameof(EmployeeToken.AppId), tokenData.AppId.ToString()),
new Claim(nameof(employee.EmployeeId), employee.EmployeeId.ToString()),
new Claim(nameof(employee.FullName), employee.FullName),
new Claim(nameof(employee.Username), employee.Username),
@@ -88,5 +91,14 @@
return new ClaimsPrincipal(claimsIdentity);
}
public void ResetPassword(short employeeId)
{
this.employeesRepository.ResetPassword(employeeId);
this.employeesRepository.ResetTokens(employeeId);
}
public void SetPassword(short employeeId, string password)
=> this.employeesRepository.SetPassword(employeeId, password);
}
}
@@ -6,36 +6,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
public class EmployeesManager : IEmployeesManager
{
private readonly IEmployeesRepository employeesRepository;
private readonly IFunctionsRepository functionsRepository;
private readonly IRolesRepository rolesRepository;
public EmployeesManager()
{
this.employeesRepository = ServiceProvider.GetService<IEmployeesRepository>();
this.functionsRepository = ServiceProvider.GetService<IFunctionsRepository>();
this.rolesRepository = ServiceProvider.GetService<IRolesRepository>();
}
public object GetEmployee(short employeeId)
=> this.employeesRepository.Find(employeeId)
?? new object { };
{
var employee = this.employeesRepository.Find(employeeId);
if (employee != null)
{
employee.Roles = this.rolesRepository.All(employeeId) ?? Array.Empty<string>();
employee.Functions = this.functionsRepository.EmployeeAll(employeeId) ?? Array.Empty<string>();
}
return employee ?? new object { };
}
public IEnumerable<object> GetEmployees()
=> this.employeesRepository.All()
?? Array.Empty<Employee>();
public IEnumerable<string> GetRoles()
=> this.rolesRepository.All()
?? Array.Empty<string>();
=> this.rolesRepository.All() ?? Array.Empty<string>();
public IDictionary<string, bool> GetRoles(short employeeId)
=> this.rolesRepository
.All(employeeId)
.ToDictionary(x => x.Key, x => x.Value)
?? new Dictionary<string, bool>();
public IEnumerable<string> GetRoles(short employeeId)
=> this.rolesRepository.All(employeeId) ?? Array.Empty<string>();
public void UpdateEmployeeFunctions(short employeeId, IEnumerable<string> functions)
{
this.functionsRepository.Delete(employeeId);
this.functionsRepository.Insert(employeeId, functions);
}
public void UpdateEmployeeRoles(short employeeId, IEnumerable<string> roles)
{
@@ -7,5 +7,9 @@
string GetAuthToken(string username, string password, string software);
IPrincipal GetClaimsPrincipal(string token);
void ResetPassword(short employeeId);
void SetPassword(short employeeId, string password);
}
}
@@ -8,10 +8,12 @@
IEnumerable<object> GetEmployees();
IDictionary<string, bool> GetRoles(short employeeId);
IEnumerable<string> GetRoles();
IEnumerable<string> GetRoles(short employeeId);
void UpdateEmployeeFunctions(short employeeId, IEnumerable<string> functions);
void UpdateEmployeeRoles(short employeeId, IEnumerable<string> roles);
}
}
@@ -0,0 +1,11 @@
namespace LaaProduction.Personalization.Interfaces
{
using System.Collections.Generic;
public interface ISoftwareManager
{
IEnumerable<string> GetFunctions();
IEnumerable<string> GetFunctions(short appId);
}
}
@@ -63,18 +63,23 @@
<Compile Include="AccountManager.cs" />
<Compile Include="EmployeesManager.cs" />
<Compile Include="Interfaces\IEmployeesManager.cs" />
<Compile Include="Interfaces\ISoftwareManager.cs" />
<Compile Include="Models\EmployeeToken.cs" />
<Compile Include="PersonalizationExtensions.cs" />
<Compile Include="Interfaces\IAccountManager.cs" />
<Compile Include="Models\Employee.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\EmployeesRepository.cs" />
<Compile Include="Repositories\FunctionsRepository.cs" />
<Compile Include="Repositories\Interfaces\IEmployeesRepository.cs" />
<Compile Include="Repositories\Interfaces\IFunctionsRepository.cs" />
<Compile Include="Repositories\Interfaces\IRolesRepository.cs" />
<Compile Include="Repositories\Interfaces\ISoftwareRepository.cs" />
<Compile Include="Repositories\RolesRepository.cs" />
<Compile Include="Repositories\SoftwareRepository.cs" />
<Compile Include="ServiceProvider.cs" />
<Compile Include="SoftwareFunctions.cs" />
<Compile Include="SoftwareManager.cs" />
<Compile Include="UsersRoles.cs" />
</ItemGroup>
<ItemGroup>
@@ -1,5 +1,7 @@
namespace LaaProduction.Personalization.Models
{
using System.Collections.Generic;
internal class Employee
{
public short EmployeeId { get; set; }
@@ -8,8 +10,10 @@
public string Username { get; set; }
public int RolesCount { get; set; }
public bool HasPassword { get; set; }
public int FunctionsCount { get; set; }
public IEnumerable<string> Roles { get; set; }
public IEnumerable<string> Functions { get; set; }
}
}
@@ -0,0 +1,9 @@
namespace LaaProduction.Personalization.Models
{
internal class EmployeeToken
{
public short AppId { get; set; }
public short EmployeeId { get; set; }
}
}
@@ -16,7 +16,8 @@
return services
.AddScoped<IAccountManager, AccountManager>()
.AddScoped<IEmployeesManager, EmployeesManager>();
.AddScoped<IEmployeesManager, EmployeesManager>()
.AddScoped<ISoftwareManager, SoftwareManager>();
}
internal static IEnumerable<string> Intersect(this IEnumerable<string> source, Type enumType)
@@ -17,33 +17,16 @@
public IEnumerable<Employee> All()
=> this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT
[E].[EmployeeId] AS [EmployeeId]
, [E].[FullName] AS [FullName]
, [E].[Username] AS [Username]
, [E].[RolesCount] AS [RolesCount]
, COUNT([UF].[SoftwareFunctionId])
OVER (PARTITION BY [E].[EmployeeId]) AS [FunctionsCount]
FROM (SELECT DISTINCT
[M].[MitarbeiterNr] AS [EmployeeId]
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
, [M].[Benutzername] AS [Username]
, COUNT([MR].[Recht])
OVER (PARTITION BY [M].[MitarbeiterNr]) AS [RolesCount]
FROM [Mitarbeiter] AS [M]
LEFT JOIN [MitarbeiterRechte] AS [MR]
ON [MR].[MitarbeiterNr] = [M].[MitarbeiterNr])
AS [E]
LEFT JOIN [UsersFunctions] AS [UF]
ON [UF].[UserId] = [E].[EmployeeId]
ORDER BY [E].[FullName]")
SELECT [M].[MitarbeiterNr] AS [EmployeeId]
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
, [M].[Benutzername] AS [Username]
FROM [Mitarbeiter] AS [M]
ORDER BY [FullName], [Username]")
.ExecuteReader(x => new Employee
{
EmployeeId = x.GetSmallint(),
FullName = x.GetString(),
Username = x.GetString(),
RolesCount = x.GetInt(),
FunctionsCount = x.GetInt()
});
public void CreateToken(short appId, short userId)
@@ -72,35 +55,19 @@
public Employee Find(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT
[E].[EmployeeId] AS [EmployeeId]
, [E].[FullName] AS [FullName]
, [E].[Username] AS [Username]
, [E].[RolesCount] AS [RolesCount]
, COUNT([UF].[SoftwareFunctionId])
OVER (PARTITION BY [E].[EmployeeId]) AS [FunctionsCount]
FROM (SELECT DISTINCT
[M].[MitarbeiterNr] AS [EmployeeId]
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
, [M].[Benutzername] AS [Username]
, COUNT([MR].[Recht])
OVER (PARTITION BY [M].[MitarbeiterNr]) AS [RolesCount]
FROM [Mitarbeiter] AS [M]
LEFT JOIN [MitarbeiterRechte] AS [MR]
ON [MR].[MitarbeiterNr] = [M].[MitarbeiterNr]
WHERE [M].[MitarbeiterNr] = @{nameof(employeeId)})
AS [E]
LEFT JOIN [UsersFunctions] AS [UF]
ON [UF].[UserId] = [E].[EmployeeId]
ORDER BY [E].[FullName]")
SELECT [M].[MitarbeiterNr] AS [EmployeeId]
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
, [M].[Benutzername] AS [Username]
, [M].[PasswordHash] AS [Password]
FROM [Mitarbeiter] AS [M]
WHERE [M].[MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.FirstOrDefault(x => new Employee
{
EmployeeId = x.GetSmallint(),
FullName = x.GetString(),
Username = x.GetString(),
RolesCount = x.GetInt(),
FunctionsCount = x.GetInt()
HasPassword = x.GetString() != null
});
public Employee GetEmployee(string token)
@@ -132,6 +99,20 @@
.SetParameter(nameof(userId), userId)
.FirstOrDefault(x => x.GetString());
public EmployeeToken GetTokenData(string token)
=> this.sqlConnection
.CreateCommand($@"
SELECT [AppId]
, [UserId]
FROM [MitarbeiterTokens]
WHERE [Token] = @{nameof(token)}")
.SetParameter(nameof(token), token)
.FirstOrDefault(x => new EmployeeToken
{
AppId = x.GetSmallint(),
EmployeeId = x.GetSmallint()
});
public IEnumerable<string> GetUserRoles(string token)
=> this.sqlConnection
.CreateCommand($@"
@@ -143,13 +124,31 @@
.SetParameter(nameof(token), token)
.ExecuteReader(x => x.GetString());
public bool TokenExists(string token)
public void ResetPassword(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
SELECT COUNT(1)
FROM [MitarbeiterTokens]
WHERE [Token] = @{nameof(token)}")
.SetParameter(nameof(token), token)
.FirstOrDefault(x => x.GetInt() > 0);
UPDATE [Mitarbeiter]
SET [PasswordHash] = NULL
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
public void ResetTokens(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
DELETE FROM [MitarbeiterTokens]
WHERE [UserId] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
public void SetPassword(short employeeId, string password)
=> this.sqlConnection
.CreateCommand($@"
UPDATE [Mitarbeiter]
SET [PasswordHash] = @{nameof(password)}
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.SetParameter(nameof(password), password)
.ExecuteNonQuery();
}
}
@@ -0,0 +1,64 @@
namespace LaaProduction.Personalization.Repositories
{
using LaaProduction.Personalization.Repositories.Interfaces;
using LaaProductionSQL.Interfaces;
using System.Collections.Generic;
using System.Linq;
internal class FunctionsRepository : IFunctionsRepository
{
private readonly ISQLConnection sqlConnection;
public FunctionsRepository(ISQLConnection sqlConnection)
=> this.sqlConnection = sqlConnection;
public IEnumerable<string> All()
=> this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT [SF].[Function]
FROM [SoftwareFunctions] AS [SF]")
.ExecuteReader(x => x.GetString());
public IEnumerable<string> All(short appId)
=> this.sqlConnection
.CreateCommand($@"
SELECT [SF].[Function]
FROM [SoftwareFunctions] AS [SF]
WHERE [SF].[AppId] = @{nameof(appId)}")
.SetParameter(nameof(appId), appId)
.ExecuteReader(x => x.GetString());
public void Delete(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
DELETE FROM [UsersFunctions]
WHERE [UserId] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
public IEnumerable<string> EmployeeAll(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
SELECT [SF].[Function]
FROM [UsersFunctions] AS [UF]
JOIN [SoftwareFunctions] AS [SF]
ON [SF].[Id] = [UF].[SoftwareFunctionId]
WHERE [UF].[UserId] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteReader(x => x.GetString());
public void Insert(short employeeId, IEnumerable<string> functions)
=> this.sqlConnection
.CreateCommand($@"
INSERT INTO [UsersFunctions] (
[UserId]
, [SoftwareFunctionId])
SELECT @{nameof(employeeId)}
, [Id]
FROM [SoftwareFunctions]
WHERE [Function] IN ({string.Join(", ", functions.Select(x => $"'{x}'"))})")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteNonQuery();
}
}
@@ -18,8 +18,14 @@
string GetToken(short appId, short userId);
EmployeeToken GetTokenData(string token);
IEnumerable<string> GetUserRoles(string token);
bool TokenExists(string token);
void ResetPassword(short employeeId);
void ResetTokens(short employeeId);
void SetPassword(short employeeId, string password);
}
}
@@ -0,0 +1,17 @@
namespace LaaProduction.Personalization.Repositories.Interfaces
{
using System.Collections.Generic;
internal interface IFunctionsRepository
{
IEnumerable<string> All();
IEnumerable<string> All(short appId);
void Delete(short employeeId);
IEnumerable<string> EmployeeAll(short employeeId);
void Insert(short employeeId, IEnumerable<string> functions);
}
}
@@ -2,11 +2,11 @@
{
using System.Collections.Generic;
public interface IRolesRepository
internal interface IRolesRepository
{
IEnumerable<string> All();
IEnumerable<KeyValuePair<string, bool>> All(short employeeId);
IEnumerable<string> All(short employeeId);
void Delete(short employeeId);
@@ -21,18 +21,14 @@
FROM [MitarbeiterRechte]")
.ExecuteReader(x => x.GetString());
public IEnumerable<KeyValuePair<string, bool>> All(short employeeId)
public IEnumerable<string> All(short employeeId)
=> this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT
[AllRoles].[Recht]
, CAST(CASE WHEN [UsrRoles].[MitarbeiterNr] IS NULL THEN 0 ELSE 1 END AS BIT)
FROM [MitarbeiterRechte] AS [AllRoles]
LEFT JOIN [MitarbeiterRechte] AS [UsrRoles]
ON [UsrRoles].[Recht] = [AllRoles].[Recht]
AND [UsrRoles].[MitarbeiterNr] = @{nameof(employeeId)}")
SELECT DISTINCT [Recht]
FROM [MitarbeiterRechte]
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
.SetParameter(nameof(employeeId), employeeId)
.ExecuteReader(x => new KeyValuePair<string, bool>(x.GetString(), x.GetBool()));
.ExecuteReader(x => x.GetString());
public void Delete(short employeeId)
=> this.sqlConnection
@@ -21,6 +21,7 @@
serviceProvider = new ServiceCollection()
.AddSingleton(_ => SQLConnection.CreateConnection(sqlConnectionString))
.AddScoped<IEmployeesRepository, EmployeesRepository>()
.AddScoped<IFunctionsRepository, FunctionsRepository>()
.AddScoped<IRolesRepository, RolesRepository>()
.AddScoped<ISoftwareRepository, SoftwareRepository>()
.BuildServiceProvider();
@@ -0,0 +1,24 @@
namespace LaaProduction.Personalization
{
using LaaProduction.Personalization.Interfaces;
using LaaProduction.Personalization.Repositories.Interfaces;
using System;
using System.Collections.Generic;
public class SoftwareManager : ISoftwareManager
{
private readonly IFunctionsRepository functionsRepository;
public SoftwareManager()
=> this.functionsRepository = ServiceProvider.GetService<IFunctionsRepository>();
public IEnumerable<string> GetFunctions()
=> this.functionsRepository.All()
?? Array.Empty<string>();
public IEnumerable<string> GetFunctions(short appId)
=> this.functionsRepository.All(appId)
?? Array.Empty<string>();
}
}
@@ -190,5 +190,18 @@
}
}
}
public static short GetAppId(this IPrincipal principal)
{
if (principal is ClaimsPrincipal claimsPrincipal)
{
if (short.TryParse($"{claimsPrincipal.FindFirst("AppId")?.Value}", out short appId))
{
return appId;
}
}
return default(short);
}
}
}
@@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
@@ -44,24 +45,70 @@
[RouteParameter("employeeId")]
public async Task<ActionResult> Edit(short employeeId = 0)
{
var bearerToken = this.User.GetNameIdentifier();
var employeeResponse = await this.httpClient
.Get($"/api/Personalization/Employees/Find/{employeeId}")
.AuthorizeBearer(this.User.GetNameIdentifier())
.SendAsync<EmployeeEditModel>();
.AuthorizeBearer(bearerToken)
.SendAsync<EmployeeFindModel>();
var employeeEditModel = new EmployeeEditModel();
if (employeeResponse.Succeeded)
if (employeeResponse.Succeeded && employeeResponse.Value != null)
{
employeeEditModel = employeeResponse.Value;
var employee = employeeResponse.Value;
employeeEditModel = employee.Map(x => new EmployeeEditModel
{
EmployeeId = x.EmployeeId,
FullName = x.FullName,
Username = x.Username,
HasPassword = x.HasPassword
});
var rolesResponse = await this.httpClient
.Get($"/api/Personalization/Roles/Find/{employeeId}")
.AuthorizeBearer(this.User.GetNameIdentifier())
.SendAsync<IDictionary<string, bool>>();
.Get($"/api/Personalization/Roles")
.AuthorizeBearer(bearerToken)
.SendAsync<IEnumerable<string>>();
if (rolesResponse.Succeeded)
if (rolesResponse.Succeeded && rolesResponse.Value != null)
{
employeeEditModel.Roles = rolesResponse.Value ?? new Dictionary<string, bool>();
var roles = rolesResponse.Value.ToDictionary(x => x, x => false);
foreach (var role in employee.Roles)
{
if (roles.ContainsKey(role))
{
roles[role] = true;
}
}
employeeEditModel.Roles = roles;
employeeEditModel.RolesCount = employee.Roles.Count();
}
else
{
this.ModelState.AddModelErrors(rolesResponse);
}
var funktionsResponse = await this.httpClient
.Get($"/api/Personalization/Software/Functions")
.AuthorizeBearer(bearerToken)
.SendAsync<IEnumerable<string>>();
if (funktionsResponse.Succeeded && funktionsResponse.Value != null)
{
var functions = funktionsResponse.Value.ToDictionary(x => x, x => false);
foreach (var function in employee.Functions)
{
if (functions.ContainsKey(function))
{
functions[function] = true;
}
}
employeeEditModel.Functions = functions;
employeeEditModel.FunctionsCount = employee.Functions.Count();
}
else
{
@@ -87,5 +134,43 @@
return this.Redirect($"~/Personalization/Employees/Edit/{model.EmployeeId}");
}
[HttpPost]
public async Task<ActionResult> EditFunctions(EmployeeEditFunctionsModel model)
{
var result = await this.httpClient
.Post($"/api/Personalization/Employees/EditFunctions")
.AuthorizeBearer(this.User.GetNameIdentifier())
.WhitJsonBody(model)
.SendAsync<object>();
return this.Redirect($"~/Personalization/Employees/Edit/{model.EmployeeId}");
}
[HttpGet]
public async Task<ActionResult> ResetPassword(short id)
{
var result = await this.httpClient
.Get($"/api/Personalization/Employees/ResetPassword/{id}")
.AuthorizeBearer(this.User.GetNameIdentifier())
.SendAsync<object>();
return this.Redirect($"~/Personalization/Employees/Edit/{id}");
}
[HttpPost]
public async Task<ActionResult> ResetPassword(EmployeePassword model)
{
if (this.ModelState.IsValid)
{
var result = await this.httpClient
.Post($"/api/Personalization/Employees/SetPassword")
.AuthorizeBearer(this.User.GetNameIdentifier())
.WhitJsonBody(new { model.EmployeeId, Password = model.PasswordI.ComputeHash() })
.SendAsync<object>();
}
return this.Redirect($"~/Personalization/Employees/Edit/{model.EmployeeId}");
}
}
}
@@ -8,6 +8,8 @@
public string Username { get; set; }
public bool HasPassword { get; set; }
public int RolesCount { get; set; }
public int FunctionsCount { get; set; }
@@ -0,0 +1,14 @@
namespace LaaProductionWeb.Areas.Personalization.Models.Employees
{
using System;
using System.ComponentModel.DataAnnotations;
public class EmployeeEditFunctionsModel
{
[Required]
public short? EmployeeId { get; set; }
public string[] Functions { get; set; }
= Array.Empty<string>();
}
}
@@ -7,7 +7,7 @@
public IDictionary<string, bool> Roles { get; set; }
= new Dictionary<string, bool>();
public IDictionary<int, string> Functions { get; set; }
= new Dictionary<int, string>();
public IDictionary<string, bool> Functions { get; set; }
= new Dictionary<string, bool>();
}
}
@@ -0,0 +1,19 @@
namespace LaaProductionWeb.Areas.Personalization.Models.Employees
{
using System.Collections.Generic;
public class EmployeeFindModel
{
public short EmployeeId { get; set; }
public string FullName { get; set; }
public string Username { get; set; }
public bool HasPassword { get; set; }
public IEnumerable<string> Roles { get; set; }
public IEnumerable<string> Functions { get; set; }
}
}
@@ -0,0 +1,17 @@
namespace LaaProductionWeb.Areas.Personalization.Models.Employees
{
using System.ComponentModel.DataAnnotations;
public class EmployeePassword
{
[Required]
public short? EmployeeId { get; set; }
[Required]
public string PasswordI { get; set; }
[Required]
[Compare(nameof(PasswordI))]
public string PasswordII { get; set; }
}
}
@@ -5,26 +5,46 @@
<div class="container-fluid mt-3 mb-5">
<div class="row">
<div class="col-12">
<h3>
<a href="/Personalization/Employees" class="text-dark text-decoration-none">
<i class="bi bi-bold bi-arrow-left small pe-3"></i>
</a>
@this.Model.FullName
</h3>
<table class="table table-borderless">
<tr>
<td class="px-3">MitarbeiterNr:</td>
<td class="w-100 fw-bold">@this.Model.EmployeeId</td>
</tr>
<tr>
<td class="px-3">Username:</td>
<td class="w-100 fw-bold">@this.Model.Username</td>
</tr>
<tr>
<td class="px-3">Name:</td>
<td class="w-100 fw-bold">@this.Model.FullName</td>
</tr>
</table>
<div class="row">
<div class="col-8">
<h3>
<a href="/Personalization/Employees" class="text-dark text-decoration-none">
<i class="bi bi-bold bi-arrow-left small pe-3"></i>
</a>
@this.Model.FullName
</h3>
<table class="table table-borderless">
<tr>
<td class="px-3">MitarbeiterNr:</td>
<td class="w-100 fw-bold">@this.Model.EmployeeId</td>
</tr>
<tr>
<td class="px-3">Username:</td>
<td class="w-100 fw-bold">@this.Model.Username</td>
</tr>
<tr>
<td class="px-3">Name:</td>
<td class="w-100 fw-bold">@this.Model.FullName</td>
</tr>
</table>
</div>
<div class="col-4">
@if (!this.Model.HasPassword)
{
<form action="~/Personalization/Employees/ResetPassword" method="post" id="employee_password">
<input type="hidden" name="@nameof(this.Model.EmployeeId)" value="@this.Model.EmployeeId" />
<div class="form-floating mb-3">
<input type="password" class="form-control" id="pwd1" name="PasswordI" placeholder="password">
<label for="pwd1">Passwort</label>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="pwd2" name="PasswordII" placeholder="confirm password">
<label for="pwd2">Passwort wiederholen</label>
</div>
</form>
}
</div>
</div>
<hr class="my-0" />
<div class="small text-danger mb-5">
@this.Html.ValidationSummary()
@@ -32,8 +52,8 @@
</div>
<div class="col-4">
<div class="dropdown">
<button class="btn btn-outline-success dropdown-toggle w-100 text-start" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Rechte: @this.Model.RolesCount
<button class="btn btn-outline-success dropdown-toggle w-100 text-start py-3" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Rechte: insgesamt @this.Model.RolesCount
</button>
<div class="dropdown-menu w-100">
<form action="~/Personalization/Employees/EditRoles" method="post" id="employee_roles" class="px-2 py-0">
@@ -63,24 +83,52 @@
</div>
</div>
<div class="col-4">
<form>
<div class="dropdown">
<button class="btn btn-outline-primary dropdown-toggle w-100 text-start" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Funktionen: @this.Model.FunctionsCount
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
<div class="dropdown">
<button class="btn btn-outline-success dropdown-toggle w-100 text-start py-3" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Funktionen: @this.Model.FunctionsCount
</button>
<div class="dropdown-menu w-100">
<form action="~/Personalization/Employees/EditFunctions" method="post" id="employee_funktions" class="px-2 py-0">
<input type="hidden" name="@nameof(this.Model.EmployeeId)" value="@this.Model.EmployeeId" />
<div class="scrollable-sm" style="max-height: 300px">
@{
var f = 1;
foreach (var function in this.Model.Functions)
{
var id = $"f_{f++}";
<div class="form-check">
<input class="form-check-input"
type="checkbox"
value="@function.Key"
name="Functions[]"
id="@id" @(function.Value ? "checked=\"checked\"" : null)>
<label for="@id" class="form-check-label">@function.Key</label>
</div>
}
}
</div>
<button class="btn btn-light btn-sm w-100 border fw-bold mt-3" type="submit">Aktualizieren</button>
</form>
</div>
</form>
</div>
</div>
<div class="col-4">
<a class="btn btn-light d-block border-dark">
<i class="bi bi-exclamation-circle"></i>
Password zurücksetzen
</a>
@if (this.Model.HasPassword)
{
<a class="btn btn-light d-block border-dark py-3" href="~/Personalization/Employees/ResetPassword/@this.Model.EmployeeId">
<i class="bi bi-exclamation-circle"></i>
Password zurücksetzen
</a>
}
else
{
<button class="btn btn-light w-100 border-dark py-3" onclick="javascript:document.getElementById('employee_password').submit()">
<i class="bi bi-exclamation-circle"></i>
Password speichern
</button>
}
</div>
</div>
</div>
@@ -8,12 +8,17 @@
using System.Web.Http;
[AuthorizeBearer]
[RoutePrefix("api/Personalization/Employees")]
public class EmployeesController : ApiController
{
private readonly IAccountManager accountManager;
private readonly IEmployeesManager employeesManager;
public EmployeesController()
=> this.employeesManager = ServiceProvider.GetService<IEmployeesManager>();
{
this.accountManager = ServiceProvider.GetService<IAccountManager>();
this.employeesManager = ServiceProvider.GetService<IEmployeesManager>();
}
[HttpGet]
public IHttpActionResult Get()
@@ -32,6 +37,7 @@
}
[HttpPost]
[Route(nameof(EditRoles))]
public IHttpActionResult EditRoles([FromBody] EditRolesModel model)
{
if (this.ModelState.IsValid)
@@ -41,6 +47,42 @@
return this.Json(true);
}
[HttpPost]
[Route(nameof(EditFunctions))]
public IHttpActionResult EditFunctions([FromBody] EditFunctionsModel model)
{
if (this.ModelState.IsValid)
{
this.employeesManager.UpdateEmployeeFunctions(model.EmployeeId.Value, model.Functions);
}
return this.Json(true);
}
[HttpGet]
[Route("ResetPassword/{id}")]
public IHttpActionResult ResetPassword(short id)
{
if (this.ModelState.IsValid)
{
this.accountManager.ResetPassword(id);
}
return this.Json(true);
}
[HttpPost]
[Route(nameof(SetPassword))]
public IHttpActionResult SetPassword(PasswordModel model)
{
if (this.ModelState.IsValid)
{
this.accountManager.SetPassword(model.EmployeeId.Value, model.Password);
}
return this.Json(true);
}
}
public class EditRolesModel
@@ -51,4 +93,22 @@
public string[] Roles { get; set; }
= Array.Empty<string>();
}
public class EditFunctionsModel
{
[Required]
public short? EmployeeId { get; set; }
public string[] Functions { get; set; }
= Array.Empty<string>();
}
public class PasswordModel
{
[Required]
public short? EmployeeId { get; set; }
[Required]
public string Password { get; set; }
}
}
@@ -0,0 +1,24 @@
namespace LaaProductionWeb.Controllers.API.Personalization
{
using LaaProduction.Personalization.Interfaces;
using LaaProductionWeb.App_Infrastructure;
using System.Web.Http;
[AuthorizeBearer]
public class SoftwareController : ApiController
{
private readonly ISoftwareManager softwareManager;
public SoftwareController()
=> this.softwareManager = ServiceProvider.GetService<ISoftwareManager>();
[HttpGet]
public IHttpActionResult Functions()
{
var employeeRoles = this.softwareManager.GetFunctions();
return this.Json(employeeRoles);
}
}
}
@@ -173,8 +173,11 @@
<Compile Include="Areas\Personalization\Controllers\PersonalizationController.cs" />
<Compile Include="Areas\Personalization\Controllers\SoftwareFunctionsController.cs" />
<Compile Include="Areas\Personalization\Models\Employees\Employee.cs" />
<Compile Include="Areas\Personalization\Models\Employees\EmployeeEditFunctionsModel.cs" />
<Compile Include="Areas\Personalization\Models\Employees\EmployeeEditModel.cs" />
<Compile Include="Areas\Personalization\Models\Employees\EmployeeEditRolesModel.cs" />
<Compile Include="Areas\Personalization\Models\Employees\EmployeeFindModel.cs" />
<Compile Include="Areas\Personalization\Models\Employees\EmployeePassword.cs" />
<Compile Include="Areas\Personalization\Models\Employees\EmployeesListModel.cs" />
<Compile Include="Areas\Personalization\PersonalizationAreaRegistration.cs" />
<Compile Include="Areas\Personalization\Controllers\SoftwareController.cs" />
@@ -201,6 +204,7 @@
<Compile Include="Areas\Software\SoftwareAreaRegistration.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Controllers\API\Personalization\EmployeesController.cs" />
<Compile Include="Controllers\API\Personalization\SoftwareController.cs" />
<Compile Include="Controllers\API\Personalization\LoginController.cs" />
<Compile Include="Controllers\API\Personalization\RolesController.cs" />
<Compile Include="Controllers\ApprovalsController.cs" />