diff --git a/CarParkValidationAPI.csproj b/CarParkValidationAPI.csproj index c3a4906..4f8186f 100644 --- a/CarParkValidationAPI.csproj +++ b/CarParkValidationAPI.csproj @@ -1,23 +1,22 @@  - net6.0 + net8.0 punt - - - - - - + + + + + - + - + diff --git a/CarParkValidationAPI.sln b/CarParkValidationAPI.sln index 90db40b..25fd62b 100644 --- a/CarParkValidationAPI.sln +++ b/CarParkValidationAPI.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 17.4.33122.133 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarParkValidationAPI", "CarParkValidationAPI.csproj", "{74980A36-AF9D-44B4-991D-994E033AD54A}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommonAD6", "..\..\CommonAD\CommonAD6.csproj", "{9EF32775-1957-4B84-9A05-C6AB69D1ABD5}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonAD6", "..\..\CommonAD6\CommonAD6.csproj", "{05B28DB7-0D54-1EAB-130D-4A1DEE9C3A27}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +17,10 @@ Global {74980A36-AF9D-44B4-991D-994E033AD54A}.Debug|Any CPU.Build.0 = Debug|Any CPU {74980A36-AF9D-44B4-991D-994E033AD54A}.Release|Any CPU.ActiveCfg = Release|Any CPU {74980A36-AF9D-44B4-991D-994E033AD54A}.Release|Any CPU.Build.0 = Release|Any CPU - {9EF32775-1957-4B84-9A05-C6AB69D1ABD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9EF32775-1957-4B84-9A05-C6AB69D1ABD5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9EF32775-1957-4B84-9A05-C6AB69D1ABD5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9EF32775-1957-4B84-9A05-C6AB69D1ABD5}.Release|Any CPU.Build.0 = Release|Any CPU + {05B28DB7-0D54-1EAB-130D-4A1DEE9C3A27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {05B28DB7-0D54-1EAB-130D-4A1DEE9C3A27}.Debug|Any CPU.Build.0 = Debug|Any CPU + {05B28DB7-0D54-1EAB-130D-4A1DEE9C3A27}.Release|Any CPU.ActiveCfg = Release|Any CPU + {05B28DB7-0D54-1EAB-130D-4A1DEE9C3A27}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Datas/AutoMapping.cs b/Datas/AutoMapping.cs deleted file mode 100644 index f9ca640..0000000 --- a/Datas/AutoMapping.cs +++ /dev/null @@ -1,27 +0,0 @@ -using AutoMapper; -using CarParkValidationAPI.Entities; -using CarParkValidationAPI.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace CarParkValidationAPI.Datas -{ - public class AutoMapping: Profile - { - public AutoMapping() - { - CreateMap() - .ForMember( dest => dest.Id, opt => opt.MapFrom(src => src.ConcessionTypeID)) - .ForMember(dest => dest.Description , opt=> opt.MapFrom(src=> src.ConcessionDesc)) - .ForMember(dest => dest.Display, opt => opt.MapFrom(src => src.ConcessionDisplay)) - .ReverseMap(); - CreateMap() - .ForMember(dest => dest.Id , opt => opt.MapFrom(src => src.ValidationPointID)) - .ForMember(dest => dest.Display, opt => opt.MapFrom(src => src.DisplayName)) - .ReverseMap(); - /*etc...*/ - } - } -} diff --git a/Repository/ConcessionTypeRepository.cs b/Repository/ConcessionTypeRepository.cs index 4b9d951..a7ac862 100644 --- a/Repository/ConcessionTypeRepository.cs +++ b/Repository/ConcessionTypeRepository.cs @@ -12,7 +12,7 @@ using CarParkValidationAPI.Datas; using CarParkValidationAPI.Interface; using Microsoft.EntityFrameworkCore; using System; -using AutoMapper; + namespace CarParkValidationAPI.Repository { @@ -21,13 +21,13 @@ namespace CarParkValidationAPI.Repository private DataContext _context; private string _currentUser = ""; private readonly AppSettings _appSettings; - private readonly IMapper _mapper; + public ConcessionTypeRepository( DataContext context, - IMapper mapper, + IOptions appSettings) { - _mapper = mapper; + _context = context; _appSettings = appSettings.Value; } @@ -61,7 +61,7 @@ namespace CarParkValidationAPI.Repository string error = ""; try { - /* + result = await _context.ConcessionTypes.Select( x => new ConcessionTypeDto() { @@ -70,9 +70,9 @@ namespace CarParkValidationAPI.Repository Display = x.ConcessionDisplay, Active = x.Active }).ToListAsync(); - */ - var olist = await _context.ConcessionTypes.ToListAsync(); - result = _mapper.Map>(olist); + + //var olist = await _context.ConcessionTypes.ToListAsync(); + //result = _mapper.Map>(olist); statuscode = 1; result.Sort((x, y) => x.Description.CompareTo(y.Description)); } @@ -100,7 +100,17 @@ namespace CarParkValidationAPI.Repository try { result = await _context.ConcessionTypes.FindAsync(id); - dto = _mapper.Map(result); + dto = new(); + if (result != null) + { + dto.Id = id; + dto.Active = result.Active; + dto.Description = result.ConcessionDesc; + dto.Display = result.ConcessionDisplay; + + + } + // dto = _mapper.Map(result); //dto = FillConcessionTypeDto(result); //result = _list.Find(x => x.Id == id); //var dto = await Task.Run(() => result); diff --git a/Repository/UserFakeRepository.cs b/Repository/UserFakeRepository.cs deleted file mode 100644 index f26d9c0..0000000 --- a/Repository/UserFakeRepository.cs +++ /dev/null @@ -1,262 +0,0 @@ -//using BCryptNet = BCrypt.Net.BCrypt; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.Options; -using CarParkValidationAPI.Authorization; -using CarParkValidationAPI.Entities; -using CarParkValidationAPI.Helpers; -using CarParkValidationAPI.Models.Users; -using CarParkValidationAPI.Models; -using CarParkValidationAPI.Datas; -using CarParkValidationAPI.Interface; -using Microsoft.EntityFrameworkCore; -using CommonAD.Models; -using CommonAD; -using Microsoft.AspNetCore.Http; - -namespace CarParkValidationAPI.Repository -{ - public class UserFakeRepository : IUserService - { - private DataContext _context; - private IJwtUtils _jwtUtils; - private readonly AppSettings _appSettings; - - public UserFakeRepository( - DataContext context, - IJwtUtils jwtUtils, - IOptions appSettings) - { - _context = context; - _jwtUtils = jwtUtils; - _appSettings = appSettings.Value; - - } - public AuthenticateResponse Authenticate(AuthenticateRequest model) - { - var user = _context.Users.SingleOrDefault(x => x.Username == model.Username); - - // validate - // if (user == null || !BCryptNet.Verify(model.Password, user.PasswordHash)) - if (user == null || (model.Password != user.PasswordHash)) - throw new System.Exception("Username or password is incorrect"); - - // authentication successful so generate jwt token - var jwtToken = _jwtUtils.GenerateJwtToken(user); - - return new AuthenticateResponse(user, jwtToken); - } - public async Task> LoginApiAD(AuthenticateRequest model) - { - User user = new User(); - int statusCode = 1; - if (model.Username.ToLower() == "admin") - { - user.FirstName = model.Username; - user.LastName = "LS Test"; - user.Role = AdminRole.Admin; - user.Username = model.Username; - user.Email = "admin@mail.com"; - } - else - { - user.Username = model.Username; - user.FirstName = "FS Normal"; - user.LastName = " LS Normal"; - user.Email = "Normal@mail.com"; - user.Role = AdminRole.Normal; - } - - // authentication successful so generate jwt token - var jwtToken = _jwtUtils.GenerateJwtToken(user); - - var retval = await Task.Run(() => new AuthenticateResponse(user, jwtToken)); - return new ResultModel() - { - Data = retval, - StatusCode = statusCode - }; - } - - public async Task> NLoginApiAD(AuthenticateRequest model) - { - User myUser = null; - AuthenticateResponse retval = null; - int statuscode = 0; - string webAPIUrl = _appSettings.LoginWebAPI; - - var loginInfo = await CommonAD.WebAPIUtil.GetLoginADAsync(webAPIUrl, "CheckADCredentials", model.Username, model.Password); - if (loginInfo != null && loginInfo.result) - { - myUser = new User(); - myUser.Email = loginInfo.email; - myUser.FirstName = loginInfo.firstName; - myUser.LastName = loginInfo.lastName; - myUser.Username = loginInfo.stafflinkNo; - //now check the adminuser table - var user = _context.AdminUsers.SingleOrDefault(x => x.StafflinkNo == model.Username); - if (user != null) - { - myUser.Role = user.RoleType; - myUser.Id = user.UserID; - } - else - myUser.Role = AdminRole.Normal; - - // validate - // if (user == null || !BCryptNet.Verify(model.Password, user.PasswordHash)) - - // authentication successful so generate jwt token - var jwtToken = _jwtUtils.GenerateJwtToken(myUser); - statuscode = 1; - retval = new AuthenticateResponse(myUser, jwtToken); - - } - - return new ResultModel() - { - Data = retval, - StatusCode = statuscode - }; - } - - public async Task> LoginAD(AuthenticateRequest model) - { - User myUser = null; - AuthenticateResponse retval = null; - int statuscode = 0; - ADConfig adConfig = new ADConfig() - { - LDAPPassword = _appSettings.LDAPPassword, - LDAPPath = _appSettings.LDAPPath, - LDAPUser = _appSettings.LDAPUser, - ADTimeOutStr = _appSettings.ADTimeOut - }; - myUser = await Task.Run(() => LoginADStaff(adConfig, model.Username, model.Password)); - if (!string.IsNullOrEmpty(myUser.Username)) - { - statuscode = 1; - // authentication successful so generate jwt token - var jwtToken = _jwtUtils.GenerateJwtToken(myUser); - - retval = new AuthenticateResponse(myUser, jwtToken); - } - return new ResultModel() - { - Data = retval, - StatusCode = statuscode - }; - } - - public async Task>> GetAllUsers() - { - var retval = await _context.Users.ToListAsync(); - return new ResultModel>() - { - Data = retval, - StatusCode = 1 - }; - - } - - public async Task> GetById(int id) - { - var user = await _context.Users.FindAsync(id); - if (user == null) throw new KeyNotFoundException("User not found"); - return new ResultModel() - { - Data = user, - StatusCode = 1 - }; - } - public async Task> SearchApiStaff(string staffLinkNo) - { - User myUser = null; - int statuscode = 0; - string webAPIUrl = _appSettings.LoginWebAPI; - var loginInfo = await CommonAD.WebAPIUtil.SearchStaffAsync(webAPIUrl, "SearchStaff", staffLinkNo); - if (loginInfo != null && loginInfo.result) - { - myUser = new User(); - myUser.Email = loginInfo.email; - myUser.FirstName = loginInfo.firstName; - myUser.LastName = loginInfo.lastName; - myUser.Username = loginInfo.stafflinkNo; - statuscode = 1; - } - - return new ResultModel() - { - Data = myUser, - StatusCode = statuscode - }; - // return myUser; - - } - - public async Task> SearchADStaff(string staffLinkNo) - { - int statuscode = 0; - ADConfig adConfig = new ADConfig() - { - LDAPPassword = _appSettings.LDAPPassword, - LDAPPath = _appSettings.LDAPPath, - LDAPUser = _appSettings.LDAPUser, - ADTimeOutStr = _appSettings.ADTimeOut - }; - User myUser = await Task.Run(() => SearchADStaff(adConfig, staffLinkNo)); - if (!string.IsNullOrEmpty(myUser.Username)) - statuscode = 1; - return new ResultModel() - { - Data = myUser, - StatusCode = statuscode - }; - - - } - private User SearchADStaff(ADConfig adConfig, string username) - { - - ADStaffLink staffLink = new ADStaffLink(adConfig); - MyADObject myADObj = staffLink.SearchStaff(username); - User user = new() - { - Username = myADObj.stafflinkNo, - Email = myADObj.email, - FirstName = myADObj.firstName, - LastName = myADObj.lastName - }; - // myADObj.JobTitle; - return user; - - // return null; - } - private User LoginADStaff(ADConfig adConfig, string username, string password) - { - - ADStaffLink staffLink = new ADStaffLink(adConfig); - MyADObject myADObj = staffLink.CheckADCredentials(username, password); - User user = new() - { - Username = myADObj.stafflinkNo, - Email = myADObj.email, - FirstName = myADObj.firstName, - LastName = myADObj.lastName - }; - // myADObj.JobTitle; - return user; - - // return null; - } - public async Task> GetAllUserReport() - { - return new ResultModel - { - Data = null, - StatusCode = 1 - }; - } - } -} \ No newline at end of file diff --git a/Repository/UserRepository.cs b/Repository/UserRepository.cs index c7018ac..66d7370 100644 --- a/Repository/UserRepository.cs +++ b/Repository/UserRepository.cs @@ -112,22 +112,70 @@ namespace CarParkValidationAPI.Repository { User myUser = null; AuthenticateResponse retval = null; + string error = "user name is not in DB or user Name profile is not generic"; int statuscode = 0; - ADConfig adConfig = new ADConfig() + string webAPIUrl = _appSettings.LoginWebAPI; + //KCO, D204KCO + //now check the adminuser table + //ward clerk + try { - LDAPPassword = _appSettings.LDAPPassword, - LDAPPath = _appSettings.LDAPPath, - LDAPUser = _appSettings.LDAPUser, - ADTimeOutStr = _appSettings.ADTimeOut - }; - myUser = await Task.Run(() => LoginADStaff(adConfig,model.Username, model.Password)); - if (!string.IsNullOrEmpty(myUser.Username)) - { - statuscode = 1; - // authentication successful so generate jwt token - var jwtToken = _jwtUtils.GenerateJwtToken(myUser); - - retval = new AuthenticateResponse(myUser, jwtToken); + var loginInfo = await CommonAD.WebAPIUtil.PostLoginADAsync(webAPIUrl, "CheckCredentials", model.Username, model.Password); + if (loginInfo != null && loginInfo.result) + { + myUser = new User(); + myUser.Email = loginInfo.email; + myUser.FirstName = loginInfo.firstName; + myUser.LastName = loginInfo.lastName; + myUser.Username = loginInfo.stafflinkNo; + //myUser.Department = loginInfo.department; + //myUser.Position = loginInfo.jobTitle; + //myUser.ManagerEmail = loginInfo.managerEmail; + //myUser.Phone = loginInfo.phone; + //now check the adminuser table + var user = _context.AdminUsers. + SingleOrDefault(x => x.StafflinkNo == model.Username + && true == x.Active); + if (user != null) + { + // myUser.Role = user.RoleType; + myUser.Id = user.UserID; + myUser.Role = user.RoleType; + //myUser.ValidationPointId = user.ValidationPointID ?? 0; + statuscode = 1; + } + else // allow + { + statuscode = 1; + myUser.Role = AdminRole.Normal; + myUser.Id = -1; + + //error = "user name does not exist in adminUser"; + } + // validate + // if (user == null || !BCryptNet.Verify(model.Password, user.PasswordHash)) + + // authentication successful so generate jwt token + if (statuscode == 1) + { + var jwtToken = _jwtUtils.GenerateJwtToken(myUser); + retval = new AuthenticateResponse(myUser, jwtToken); + error = ""; + } + } + else + { + error = "user name or password is not correct"; + } + + + } + catch (Exception ex) + { + retval = null; + error = ex.ToString(); + statuscode = 0; + throw ex; } return new ResultModel() { @@ -185,58 +233,36 @@ namespace CarParkValidationAPI.Repository public async Task> SearchADStaff(string staffLinkNo) { int statuscode = 0; - ADConfig adConfig = new ADConfig() + User myUser = null; + AuthenticateResponse retval = null; + + + string webAPIUrl = _appSettings.LoginWebAPI; + var loginInfo = await CommonAD.WebAPIUtil.SearchStaffAsync(webAPIUrl, "SearchStaff", staffLinkNo); + if (!string.IsNullOrEmpty(loginInfo.firstName)) { - LDAPPassword = _appSettings.LDAPPassword, - LDAPPath = _appSettings.LDAPPath, - LDAPUser = _appSettings.LDAPUser, - ADTimeOutStr = _appSettings.ADTimeOut - }; - User myUser = await Task.Run(() => SearchADStaff(adConfig, staffLinkNo)); - if (!string.IsNullOrEmpty(myUser.Username)) + myUser = new(); + myUser.FirstName = loginInfo.firstName; + myUser.LastName = loginInfo.lastName; + myUser.Username = staffLinkNo; + myUser.Email = loginInfo.email; + /* + myUser.Department = loginInfo.department; + myUser.Position = loginInfo.jobTitle; + myUser.Phone = loginInfo.phone; + myUser.ManagerEmail = loginInfo.managerEmail; + */ statuscode = 1; + } return new ResultModel() { Data = myUser, StatusCode = statuscode }; - - } - private User SearchADStaff(ADConfig adConfig, string username) - { - - ADStaffLink staffLink = new ADStaffLink(adConfig); - MyADObject myADObj = staffLink.SearchStaff(username); - User user = new() - { - Username = myADObj.stafflinkNo, - Email = myADObj.email, - FirstName = myADObj.firstName, - LastName = myADObj.lastName - }; - // myADObj.JobTitle; - return user; - - // return null; - } - private User LoginADStaff(ADConfig adConfig, string username, string password) - { - - ADStaffLink staffLink = new ADStaffLink(adConfig); - MyADObject myADObj = staffLink.CheckADCredentials(username,password); - User user = new() - { - Username = myADObj.stafflinkNo, - Email = myADObj.email, - FirstName = myADObj.firstName, - LastName = myADObj.lastName - }; - // myADObj.JobTitle; - return user; - - // return null; } + + private string GetVPDesc(int? idn) { string result = ""; diff --git a/Startup.cs b/Startup.cs index 508f14e..c6fec3d 100644 --- a/Startup.cs +++ b/Startup.cs @@ -66,7 +66,7 @@ namespace CarParkValidationAPI }; }); - services.AddAutoMapper(typeof(Startup)); + // services.AddAutoMapper(typeof(Startup)); //** //publish need to take comment out