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> GetUser(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllUsers() { var retVal = new ApiResponseDTO>() { 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> DeleteUser(int id) { var retVal = new ApiResponseDTO() { 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> UpdateUser([FromBody] UserDTO userDTO) { var retVal = new ApiResponseDTO() { 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> Authenticate([FromBody] UserSimpleDTO userSimpleDTO) { var retVal = new ApiResponseDTO() { 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; } /// /// Ez a 2 faktoros első lépése. /// /// /// [HttpPost("Authenticate2F1")] public async Task> Authenticate2F1([FromBody] UserSimpleDTO userSimpleDTO) { var retVal = new ApiResponseDTO() { 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> Authenticate2F2([FromBody] User2FADTO user2FADTO) { var retVal = new ApiResponseDTO() { 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> Authenticate2F2ChangePassword([FromBody] UserChangePassword2FADTO userChangePassword2FADTO) { var retVal = new ApiResponseDTO() { 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> AuthenticateNFC([FromBody] UserSimpleDTO userSimpleDTO) { var retVal = new ApiResponseDTO() { 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> GetRole(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllRoles() { var retVal = new ApiResponseDTO>() { 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> DeleteRole(int id) { var retVal = new ApiResponseDTO() { 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> UpdateRole([FromBody] RoleDTO roleDTO) { var retVal = new ApiResponseDTO() { 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> GetUserRole(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllUserRoles() { var retVal = new ApiResponseDTO>() { 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> DeleteUserRole(int id) { var retVal = new ApiResponseDTO() { 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> UpdateUserRole([FromBody] UserRoleDTO userRoleDTO) { var retVal = new ApiResponseDTO() { 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> GetRoleCheckListTemplateHeader(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllRoleCheckListTemplateHeaders() { var retVal = new ApiResponseDTO>() { 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> DeleteRoleCheckListTemplateHeader(int id) { var retVal = new ApiResponseDTO() { 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> UpdateRoleCheckListTemplateHeader([FromBody] RoleCheckListTemplateHeaderDTO roleCheckListTemplateHeaderDTO) { var retVal = new ApiResponseDTO() { 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> GetRoleCheckPoint(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllRoleCheckPoints() { var retVal = new ApiResponseDTO>() { 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> DeleteRoleCheckPoint(int id) { var retVal = new ApiResponseDTO() { 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> UpdateRoleCheckPoint([FromBody] RoleCheckPointDTO roleCheckPointDTO) { var retVal = new ApiResponseDTO() { IsSuccess = true }; try { var result = await _userService.UpdateRoleCheckPointAsync(roleCheckPointDTO); retVal.Data = result; } catch (Exception ex) { retVal.IsSuccess = false; Log.Error(ex.Message); } return retVal; } } }