Files
WorkFlowCheck/src/WorkFlowCheck.API/Controllers/UserController.cs
T
2025-04-14 12:31:50 +02:00

757 lines
25 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.BL.Services.Interfaces;
using Serilog;
using WorkFlowCheck.BL.Services;
namespace WorkFlowCheck.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private IUserService _userService;
public UserController(IUserService userService)
{
_userService = userService;
}
[HttpGet("GetUser/{id}")]
public async Task<ApiResponseDTO<UserDTO>> GetUser(int id)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true,
};
var userDTO = await _userService.GetUserAsync(id);
if (userDTO != null)
{
if (userDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = userDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = userDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllUsers")]
public async Task<ApiResponseDTO<List<UserDTO>>> GetAllUsers()
{
var retVal = new ApiResponseDTO<List<UserDTO>>()
{
IsSuccess = true,
};
var userDTO = await _userService.GetAllUserAsync();
if (userDTO != null)
{
retVal.IsSuccess = true;
retVal.Data = userDTO;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("DeleteUser/{id}")]
public async Task<ApiResponseDTO<bool>> DeleteUser(int id)
{
var retVal = new ApiResponseDTO<bool>()
{
IsSuccess = true,
};
var result = await _userService.DeleteUserAsync(id);
if (result != null)
{
retVal.IsSuccess = true;
retVal.Data = result;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateUser")]
public async Task<ApiResponseDTO<UserDTO>> UpdateUser([FromBody] UserDTO userDTO)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateUserAsync(userDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
[HttpPost("Authenticate")]
public async Task<ApiResponseDTO<UserDTO>> Authenticate([FromBody] UserSimpleDTO userSimpleDTO)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
if (string.IsNullOrEmpty(userSimpleDTO.UserName) || string.IsNullOrEmpty(userSimpleDTO.Password))
{
retVal.IsSuccess = false;
retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó");
return retVal;
}
try
{
var userDTO_Response = await _userService.Authenticate(userSimpleDTO.UserName, userSimpleDTO.Password);
if (userDTO_Response != null)
{
if (string.IsNullOrEmpty(userDTO_Response.UserName) == false && userDTO_Response.UserName == userSimpleDTO.UserName)
{
retVal.IsSuccess = true;
retVal.Data = userDTO_Response;
Log.Information($"Sikeres azonosítás! {userSimpleDTO.UserName}");
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
/// <summary>
/// Ez a 2 faktoros első lépése.
/// </summary>
/// <param name="userSimpleDTO"></param>
/// <returns></returns>
[HttpPost("Authenticate2F1")]
public async Task<ApiResponseDTO<string>> Authenticate2F1([FromBody] UserSimpleDTO userSimpleDTO)
{
var retVal = new ApiResponseDTO<string>()
{
IsSuccess = true
};
if (string.IsNullOrEmpty(userSimpleDTO.UserName) || string.IsNullOrEmpty(userSimpleDTO.Password))
{
retVal.IsSuccess = false;
retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó");
return retVal;
}
try
{
var response = await _userService.Authenticate2F1(userSimpleDTO.UserName, userSimpleDTO.Password);
if (!string.IsNullOrEmpty(response))
{
retVal.IsSuccess = true;
retVal.Data = response;
Log.Information($"Sikeres azonosítás! {userSimpleDTO.UserName}");
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
[HttpPost("Authenticate2F2")]
public async Task<ApiResponseDTO<UserDTO>> Authenticate2F2([FromBody] User2FADTO user2FADTO)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
if (string.IsNullOrEmpty(user2FADTO.UserName) ||
string.IsNullOrEmpty(user2FADTO.Password) ||
string.IsNullOrEmpty(user2FADTO.Code) ||
string.IsNullOrEmpty(user2FADTO.Token2FA))
{
retVal.IsSuccess = false;
retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó");
return retVal;
}
try
{
var userDTO_Response = await _userService.Authenticate2F2(user2FADTO.UserName,
user2FADTO.Password,
user2FADTO.Code,
user2FADTO.Token2FA);
if (userDTO_Response != null)
{
if (string.IsNullOrEmpty(userDTO_Response.UserName) == false)
{
retVal.IsSuccess = true;
retVal.Data = userDTO_Response;
Log.Information($"Sikeres azonosítás! {user2FADTO.UserName}");
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {user2FADTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {user2FADTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
[HttpPost("Authenticate2F2ChangePassword")]
public async Task<ApiResponseDTO<UserDTO>> Authenticate2F2ChangePassword([FromBody] UserChangePassword2FADTO userChangePassword2FADTO)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
if (string.IsNullOrEmpty(userChangePassword2FADTO.UserName) ||
string.IsNullOrEmpty(userChangePassword2FADTO.OldPassword) ||
string.IsNullOrEmpty(userChangePassword2FADTO.NewPassword1) ||
string.IsNullOrEmpty(userChangePassword2FADTO.NewPassword2) ||
string.IsNullOrEmpty(userChangePassword2FADTO.Code) ||
string.IsNullOrEmpty(userChangePassword2FADTO.Token2FA))
{
retVal.IsSuccess = false;
retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó");
return retVal;
}
try
{
var userDTO_Response = await _userService.Authenticate2F2ChangePassword(userChangePassword2FADTO.UserName,
userChangePassword2FADTO.OldPassword,
userChangePassword2FADTO.NewPassword1,
userChangePassword2FADTO.Code,
userChangePassword2FADTO.Token2FA);
if (userDTO_Response != null)
{
if (string.IsNullOrEmpty(userDTO_Response.UserName) == false)
{
retVal.IsSuccess = true;
retVal.Data = userDTO_Response;
Log.Information($"Sikeres azonosítás! {userChangePassword2FADTO.UserName}");
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userChangePassword2FADTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userChangePassword2FADTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
[HttpPost("AuthenticateNFC")]
public async Task<ApiResponseDTO<UserDTO>> AuthenticateNFC([FromBody] UserSimpleDTO userSimpleDTO)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
if (string.IsNullOrEmpty(userSimpleDTO.UserName) || string.IsNullOrEmpty(userSimpleDTO.Password))
{
retVal.IsSuccess = false;
retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó");
return retVal;
}
try
{
var userDTO_Response = await _userService.AuthenticateNFC(userSimpleDTO.UserName);
if (userDTO_Response != null)
{
if (string.IsNullOrEmpty(userDTO_Response.UserName) == false && userDTO_Response.NFCActive)
{
retVal.IsSuccess = true;
retVal.Data = userDTO_Response;
Log.Information($"Sikeres azonosítás! {userSimpleDTO.UserName}");
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("GetRole/{id}")]
public async Task<ApiResponseDTO<RoleDTO>> GetRole(int id)
{
var retVal = new ApiResponseDTO<RoleDTO>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetRoleAsync(id);
if (RoleDTO != null)
{
if (RoleDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllRoles")]
public async Task<ApiResponseDTO<List<RoleDTO>>> GetAllRoles()
{
var retVal = new ApiResponseDTO<List<RoleDTO>>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetAllRoleAsync();
if (RoleDTO != null)
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("DeleteRole/{id}")]
public async Task<ApiResponseDTO<bool>> DeleteRole(int id)
{
var retVal = new ApiResponseDTO<bool>()
{
IsSuccess = true,
};
var result = await _userService.DeleteRoleAsync(id);
if (result != null)
{
retVal.IsSuccess = true;
retVal.Data = result;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateRole")]
public async Task<ApiResponseDTO<RoleDTO>> UpdateRole([FromBody] RoleDTO roleDTO)
{
var retVal = new ApiResponseDTO<RoleDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateRoleAsync(roleDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("GetUserRole/{id}")]
public async Task<ApiResponseDTO<UserRoleDTO>> GetUserRole(int id)
{
var retVal = new ApiResponseDTO<UserRoleDTO>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetUserRoleAsync(id);
if (RoleDTO != null)
{
if (RoleDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllUserRoles")]
public async Task<ApiResponseDTO<List<UserRoleDTO>>> GetAllUserRoles()
{
var retVal = new ApiResponseDTO<List<UserRoleDTO>>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetAllUserRoleAsync();
if (RoleDTO != null)
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("DeleteUserRole/{id}")]
public async Task<ApiResponseDTO<bool>> DeleteUserRole(int id)
{
var retVal = new ApiResponseDTO<bool>()
{
IsSuccess = true,
};
var result = await _userService.DeleteUserRoleAsync(id);
if (result != null)
{
retVal.IsSuccess = true;
retVal.Data = result;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateUserRole")]
public async Task<ApiResponseDTO<UserRoleDTO>> UpdateUserRole([FromBody] UserRoleDTO userRoleDTO)
{
var retVal = new ApiResponseDTO<UserRoleDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateUserRoleAsync(userRoleDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("GetRoleCheckListTemplateHeader/{id}")]
public async Task<ApiResponseDTO<RoleCheckListTemplateHeaderDTO>> GetRoleCheckListTemplateHeader(int id)
{
var retVal = new ApiResponseDTO<RoleCheckListTemplateHeaderDTO>()
{
IsSuccess = true,
};
var roleCheckListTemplateHeaderDTO = await _userService.GetRoleCheckListTemplateHeaderAsync(id);
if (roleCheckListTemplateHeaderDTO != null)
{
if (roleCheckListTemplateHeaderDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = roleCheckListTemplateHeaderDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = roleCheckListTemplateHeaderDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllRoleCheckListTemplateHeaders")]
public async Task<ApiResponseDTO<List<RoleCheckListTemplateHeaderDTO>>> GetAllRoleCheckListTemplateHeaders()
{
var retVal = new ApiResponseDTO<List<RoleCheckListTemplateHeaderDTO>>()
{
IsSuccess = true,
};
var results = await _userService.GetAllRoleCheckListTemplateHeadersAsync();
if (results != null)
{
retVal.IsSuccess = true;
retVal.Data = results;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("DeleteRoleCheckListTemplateHeader/{id}")]
public async Task<ApiResponseDTO<bool>> DeleteRoleCheckListTemplateHeader(int id)
{
var retVal = new ApiResponseDTO<bool>()
{
IsSuccess = true,
};
var result = await _userService.DeleteRoleCheckListTemplateHeaderAsync(id);
if (result != null)
{
retVal.IsSuccess = true;
retVal.Data = result;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateRoleCheckListTemplateHeader")]
public async Task<ApiResponseDTO<RoleCheckListTemplateHeaderDTO>> UpdateRoleCheckListTemplateHeader([FromBody] RoleCheckListTemplateHeaderDTO roleCheckListTemplateHeaderDTO)
{
var retVal = new ApiResponseDTO<RoleCheckListTemplateHeaderDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateRoleCheckListTemplateHeaderAsync(roleCheckListTemplateHeaderDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("GetRoleCheckPoint/{id}")]
public async Task<ApiResponseDTO<RoleCheckPointDTO>> GetRoleCheckPoint(int id)
{
var retVal = new ApiResponseDTO<RoleCheckPointDTO>()
{
IsSuccess = true,
};
var roleCheckPointDTO = await _userService.GetRoleCheckPointAsync(id);
if (roleCheckPointDTO != null)
{
if (roleCheckPointDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = roleCheckPointDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = roleCheckPointDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllRoleCheckPoints")]
public async Task<ApiResponseDTO<List<RoleCheckPointDTO>>> GetAllRoleCheckPoints()
{
var retVal = new ApiResponseDTO<List<RoleCheckPointDTO>>()
{
IsSuccess = true,
};
var results = await _userService.GetAllRoleCheckPointsAsync();
if (results != null)
{
retVal.IsSuccess = true;
retVal.Data = results;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("DeleteRoleCheckPoint/{id}")]
public async Task<ApiResponseDTO<bool>> DeleteRoleCheckPoint(int id)
{
var retVal = new ApiResponseDTO<bool>()
{
IsSuccess = true,
};
var result = await _userService.DeleteRoleCheckPointAsync(id);
if (result != null)
{
retVal.IsSuccess = true;
retVal.Data = result;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateRoleCheckPoint")]
public async Task<ApiResponseDTO<RoleCheckPointDTO>> UpdateRoleCheckPoint([FromBody] RoleCheckPointDTO roleCheckPointDTO)
{
var retVal = new ApiResponseDTO<RoleCheckPointDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateRoleCheckPointAsync(roleCheckPointDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
}
}