public static List <ModuleVm> GetModulesList(int empId) { List <ModuleVm> moduleVmList = new List <ModuleVm>(); SetupEntities db = new SetupEntities(); var actionsList = db.SecActionLibs.ToList(); var moduleDbList = db.Modules.Include("ModuleScreens").ToList(); Mapper.CreateMap <Module, ModuleVm>().IgnoreAllNonExisting(); Mapper.CreateMap <ModuleScreen, ModuleScreenVm>().IgnoreAllNonExisting(); Mapper.CreateMap <ScreenAction, ScreenActionVM>().IgnoreAllNonExisting(); Mapper.CreateMap <ScreenActionUser, ScreenActionUserVm>().IgnoreAllNonExisting(); Mapper.Map(moduleDbList, moduleVmList); string userId = GetUserIdByEmpId(empId); int actionId, screenId; foreach (var moduleVmObj in moduleVmList) { foreach (var screenVmObj in moduleVmObj.ModuleScreens) { screenId = screenVmObj.ScreenId; foreach (var action in screenVmObj.ScreenActions) { actionId = action.ActionId; action.ActionName = actionsList.Where(x => x.ActionId == actionId).FirstOrDefault().ActionName; action.Selected = screenVmObj.ScreenActionUsers .Any(x => x.ActionId == actionId && x.UserId == userId && x.ScreenId == screenId) ? "selected" : ""; } } } return(moduleVmList); }
public static List <PrefixSetup> GetPrefixSetupList() { SetupEntities db = new SetupEntities(); var prefixSetupList = db.PrefixSetups.ToList(); return(prefixSetupList); }
public static bool IsSuperUser(string userId) { SetupEntities db = new SetupEntities(); bool isExists = db.AspNetUserRoles.Any(x => x.UserId == userId && x.RoleId == "1"); return(isExists); }
internal static List <UserVm> GetUsersList() { SetupEntities db = new SetupEntities(); List <UserVm> userListVm = new List <UserVm>(); var userListDb = db.AspNetUsers.ToList(); var userEmps = db.UserEmployees.ToList(); Mapper.CreateMap <AspNetUser, UserVm>().IgnoreAllNonExisting(); Mapper.Map(userListDb, userListVm); HREntities db1 = new HREntities(); List <Employee> empList = db1.Employees.Include("Department").ToList(); int empId; Employee emp; foreach (var item in userListVm) { empId = userEmps.Where(x => x.UserId == item.Id).FirstOrDefault().EmpId; emp = empList.Where(x => x.EmpId == empId).FirstOrDefault(); item.EmpId = empId; item.EmpNameEn = emp.EmpNameEn; item.DepNameEn = emp.Department.DepNameEn; } return(userListVm); }
/// <summary> /// Will call this in case of insert new record is falied /// </summary> /// <param name="prefixFor">Enum PrefixFor</param> public static void LastIdRemoveOne(PrefixForEnum prefixFor) { SetupEntities db = new SetupEntities(); int prefixForId = (int)prefixFor; var lastIndexObj = db.PrefixLastIds.Where(x => x.PrefixForId == prefixForId).FirstOrDefault(); lastIndexObj.LastId = lastIndexObj.LastId - 1; db.SaveChanges(); }
public static bool CheckUserAction(ScreenEnum screen, ActionEnum action) { bool hasRights = false; SetupEntities db = new SetupEntities(); string userId = HttpContext.Current.User.Identity.GetUserId(); int screenId = (int)screen; int actionId = (int)action; hasRights = db.ScreenActionUsers .Any(x => x.ScreenId == screenId && x.ActionId == actionId && x.UserId == userId); return(hasRights); }
public static string GetUserIdByEmpId(int empId) { SetupEntities db = new SetupEntities(); string userId = ""; var userEmpObj = db.UserEmployees.Where(x => x.EmpId == empId).FirstOrDefault(); if (userEmpObj != null) { userId = userEmpObj.UserId; } return(userId); }
public static string AddEditPrefix(FormCollection form) { string isSaved = "true"; SetupEntities db = new SetupEntities(); var count = db.PrefixSetups.Count(); PrefixSetup prefixSetupDb; for (int i = 0; i < count - 1; i++) { //prefix for Id int prefixForId = int.Parse(form["PrefixSetup[" + i + "].PrefixForId"]); prefixSetupDb = db.PrefixSetups.Where(x => x.PrefixForId == prefixForId).FirstOrDefault(); prefixSetupDb.Delimiter = form["PrefixSetup[" + i + "].Delimiter"]; prefixSetupDb.IncludeMonth = bool.Parse(form["PrefixSetup[" + i + "].IncludeMonth"]); prefixSetupDb.IncludeYear = bool.Parse(form["PrefixSetup[" + i + "].IncludeYear"]); prefixSetupDb.NumberAfterChar = bool.Parse(form["PrefixSetup[" + i + "].NumberAfterChar"]); prefixSetupDb.PrefixChar = form["PrefixSetup[" + i + "].PrefixChar"]; prefixSetupDb.ResetNumberEvery = form["PrefixSetup[" + i + "].ResetNumberEvery"]; } db.SaveChanges(); return(isSaved); }
public static string AssignUserIdToEmpId(int empId, string userId) { string isSaved = "true"; SetupEntities db = new SetupEntities(); UserEmployee userEmp = new UserEmployee() { EmpId = empId, UserId = userId }; db.UserEmployees.Add(userEmp); try { db.SaveChanges(); } catch (Exception ex) { isSaved = "false" + ex.Message; throw; } return(isSaved); }
public static string GeneratePrefixCode(PrefixForEnum prefixFor, bool forSubmit) { StringBuilder prefixCode = new StringBuilder(); SetupEntities db = new SetupEntities(); int prefixForId = (int)prefixFor; PrefixSetup prefixSetup = db.PrefixSetups.Include("PrefixLastId") .Where(x => x.PrefixForId == prefixForId).FirstOrDefault(); //Take the Id PrefixLastId prefixLastIdObj = prefixSetup.PrefixLastId; string currentId = prefixLastIdObj.LastId.ToString(); //Reset Id every DateTime lastUpdateDate = prefixLastIdObj.LastUpdateDate.Value; if (prefixSetup.ResetNumberEvery == "month") { //If reset is active .. must include month and year .. even if it was wrong inseted in the setup // to avoid duplicate codes prefixSetup.IncludeMonth = true; prefixSetup.IncludeYear = true; if (lastUpdateDate.Month != DateTime.Now.Month) // New record in new month .. reset the ID { currentId = "1"; } } if (prefixSetup.ResetNumberEvery == "year") { //If reset is active .. must include year .. even if it was wrong inseted in the setup // to avoid duplicate codes // prefixSetup.IncludeMonth = true; prefixSetup.IncludeYear = true; if (lastUpdateDate.Year != DateTime.Now.Year) // New record in new year .. reset the ID { currentId = "1"; } } if (!forSubmit) { currentId = "??"; } //build the prefix if (prefixSetup.NumberAfterChar.Value) { prefixCode.Append(prefixSetup.PrefixChar); prefixCode.Append(currentId); } else { prefixCode.Append(currentId); prefixCode.Append(prefixSetup.PrefixChar); } if (prefixSetup.IncludeMonth.Value) { prefixCode.Append(prefixSetup.Delimiter); prefixCode.Append(DateTime.Now.Month); } if (prefixSetup.IncludeYear.Value) { prefixCode.Append(prefixSetup.Delimiter); prefixCode.Append(DateTime.Now.Year); } if (forSubmit) { if (prefixSetup.ResetNumberEvery == "never") { //update lastId = lastId +1 prefixLastIdObj.LastId = prefixLastIdObj.LastId + 1; } else if (prefixSetup.ResetNumberEvery == "month") { if (lastUpdateDate.Month != DateTime.Now.Month) // New record in new month .. reset the ID { prefixLastIdObj.LastId = 2; // one is alrady taken ..static codeing above } else // reset monthly but already Id rested by anoth record { prefixLastIdObj.LastId = prefixLastIdObj.LastId + 1; } } if (prefixSetup.ResetNumberEvery == "year") { if (lastUpdateDate.Year != DateTime.Now.Year) // New record in new year .. reset the ID { prefixLastIdObj.LastId = 2; // one is alrady taken ..static codeing above } else // reset yearly but already Id rested by anoth record { prefixLastIdObj.LastId = prefixLastIdObj.LastId + 1; } } prefixLastIdObj.LastUpdateDate = DateTime.Now; try { db.SaveChanges(); } catch { throw; } } return(prefixCode.ToString()); }
internal static string AddEditSecRights(System.Web.Mvc.FormCollection form) { string isSaved = "true"; int empId = int.Parse(form["EmpId"]); string userId = GetUserIdByEmpId(empId); //Will be added into AspNetUserRoles to allow edit for closde operations string isSuperUser = form["IsSuperUser"]; SetupEntities db = new SetupEntities(); ScreenActionUser actionUser; foreach (var key in form.AllKeys) { if (key.StartsWith("ActionId")) { string index = key.Split('[').ToArray()[1].Replace("]", ""); int screenId = int.Parse(form["ScreenId[" + index + "]"]); var userActionsDb = db.ScreenActionUsers.Where(x => x.UserId == userId && x.ScreenId == screenId).ToList(); foreach (var item in userActionsDb) { db.ScreenActionUsers.Remove(item); } string actionId = form[key]; if (actionId.Contains(",")) { string[] actionIds = actionId.Split(',').ToArray(); for (int i = 0; i < actionIds.Length; i++) { actionUser = new ScreenActionUser() { ActionId = int.Parse(actionIds[i]), ScreenId = screenId, UserId = userId }; db.ScreenActionUsers.Add(actionUser); } } else { actionUser = new ScreenActionUser() { ActionId = int.Parse(actionId), ScreenId = screenId, UserId = userId }; db.ScreenActionUsers.Add(actionUser); } } } //Check if added as super user before bool isExists = db.AspNetUserRoles.Any(x => x.UserId == userId && x.RoleId == "1"); if (!isExists && isSuperUser.ToLower() == "true") { AspNetUserRole aspNetUserRole = new AspNetUserRole(); aspNetUserRole.RoleId = "1"; aspNetUserRole.UserId = userId; db.AspNetUserRoles.Add(aspNetUserRole); } else if (isExists && isSuperUser.ToLower() == "false") // removed from super user role { var aspNetUserRole = db.AspNetUserRoles.Where(x => x.UserId == userId && x.RoleId == "1").FirstOrDefault(); db.AspNetUserRoles.Remove(aspNetUserRole); } try { db.SaveChanges(); } catch (Exception ex) { isSaved = "false " + ex.Message; } return(isSaved); }