public List <SelectListItem> GetModulesPermissionForRole(int roleID) { List <SelectListItem> lstModule = null; try { lstModule = (from XX in _dbContext.Tbl_MP_Master_Module where XX.IsActive == true select new SelectListItem() { ID = XX.pk_ModuleId, Description = XX.ModuleName, IsActive = false }).ToList(); List <TBL_MP_Master_RoleModule> roleModules = (from xx in _dbContext.TBL_MP_Master_RoleModule where xx.FK_RoleId == roleID select xx).ToList(); foreach (SelectListItem item in lstModule) { TBL_MP_Master_RoleModule dbModule = roleModules.Where(x => x.FK_ModuleId == (int?)item.ID).FirstOrDefault(); if (dbModule != null) { item.IsActive = true; } } } catch (Exception ex) { string errMessage = ex.Message; if (ex.InnerException != null) { errMessage += string.Format("\n{0}", ex.InnerException.Message); } MessageBox.Show(errMessage, "ServiceRoles::GetModulesPermissionForRole", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(lstModule); }
//public List<SelectListItem> GetAllFormsForRoleModule(int roleID, int moduleID) //{ // List<SelectListItem> lstForms = new List<SelectListItem>(); // try // { // List<TBL_MP_Master_RoleForm> dbForms = (from xx in _dbContext.TBL_MP_Master_RoleForm // where xx.FK_RoleID == roleID && xx.FK_ModuleId == moduleID // select xx).ToList(); // foreach (TBL_MP_Master_RoleForm dbItem in dbForms) // { // SelectListItem item = new SelectListItem() // { // ID = dbItem.PK_RoleFormID, // }; // item.Description = dbItem.Tbl_MP_Master_Module_Forms.DisplayName; // lstForms.Add(item); // } // lstForms = lstForms.OrderBy(x => x.Description).ToList(); // } // catch (Exception ex) // { // MessageBox.Show(string.Format("{0}\n{1}", ex.Message, ex.InnerException.Message), "ServiceRoles::GetAllFormsForRoleModule", MessageBoxButtons.OK, MessageBoxIcon.Error); // } // return lstForms; //} //public List<MultiSelectListItem> GetMultiselectModuleFormsForRole(int roleID, int moduleID) //{ // List<MultiSelectListItem> lstForms = new List<MultiSelectListItem>(); // try // { // lstForms= (from xx in _dbContext.TBL_MP_Master_RoleForm.AsEnumerable() // where xx.FK_RoleID == roleID && xx.FK_ModuleId == moduleID // select new MultiSelectListItem() { // ID= xx.PK_RoleFormID, // Code= xx.FK_FormId.ToString(), // Description=xx.Tbl_MP_Master_Module_Forms.DisplayName, // EntityType= APP_ENTITIES.ROLE_MODULE_FORMS, // Selected=false // }).ToList(); // lstForms = lstForms.OrderBy(x => x.Description).ToList(); // } // catch (Exception ex) // { // string strError = ex.Message; // if (ex.InnerException != null) strError += "\n" + ex.InnerException.Message; // MessageBox.Show(strError, "ServiceRoles::GetMultiselectModuleFormsForRole", MessageBoxButtons.OK, MessageBoxIcon.Error); // } // return lstForms; //} public bool RemoveRoleModule(int roleModuleID) { bool result = false; try { int roleID = 0; int moduleID = 0; TBL_MP_Master_RoleModule model = _dbContext.TBL_MP_Master_RoleModule.Where(x => x.PK_RoleModuleID == roleModuleID).FirstOrDefault(); if (model != null) { roleID = model.FK_RoleId; moduleID = model.FK_ModuleId; _dbContext.TBL_MP_Master_RoleModule.Remove(model); _dbContext.SaveChanges(); string strQuery = string.Format("DELETE FROM TBL_MP_Master_RoleForm WHERE FK_ROLEID={0} AND FK_MODULEID={1}", roleID, moduleID); _dbContext.Database.ExecuteSqlCommand(strQuery); } } catch (Exception ex) { string errMessage = ex.Message; if (ex.InnerException != null) { errMessage += "\n" + ex.InnerException.Message; } MessageBox.Show(errMessage, "ServiceRoles::UpdateRoleModules", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(result); }
public bool UpdateRoleModules(int roleID, BindingList <MultiSelectListItem> roleModules) { bool result = false; try { // INSETRING MODULE NOT FOUND IN DATABASE foreach (MultiSelectListItem item in roleModules) { int moduelID = int.Parse(item.Code); TBL_MP_Master_RoleModule dbRoleModule = (from xx in _dbContext.TBL_MP_Master_RoleModule where xx.FK_RoleId == roleID && xx.FK_ModuleId == moduelID select xx).FirstOrDefault(); if (dbRoleModule == null) { _dbContext.TBL_MP_Master_RoleModule.Add(new TBL_MP_Master_RoleModule() { FK_RoleId = roleID, FK_ModuleId = moduelID }); _dbContext.SaveChanges(); } } // DELETING MODULE NOT FOUND IN COLLECTION String deleteIDs = string.Empty; List <TBL_MP_Master_RoleModule> dbItems = _dbContext.TBL_MP_Master_RoleModule.Where(x => x.FK_RoleId == roleID).ToList(); foreach (TBL_MP_Master_RoleModule item in dbItems) { MultiSelectListItem mItem = roleModules.Where(x => x.Code == item.FK_ModuleId.ToString()).FirstOrDefault(); if (mItem == null) { deleteIDs += item.FK_ModuleId.ToString() + DefaultStringSeperator; } } if (deleteIDs != string.Empty) { deleteIDs = deleteIDs.TrimEnd(DefaultStringSeperator).Replace(DefaultStringSeperator, ','); string strQuery = string.Format("DELETE FROM TBL_MP_Master_RoleModule WHERE FK_ROLEID={0} AND FK_MODULEID IN ({1})", roleID, deleteIDs); _dbContext.Database.ExecuteSqlCommand(strQuery); } } catch (Exception ex) { string errMessage = ex.Message; if (ex.InnerException != null) { errMessage += "\n" + ex.InnerException.Message; } MessageBox.Show(errMessage, "ServiceRoles::UpdateRoleModules", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(result); }
public int GetModuleIDforRoleModule(int roleModuleID) { int moduleID = 0; try { TBL_MP_Master_RoleModule dbRoleModule = _dbContext.TBL_MP_Master_RoleModule.Where(x => x.PK_RoleModuleID == roleModuleID).FirstOrDefault(); if (dbRoleModule != null) { moduleID = dbRoleModule.FK_ModuleId; } } catch (Exception ex) { MessageBox.Show(ex.Message, "GetModuleIDforRoleModule", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(moduleID); }