HRM-10/HRM.DA/DA/Users/UserRoleDA.cs
2026-11-17 14:52:13 +06:00

273 lines
13 KiB
C#

using HRM.BO;
using Ease.Core.DataAccess;
using System;
using System.Data;
namespace HRM.DA
{
internal class UserRoleDA
{
#region Constructor
private UserRoleDA()
{
}
#endregion
#region Insert function
internal static void Insert(TransactionContext tc, UserRole item)
{
//item.Status = EnumStatus.Inactive;
item.EventDate = DateTime.Now;
item.CreatedDate = DateTime.Now;
tc.ExecuteNonQuery(
"INSERT INTO UserRole(UserRoleID,RoleID,UserID,EventDate,CreatedBy,status,ComputerName,ApprovedComputerName)" +
" VALUES(%n, %n, %n,%D,%n,%n,%s,%s)", item.ID, item.RoleID, item.UserID, item.EventDate, item.CreatedBy,
(int)item.UserRoleStatus, item.ComputerName, item.ApprovedComputerName);
}
#endregion
#region Update function
internal static void Update(TransactionContext tc, UserRole item)
{
tc.ExecuteNonQuery(
"UPDATE UserRole SET roleID=%n, userID=%n, AuthorizedDate=%D,AuthorizedBy=%n,status=%n,ComputerName=%s,ApprovedComputerName=%s" +
" WHERE UserRoleID=%n", item.RoleID, item.UserID, DateTime.Now, item.UserID, item.UserRoleStatus,
item.ComputerName, item.ComputerName, item.ID);
}
#endregion
#region Get Function
internal static IDataReader Get(TransactionContext tc)
{
return tc.ExecuteReader("SELECT * FROM UserRole");
}
internal static IDataReader Get(TransactionContext tc, int RoleID)
{
return tc.ExecuteReader("SELECT * FROM UserRole WHERE RoleID=%n", RoleID);
}
internal static IDataReader Get(TransactionContext tc, EnumRoleType roleType, int? UserID, int? roleID)
{
string sqlClause = " ";
sqlClause = SQLParser.MakeSQL(" AND r.roleType = %n", roleType);
if (UserID != null)
{
sqlClause = SQLParser.TagSQL(sqlClause) + SQLParser.MakeSQL(" ur.UserID= %n", UserID);
}
if (roleID != null)
{
sqlClause = SQLParser.TagSQL(sqlClause) + SQLParser.MakeSQL(" ur.roleid= %n", roleID);
}
return tc.ExecuteReader(
@"SELECT ur.*, u.LOGINID LoginID, u.USERNAME UserName,R.Name RoleName, u.EmployeeID employeeID,
(SELECT e.PayrollTypeID FROM Employee e WHERE e.EmployeeID = u.EmployeeID) EmpPayrollTypeID
FROM UserRole ur,[Users] u, Role R
WHERE ur.RoleID=r.RoleID and u.UserID =ur.UserID %q order by u.LoginID,R.Name", sqlClause);
}
internal static IDataReader GetByUserID(TransactionContext tc, int UserID)
{
return tc.ExecuteReader("SELECT * FROM UserRole WHERE UserID=%n and Status=1", UserID);
}
internal static IDataReader getAllByUserEMpNo(TransactionContext tc, string empIn)
{
return tc.ExecuteReader(
@"SELECT ur.*, u.LoginID, r.Name RoleName, u.employeeID FROM UserRole ur, Users u, Employee e, Role r where
ur.UserID = u.UserId and u.LoginID= e.EmployeeNo and r.RoleID = ur.RoleID
and e.employeeid IN (%q)", empIn);
}
internal static IDataReader GetRoleRelateEmp(TransactionContext tc, string empIn)
{
return tc.ExecuteReader("SELECT ul.* FROM UserRole ul, Users u WHERE u.UserID= ul.UserID and u.LoginID=%s",
empIn);
}
internal static DataSet GetRoleAudit(TransactionContext tc, int type, DateTime fromDate, DateTime ToDate)
{
DataSet oIDs = new DataSet();
try
{
// string sSQL = SQLParser.MakeSQL(@"SELECT zr.*,(SELECT Username FROM users WHERE userid=zr.createdby) ActionBy,(SELECT Username FROM users WHERE userid=zr.UserID) UserName,r.Name RoleName
// FROM z_UserRole zr,[Role] r
// WHERE zr.RoleID = r.RoleID
// AND zr.AuditDate>=%d
// And zr.AuditDate<=%d AND zr.AuditType IN('Inserted','Update (after)') AND zr.RoleID=%n
// AND zr.[Status]<>12
// ORDER BY zr.AuditDate DESC", fromDate, ToDate,nRoleID);
string sSQL = SQLParser.MakeSQL(
@"SELECT zr.*,(SELECT Loginid FROM users WHERE userid=zr.createdby) ActionBy,(SELECT Username FROM users WHERE userid=zr.UserID) UserName,r.Name RoleName
FROM z_UserRole zr,Role r
WHERE zr.RoleID = r.RoleID
AND zr.AuditDate>=%d
And zr.AuditDate<=%d AND zr.AuditType IN('Inserted','Update (after)')
AND zr.Status<>12
ORDER BY zr.AuditDate DESC", fromDate, ToDate);
oIDs = tc.ExecuteDataSet(sSQL);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return oIDs;
}
#endregion
#region Delete function
internal static void Delete(TransactionContext tc, int id)
{
tc.ExecuteNonQuery("DELETE FROM UserRole WHERE RoleID=%n", id);
}
internal static void DeleteByUserID(TransactionContext tc, int nUserID)
{
tc.ExecuteNonQuery("DELETE FROM UserRole WHERE UserID=%n", nUserID);
}
#region Maker-checker (Authentication > User Role Permission, UserRoleMakerChecker_DB_Changes.sql)
// UserRole.Status: Approved (5) = live; NewNotYetApprove (2) = given, not live until approved;
// DeletedNotYetApprove (3) = being taken away, still live until approved.
// Maker gives a role: it waits for approval (Status 2). CreatedBy is the maker.
internal static void InsertPending(TransactionContext tc, int userRoleID, int roleID, int userID,
int makerID, DateTime requestDate)
{
tc.ExecuteNonQuery(
"INSERT INTO UserRole(UserRoleID, RoleID, UserID, EventDate, CreatedBy, Status, ComputerName, ApprovedComputerName)" +
" VALUES(%n, %n, %n, %D, %n, %n, %s, %s)",
userRoleID, roleID, userID, requestDate, makerID, (int)EnumAuthStatus.NewNotYetApprove, "", "");
}
// Every change below touches ONE UserRole row (by UserRoleID): the audit trigger dbo.truserrole
// handles a single row only, and a multi-row UPDATE fails inside it (Msg 512).
// Maker takes a live role away: it stays live (Status 3) until approved. ModifiedBy is the maker.
internal static void RequestRemoval(TransactionContext tc, int userRoleID, int makerID, DateTime requestDate)
{
tc.ExecuteNonQuery(
"UPDATE UserRole SET Status=%n, ModifiedBy=%n, ModifiedDate=%D WHERE UserRoleID=%n",
(int)EnumAuthStatus.DeletedNotYetApprove, makerID, requestDate, userRoleID);
}
// Checker approves a given role: it becomes live. AuthorizedBy / AuthorizedDate are the checker.
internal static void ApproveRow(TransactionContext tc, int userRoleID, int approverID, DateTime approveDate)
{
tc.ExecuteNonQuery(
"UPDATE UserRole SET Status=%n, AuthorizedBy=%n, AuthorizedDate=%D WHERE UserRoleID=%n",
(int)EnumAuthStatus.Approved, approverID, approveDate, userRoleID);
}
// Checker rejects a removal: the role stays live as before.
internal static void RestoreRow(TransactionContext tc, int userRoleID)
{
tc.ExecuteNonQuery("UPDATE UserRole SET Status=%n WHERE UserRoleID=%n",
(int)EnumAuthStatus.Approved, userRoleID);
}
// Checker approves a removal, or rejects a given role: the row goes.
internal static void DeleteRow(TransactionContext tc, int userRoleID)
{
tc.ExecuteNonQuery("DELETE FROM UserRole WHERE UserRoleID=%n", userRoleID);
}
// Inside the maker's write transaction: counts the user's changes of one role type waiting for approval
// and keeps that range locked until commit, so a second maker sending at the same moment waits here
// and then sees the first change (instead of making a second pending change).
internal static int CountPendingLocked(TransactionContext tc, int userID, EnumRoleType roleType)
{
object o = tc.ExecuteScalar(
"SELECT COUNT(*) FROM UserRole WITH (UPDLOCK, HOLDLOCK)" +
" WHERE UserID=%n AND Status IN (%n, %n) AND RoleID IN (SELECT RoleID FROM Role WHERE RoleType=%n)",
userID, (int)EnumAuthStatus.NewNotYetApprove, (int)EnumAuthStatus.DeletedNotYetApprove, (int)roleType);
return o == null || o == DBNull.Value ? 0 : Convert.ToInt32(o);
}
// Same idea for a Self Service employee with no login yet: counts the employee's logins and keeps them
// locked, so two makers cannot both create a login for the same employee.
internal static int CountEmployeeLoginsLocked(TransactionContext tc, int employeeID)
{
object o = tc.ExecuteScalar(
"SELECT COUNT(*) FROM Users WITH (UPDLOCK, HOLDLOCK) WHERE EmployeeID=%n AND UserType=%n",
employeeID, (int)EnumUserType.Employee);
return o == null || o == DBNull.Value ? 0 : Convert.ToInt32(o);
}
internal static int CountByUser(TransactionContext tc, int userID)
{
object o = tc.ExecuteScalar("SELECT COUNT(*) FROM UserRole WHERE UserID=%n", userID);
return o == null || o == DBNull.Value ? 0 : Convert.ToInt32(o);
}
// UserRole rows with role, person and maker for the maker-checker screens. pendingOnly: only rows
// waiting for approval (Status 2 / 3). userID / roleType narrow it to one person's roles of one type.
internal static IDataReader GetForMakerChecker(TransactionContext tc, bool pendingOnly, int? userID, EnumRoleType? roleType)
{
string clause = "";
if (pendingOnly)
clause += SQLParser.MakeSQL(" AND ur.Status IN (%n, %n)",
(int)EnumAuthStatus.NewNotYetApprove, (int)EnumAuthStatus.DeletedNotYetApprove);
if (userID.HasValue)
clause += SQLParser.MakeSQL(" AND ur.UserID=%n", userID.Value);
if (roleType.HasValue)
clause += SQLParser.MakeSQL(" AND r.RoleType=%n", (int)roleType.Value);
return tc.ExecuteReader(
@"SELECT ur.UserRoleID, ur.RoleID, ur.UserID, ur.Status, ur.CreatedBy, ur.EventDate,
ur.ModifiedBy, ur.ModifiedDate, r.Name RoleName, r.RoleType,
u.LoginID, COALESCE(e.Name, u.UserName) PersonName, u.EmployeeID,
e.EmployeeNo, e.PayrollTypeID, mk.LoginID MakerLoginID
FROM UserRole ur
JOIN Role r ON r.RoleID = ur.RoleID
JOIN Users u ON u.UserID = ur.UserID
LEFT JOIN Employee e ON e.EmployeeID = u.EmployeeID
LEFT JOIN Users mk ON mk.UserID = CASE WHEN ur.Status = %n THEN ur.ModifiedBy ELSE ur.CreatedBy END
WHERE 1 = 1 %q
ORDER BY PersonName, r.Name",
(int)EnumAuthStatus.DeletedNotYetApprove, clause);
}
#endregion
internal static void Update(TransactionContext tc, int id, EnumStatus status)
{
tc.ExecuteNonQuery("UPDATE UserRole SET Status = %n,AuthorizedBy=%n,AuthorizedDate=%D WHERE UserID = %n",
status, id, DateTime.Now, id);
}
#endregion
internal static DataSet GetUserMenuByRole(TransactionContext tc, string roleId)
{
string sql = SQLParser.MakeSQL(
@"SELECT RP.ROLEID,RP.MENUKEY,CAN.VALUE Name,C.VALUE URL FROM ROLEPERMISSION RP,CONFIGURATIONATTRIBUTE CA,CONFIGURATION C,CONFIGURATIONATTRIBUTE CAN
WHERE C.NODE='formname' AND RP.MENUKEY=CA.VALUE AND CA.ATTRIBUTENAME='key' AND CA.PARENTID=C.PARENTID AND CAN.PARENTID=C.PARENTID AND CAN.ATTRIBUTENAME='name'
AND RP.ROLEID IN (%q) AND RP.ISAPPROVED=1 GROUP BY RP.ROLEID,RP.MENUKEY,CAN.VALUE,CA.PARENTID,C.VALUE ORDER BY RP.MENUKEY",
roleId);
return tc.ExecuteDataSet(sql);
}
}
}