Mike Phares b586da5c82 Removed PdfViewController, HtmlViewRenderer and FakeView to be replaced with ViewEngineResult Render method
Added HttpException class for missing HttpException for net8

Wrapped HttpContext.Session, GetJsonResult, IsAjaxRequest and GetUserIdentityName in controllers for net8

Added AuthenticationService to test Fab2ApprovalMKLink code for net8

Compile conditionally flags to debug in dotnet core
2025-05-23 12:27:09 -07:00

311 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Dapper;
using Fab2ApprovalSystem.Misc;
using Fab2ApprovalSystem.Models;
namespace Fab2ApprovalSystem.DMO;
public class AdminDMO {
private readonly IDbConnection db = new SqlConnection(GlobalVars.DB_CONNECTION_STRING);
#if !NET8
private static FabApprovalTrainingEntities FabApprovalDB = new FabApprovalTrainingEntities();
#endif
public List<ParentChildModel> GetAllSubRoles(string showInactiveRoles = "") {
List<ParentChildModel> results = new();
List<Role> roles = GetSubRoles();
ParentChildModel child;
ParentChildModel parent;
foreach (Role r in roles) {
parent = new ParentChildModel {
id = r.RoleID,
parentid = -1,
text = r.RoleName,
value = r.RoleID.ToString()
};
foreach (SubRole sr in r.SubRoles) {
if (sr.Inactive) {
// hide inactive roles unless parameter says otherwise
if (!showInactiveRoles.Equals("true"))
continue;
}
child = new ParentChildModel {
id = sr.SubRoleID,
parentid = r.RoleID,
text = sr.SubRoleCategoryItem + (sr.Inactive ? " (Inactive)" : ""),
value = sr.SubRoleID.ToString()
};
results.Add(child);
}
results.Add(parent);
};
return results;
}
public List<Role> GetSubRoles() {
StringBuilder sql = new();
sql.Append(
"SELECT R.RoleID, R.RoleName, SubRoleID, SubRoleCategoryItem, SR.RoleID, SR.Inactive " +
"FROM vSubRoles SR " +
"INNER JOIN Role R ON R.RoleID = SR.RoleID " +
"ORDER BY R.RoleID, SubRoleCategoryItem ");
db.Open();
Dictionary<int, Role> lookup = new();
List<Role> data = db.Query<Role, SubRole, Role>(sql.ToString(),
(parent, child) => {
Role role;
if (!lookup.TryGetValue(parent.RoleID, out role)) {
lookup.Add(parent.RoleID, role = parent);
}
role.SubRoles.Add(child);
return role;
},
splitOn: "SubRoleID").Distinct().ToList();
return data;
}
public List<UserSubRoles> GetUserSubRoles(int userId) {
DynamicParameters parameters = new();
parameters.Add("@UserId", userId);
List<UserSubRoles> userSubRoleList = db.Query<UserSubRoles>("GetSubRolesByUserId", parameters, commandType: CommandType.StoredProcedure).ToList();
return userSubRoleList;
}
public IEnumerable<LoginModel> GetAllUsersBySubRole(int subRole) {
StringBuilder sql = new();
sql.Append("SELECT FirstName + ' ' + LastName AS FullName , LoginID, FirstName, LastName, U.UserID, SubRoleID ");
sql.Append("FROM UserSubRole UR ");
sql.Append("INNER JOIN Users U ON UR.UserID = U.UserID ");
sql.Append("WHERE UR.SubRoleID = " + subRole.ToString() + " ");
sql.Append("ORDER BY FirstName");
return db.Query<LoginModel>(sql.ToString()).ToList();
}
public void AddUserRoles(int subRole, string userids) {
string sql;
string[] arrayOfUsers = userids.Split(new char[] { '~' });
for (int i = 0; i < arrayOfUsers.Length; i++) {
sql = "INSERT INTO UserSubRole (UserID, SubRoleID) VALUES (" + arrayOfUsers[i] + ", " + subRole + " )";
db.Execute(sql);
}
}
public void DeleteUserFromAllTrainingGroups(int userId) {
string sql = "DELETE FROM TrainingGroupMembers WHERE UserId = " + userId;
db.Open();
db.Execute(sql);
return;
}
public void DeleteUserRoles(int subRole, string userids) {
string sql;
string[] arrayOfUsers = userids.Split(new char[] { '~' });
for (int i = 0; i < arrayOfUsers.Length; i++) {
sql = "DELETE FROM UserSubRole WHERE UserID = " + arrayOfUsers[i] + " AND SubRoleID = " + subRole;
db.Execute(sql);
}
}
#if !NET8
public List<TrainingReportUser> GetTrainingReportUsers() {
List<TrainingReportUser> CurrentReportUsers = (from a in FabApprovalDB.TrainingReportUsers select a).ToList();
return CurrentReportUsers;
}
#endif
#if NET8
public List<TrainingReportUser> GetTrainingReportUsers() =>
throw new NotImplementedException();
#endif
#if !NET8
public List<TECNNotificationsUser> GetTECNNotificationUsers() {
List<TECNNotificationsUser> currentTECNNotificationUsers = (from a in FabApprovalDB.TECNNotificationsUsers select a).ToList();
return currentTECNNotificationUsers;
}
#endif
public void TrainingReportAddUser(int userId) {
string sql = "INSERT INTO TrainingReportUsers (UserId) " + "VALUES ('" + userId + "') ";
db.Open();
db.Execute(sql);
return;
}
public void TECNExpirationAddUser(int userId) {
string sql = "INSERT INTO TECNNotificationsUsers (UserId) " + "VALUES ('" + userId + "') ";
db.Open();
db.Execute(sql);
return;
}
public void TrainingReportDeleteUser(int userId) {
DynamicParameters parameters = new();
parameters.Add("@UserID", userId);
db.Execute("DeleteUserFromTrainingReport", parameters, commandType: CommandType.StoredProcedure);
return;
}
public void TECNExpirationDeleteUser(int userId) {
DynamicParameters parameters = new();
parameters.Add("@UserID", userId);
db.Execute("DeleteUserFromTECNReport", parameters, commandType: CommandType.StoredProcedure);
return;
}
public List<TrainingGroup> GetTrainingGroups() {
#if !NET8
var TrainingGroups = from a in FabApprovalDB.TrainingGroups select a;
List<TrainingGroup> GroupsToReturn = TrainingGroups.ToList();
return GroupsToReturn;
#endif
#if NET8
throw new NotImplementedException();
#endif
}
#if !NET8
public void AddNewTrainingGroup(string groupName) {
TrainingGroup existing = null;
// Check to see that the group name doesn't exist.
try {
existing = (from a in FabApprovalDB.TrainingGroups where a.TrainingGroupName == groupName select a).FirstOrDefault();
} catch {
}
if (existing == null) {
string sql = "INSERT INTO TrainingGroups (TrainingGroupName) " + "VALUES ('" + groupName + "') ";
this.db.Open();
this.db.Execute(sql);
return;
} else {
return;
}
}
#endif
public void DeleteTrainingGroup(int groupID) {
try {
string sql = "DELETE FROM TrainingGroups WHERE TrainingGroupID = " + groupID;
db.Open();
db.Execute(sql);
sql = "DELETE FROM TrainingGroupMembers WHERE TrainingGroupID = " + groupID;
db.Execute(sql);
return;
} catch {
}
}
#if !NET8
public List<TrainingGroupMember> GetTrainingGroupMembers(int GroupID) {
return (from a in FabApprovalDB.TrainingGroupMembers where a.TrainingGroupID == GroupID select a).ToList();
}
public void AddUserToGroup(int userId, int groupId) {
UserAccountDMO userDB = new UserAccountDMO();
string userFullName = userDB.GetUserByID(userId).FullName;
TrainingGroupMember existing = null;
existing = (from a in FabApprovalDB.TrainingGroupMembers where a.TrainingGroupID == groupId && a.UserID == userId select a).FirstOrDefault();
if (existing == null) {
var parameters = new DynamicParameters();
parameters = new DynamicParameters();
parameters.Add("@GroupID", groupId);
parameters.Add("@UserID", userId);
parameters.Add("@UserFullName", userFullName);
this.db.Execute("AddUserToTrainingGroup", parameters, commandType: CommandType.StoredProcedure);
} else {
throw new Exception("The user already exists in this training group.");
}
}
#endif
public void DeleteFromGroup(int userId, int groupId) {
DynamicParameters parameters = new();
parameters.Add("@GroupID", groupId);
parameters.Add("@UserID", userId);
db.Execute("DeleteUserFromTrainingGroup", parameters, commandType: CommandType.StoredProcedure);
return;
}
public void DeleteUser(UserAccountDMO userDMO, TrainingDMO trainingDMO, LoginModel loginModel) {
if (loginModel != null) {
userDMO.DeleteUser(loginModel);
// Remove open trainings
// Get a list of all user assigned trainings.
List<TrainingAssignment> trainingAssignments = trainingDMO.GetTrainingAssignmentsByUserID(loginModel.UserID);
// Go Through that list.
foreach (var trainingAssignment in trainingAssignments) {
// Delete Any document acknowledgements.
trainingDMO.DeleteTrainingDocAck(trainingAssignment.ID);
// Delete the training assignment itself
trainingDMO.DeleteTrainingAssignment(trainingAssignment.ID);
// Check the parent Training task to set to to complete if applicable.
if (trainingDMO.CheckTrainingStatus(trainingAssignment.ID)) {
int TrainingID = trainingAssignment.TrainingID;
// Set Training status to complete
trainingDMO.UpdateTrainingStatus(TrainingID);
}
}
// Remove user from any Training Groups
DeleteUserFromAllTrainingGroups(loginModel.UserID);
// Remove User from training report notifications
TrainingReportDeleteUser(loginModel.UserID);
// Remove user from TECN Expiration Notifications
TECNExpirationDeleteUser(loginModel.UserID);
// Get user subroles
List<UserSubRoles> userSubRoles = GetUserSubRoles(loginModel.UserID);
// Delete user from any subroles
foreach (var userSubRole in userSubRoles) {
DeleteUserRoles(userSubRole.SubRoleID, loginModel.UserID.ToString());
}
}
}
}