358 lines
13 KiB
C#
358 lines
13 KiB
C#
//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 SpreadsheetLight;
|
|
using System.IO;
|
|
using System;
|
|
|
|
namespace CarParkValidationAPI.Repository
|
|
{
|
|
public class UserRepository : IUserService
|
|
{
|
|
private DataContext _context;
|
|
private IJwtUtils _jwtUtils;
|
|
private readonly AppSettings _appSettings;
|
|
private readonly IValidationPoint _validationPoint;
|
|
private Dictionary<int, LookupCodeDto> _validationPointDic;
|
|
|
|
public UserRepository(
|
|
DataContext context,
|
|
IJwtUtils jwtUtils,
|
|
IValidationPoint validationPoint,
|
|
IOptions<AppSettings> appSettings)
|
|
{
|
|
_context = context;
|
|
_jwtUtils = jwtUtils;
|
|
_validationPoint = validationPoint;
|
|
_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<ResultModel<AuthenticateResponse>> LoginApiAD(AuthenticateRequest model)
|
|
{
|
|
User myUser = null;
|
|
AuthenticateResponse retval = null;
|
|
int statuscode = 0;
|
|
string error = "";
|
|
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 = user.RoleType;
|
|
myUser.Id = user.UserID;
|
|
myUser.ValidationPointId = user.ValidationPointID??0;
|
|
statuscode = 1;
|
|
}
|
|
else //not allow
|
|
{
|
|
statuscode = 0;
|
|
error = "username is not exist in admimuser.";
|
|
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);
|
|
|
|
}
|
|
else
|
|
error = "error username or password is invalid";
|
|
|
|
return new ResultModel<AuthenticateResponse>()
|
|
{
|
|
Data = retval,
|
|
StatusCode = statuscode,
|
|
Message = error
|
|
};
|
|
}
|
|
|
|
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<List<User>>> GetAllUsers()
|
|
{
|
|
var retval = await _context.Users.ToListAsync();
|
|
return new ResultModel<List<User>>()
|
|
{
|
|
Data = retval,
|
|
StatusCode = 1
|
|
};
|
|
|
|
}
|
|
|
|
public async Task<ResultModel<User>> GetById(int id)
|
|
{
|
|
var user = await _context.Users.FindAsync(id);
|
|
if (user == null) throw new KeyNotFoundException("User not found");
|
|
return new ResultModel<User>()
|
|
{
|
|
Data = user,
|
|
StatusCode = 1
|
|
};
|
|
}
|
|
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)
|
|
{
|
|
|
|
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 = "";
|
|
int id = idn ?? 1;
|
|
if (_validationPointDic.ContainsKey(id))
|
|
{
|
|
result = _validationPointDic[id].Description;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<ResultModel<FileContent>> GetAllUserReport()
|
|
{
|
|
//excel SLDocument sl = new SLDocument();
|
|
int statusCode = 1;
|
|
AdminUser item;
|
|
int col = 1;
|
|
int row = 1;
|
|
SLDocument sl = new SLDocument();
|
|
var ret = await _validationPoint.GetValidationPointDicAsync();
|
|
if (ret.StatusCode == 1)
|
|
_validationPointDic = ret.Data;
|
|
//first sheetName
|
|
sl.RenameWorksheet(SLDocument.DefaultFirstSheetName, "All User");
|
|
SLStyle style = sl.CreateStyle();
|
|
style.SetFontUnderline(DocumentFormat.OpenXml.Spreadsheet.UnderlineValues.Single);
|
|
style.SetFontBold(true);
|
|
style.SetFont("Arial Black", 16);
|
|
sl.SetCellStyle(1, 5, style);
|
|
sl.SetRowHeight(1, 1, 25);
|
|
sl.SetCellValue(row++, 5, "The User Report");
|
|
|
|
sl.SetCellValue(1, 6, " " + DateTime.Now.ToString("ddd dd/MM/yyyy HH:mm"));
|
|
|
|
int startTitleRow = 3;
|
|
row = startTitleRow;
|
|
//make the title bold
|
|
style = sl.CreateStyle();
|
|
style.Font.Bold = true;
|
|
sl.SetRowStyle(startTitleRow, style);
|
|
//set it height
|
|
sl.SetRowHeight(startTitleRow, startTitleRow, 25);
|
|
// add the title first
|
|
sl.SetCellValue(row, col++, "Stafflink ID");
|
|
sl.SetCellValue(row, col++, "Name");
|
|
sl.SetCellValue(row, col++, "Role");
|
|
sl.SetCellValue(row, col++, "Validation Point");
|
|
sl.SetCellValue(row, col++, "Active");
|
|
|
|
sl.FreezePanes(row,0); //frozen the row.
|
|
//wrap text on is concession Expiry
|
|
style = sl.CreateStyle();
|
|
style.SetWrapText(true);
|
|
sl.SetColumnStyle(4, style);
|
|
|
|
//format date
|
|
|
|
style = sl.CreateStyle();
|
|
style.FormatCode = "dd/MM/yyyy HH:MM";
|
|
sl.SetColumnStyle(1, style);
|
|
//make column width
|
|
sl.SetColumnWidth(1, 20);
|
|
sl.SetColumnWidth(2, 50);
|
|
sl.SetColumnWidth(3, 20);
|
|
sl.SetColumnWidth(4, 30);
|
|
sl.SetColumnWidth(5, 20);
|
|
/*
|
|
public enum AdminRole
|
|
{
|
|
Normal = 1,
|
|
Admin = 2
|
|
}
|
|
*/
|
|
int startRowFrom = startTitleRow + 1;
|
|
var list = await _context.AdminUsers.ToListAsync();
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
col = 1;
|
|
item = list[i];
|
|
row = i + startRowFrom;
|
|
sl.SetCellValue(row, col++, item.StafflinkNo);
|
|
sl.SetCellValue(row, col++, item.Name);
|
|
sl.SetCellValue(row, col++, (int) item.RoleType == 1 ? "Normal": "Admin");
|
|
sl.SetCellValue(row, col++, GetVPDesc(item.ValidationPointID) );
|
|
sl.SetCellValue(row, col++, item.Active ? "Yes" : "No");
|
|
|
|
}
|
|
byte[] array;
|
|
// sl.SaveAs("C:\\Temp\\Report\\ConcessionValidationDate" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx");
|
|
using (MemoryStream ws = new MemoryStream())
|
|
{
|
|
sl.SaveAs(ws);
|
|
array = ws.ToArray();
|
|
/*
|
|
using (FileStream fs = new FileStream("c:\\temp\\Report\\conReport.xlsx",FileMode.Create,FileAccess.Write))
|
|
{
|
|
ws.WriteTo(fs);
|
|
fs.Close();
|
|
}
|
|
*/
|
|
ws.Close();
|
|
}
|
|
|
|
var result = new FileContent
|
|
{
|
|
Content = array,
|
|
FileName = "UserReport" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm") + ".xlsx",
|
|
|
|
};
|
|
|
|
return new ResultModel<FileContent>
|
|
{
|
|
Data = result,
|
|
StatusCode = statusCode
|
|
};
|
|
|
|
}
|
|
}
|
|
} |