first time include all file
This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
//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;
|
||||
|
||||
namespace CarParkValidationAPI.Repository;
|
||||
|
||||
public class UserServiceRepository : IUserService
|
||||
{
|
||||
private DataContext _context;
|
||||
private IJwtUtils _jwtUtils;
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public UserServiceRepository(
|
||||
DataContext context,
|
||||
IJwtUtils jwtUtils,
|
||||
IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_jwtUtils = jwtUtils;
|
||||
_appSettings = appSettings.Value;
|
||||
|
||||
}
|
||||
|
||||
public AuthenticateResponse Authenticate(AuthenticateRequest model)
|
||||
{
|
||||
User myUser = new();
|
||||
var adminUser = _context.AdminUsers.SingleOrDefault(x => x.StafflinkNo == model.Username);
|
||||
if (adminUser != null)
|
||||
{
|
||||
myUser.Id = adminUser.Id;
|
||||
myUser.Email = adminUser.Email;
|
||||
myUser.FirstName = adminUser.FirstName;
|
||||
myUser.LastName = adminUser.LastName;
|
||||
myUser.Role = adminUser.RoleType;
|
||||
}
|
||||
// validate
|
||||
// if (user == null || !BCryptNet.Verify(model.Password, user.PasswordHash))
|
||||
// if (adminUser == null || (model.Password != adminUser.PasswordHash))
|
||||
// throw new System.Exception("Username or password is incorrect");
|
||||
|
||||
// authentication successful so generate jwt token
|
||||
var jwtToken = _jwtUtils.GenerateJwtToken(myUser);
|
||||
|
||||
return new AuthenticateResponse(myUser, jwtToken);
|
||||
}
|
||||
|
||||
public async Task<ResultModel<AuthenticateResponse>> LoginApiAD(AuthenticateRequest model)
|
||||
{
|
||||
User myUser = null;
|
||||
AuthenticateResponse retval = null;
|
||||
int statuscode = 0;
|
||||
string webAPIUrl = _appSettings.LoginWebAPI;
|
||||
|
||||
|
||||
myUser = new User();
|
||||
|
||||
//now check the adminuser table
|
||||
var user = _context.AdminUsers.
|
||||
SingleOrDefault(x => x.StafflinkNo == model.Username
|
||||
&& x.Active == true);
|
||||
if (user != null)
|
||||
{
|
||||
myUser.Role = (int) user.RoleType;
|
||||
myUser.Id = user.Id;
|
||||
myUser.ValidationPointId = user.ValidationPointId??0;
|
||||
statuscode = 1;
|
||||
}
|
||||
else //not allow
|
||||
{
|
||||
statuscode = 0;
|
||||
myUser.Role = (int) 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<AuthenticateResponse>()
|
||||
{
|
||||
Data = retval,
|
||||
StatusCode = statuscode
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<AuthenticateResponse>> LoginApiAD_old(AuthenticateRequest model)
|
||||
{
|
||||
User myUser = null;
|
||||
AuthenticateResponse retval = null;
|
||||
int statuscode = 0;
|
||||
string webAPIUrl = _appSettings.LoginWebAPI;
|
||||
|
||||
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;
|
||||
//now check the adminuser table
|
||||
var user = _context.AdminUsers.
|
||||
SingleOrDefault(x => x.StafflinkNo == model.Username
|
||||
&& x.Active == true);
|
||||
if (user != null)
|
||||
{
|
||||
myUser.Role = (int) user.RoleType;
|
||||
myUser.Id = user.Id;
|
||||
myUser.ValidationPointId = user.ValidationPointId ?? 0;
|
||||
statuscode = 1;
|
||||
}
|
||||
else //not allow
|
||||
{
|
||||
statuscode = 0;
|
||||
myUser.Role = (int) 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<AuthenticateResponse>()
|
||||
{
|
||||
Data = retval,
|
||||
StatusCode = statuscode
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ResultModel<AuthenticateResponse>> 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<AuthenticateResponse>()
|
||||
{
|
||||
Data = retval,
|
||||
StatusCode = statuscode
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public async Task<ResultModel<User>> 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<User>()
|
||||
{
|
||||
Data = myUser,
|
||||
StatusCode = statuscode
|
||||
};
|
||||
// return myUser;
|
||||
|
||||
}
|
||||
|
||||
public async Task<ResultModel<User>> 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<User>()
|
||||
{
|
||||
Data = myUser,
|
||||
StatusCode = statuscode
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
private User SearchADStaff(ADConfig adConfig, string username)
|
||||
{
|
||||
|
||||
|
||||
MyADObject myADObj = new();
|
||||
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)
|
||||
{
|
||||
|
||||
|
||||
MyADObject myADObj = new();
|
||||
User user = new()
|
||||
{
|
||||
Username = myADObj.stafflinkNo,
|
||||
Email = myADObj.email,
|
||||
FirstName = myADObj.firstName,
|
||||
LastName = myADObj.lastName
|
||||
};
|
||||
// myADObj.JobTitle;
|
||||
return user;
|
||||
|
||||
// return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user