/// <summary> /// Method to delete user /// </summary> /// <param name="user">selected user to delete</param> /// <returns>result of delete operation</returns> private string DeactivateUser(UserMapping user, bool?activateValue, bool?isUSerEdited) { try { if (user != null) { bool?IsUserRegister = user.IsRegistered; //Getting the particular row and mapping it to the object of the table. UserRoleBPUMapping userRoleBPUMapping = DbEntity.UserRoleBPUMappings.Single(o => o.ID == user.Id); //Making the role unvailable for the user by making the isactive state false userRoleBPUMapping.IsActive = activateValue; userRoleBPUMapping.ModifiedById = user.User.ModifiedById; userRoleBPUMapping.ModifiedDate = DateTime.Now; userRoleBPUMapping.CreatedById = user.User.CreatedById; bool result = Convert.ToBoolean(DbEntity.SaveChanges()); //check if any more roles are availabel of given user int roleCount = DbEntity.UserRoleBPUMappings.Where(o => o.UserId == user.User.UserId && o.IsActive == true).Count(); //if all roles are not active then make the user inctive/active from user table if (roleCount == 0) { ////Getting the particular row and mapping it to the object of the table. User userDB = DbEntity.Users.Single(o => o.UserId == user.User.UserId); //Making the user unvailable by making the isactive state false userDB.IsActive = activateValue; user.IsActive = activateValue; userDB.ModifiedById = user.User.ModifiedById; userDB.ModifiedDate = DateTime.Now; } if (activateValue == true) { ////Getting the particular row and mapping it to the object of the table. User userDB = DbEntity.Users.Single(o => o.UserId == user.User.UserId); //Making the user unvailable by making the isactive state false userDB.IsActive = activateValue; userDB.ModifiedById = user.User.ModifiedById; userDB.ModifiedDate = DateTime.Now; user.IsActive = activateValue; //making isregister value from userrolebpumapping table to 1 after activation if (IsUserRegister == false) { userRoleBPUMapping.IsRegistered = true; } } #region Logger string deletedUserName = DbEntity.Users.Where(o => o.UserId == user.User.UserId).Select(o => o.UserName).FirstOrDefault().ToString(); description = string.Format(Constants.DeleteUser, deletedUserName, user.User.CreatedById); Loggers loggers = null; if (activateValue == false) { loggers = new Loggers { ActionMethod = Constants.ActionMethodForDeleteUser, Description = description, Operation = Constants.Delete, UserID = user.User.CreatedById }; } else { loggers = new Loggers { ActionMethod = Constants.ActionMethodForActivateUser, Description = description, Operation = Constants.Add, UserID = user.User.CreatedById }; } logger = LoggerObj.LoggerMethod(loggers); DbEntity.Loggers.Add(logger); #endregion result = Convert.ToBoolean(DbEntity.SaveChanges()); string resultinString = null; if (activateValue == false) { if (result.Equals(true) && isUSerEdited != true) { resultinString = Constants.UserDeactivatedSuccessfully; user.User.CreatedByUser = GetUser((int)user.User.CreatedById); user.User.IsActive = false; SendEmailToAllEMIEUsers(MailMessageType.UserDeactivated, user.User); } else { resultinString = Constants.UserDeactivationFailed; } } else { if (IsUserRegister == false && result.Equals(true) && isUSerEdited != true) { resultinString = Constants.UserRegisteredSuccessfully; user.User.CreatedByUser = GetUser((int)user.User.CreatedById); user.User.IsActive = user.IsActive; SendEmailToAllEMIEUsers(MailMessageType.UserRegistered, user.User); } else if (result.Equals(true) && isUSerEdited != true) { resultinString = Constants.UserActivatedSuccessfully; user.User.CreatedByUser = GetUser((int)user.User.CreatedById); user.User.IsActive = user.IsActive; SendEmailToAllEMIEUsers(MailMessageType.UserActivated, user.User); } else { resultinString = Constants.UserActivationFailed; } } return(resultinString); } else { return(Constants.OperationFailed); } } catch (Exception) { throw; } }
/// <summary> /// Method to edit selected user /// </summary> /// <param name="user">user with new editable values</param> /// <param name="oldUser">selected user to edit</param> /// <returns>result of edit operation</returns> private string EditUser(UserMapping user, UserMapping oldUser) { try { user.User.CreatedByUser = GetUser((int)user.User.CreatedById); string resultInSTring = null; bool result = false; int count = DbEntity.UserRoleBPUMappings.Where(userfrmtbl => userfrmtbl.RoleId == user.User.UserRole.RoleId && userfrmtbl.BPUId == user.User.UserBPU.BPUId && userfrmtbl.UserId == user.User.UserId).Count(); //check if user with edited values are same with old value then don't edit it if (count > 0) { if (user.IsActive != oldUser.IsActive) { UserRoleBPUMapping userwithRoleBPU = DbEntity.UserRoleBPUMappings.Where(o => o.ID == oldUser.Id).FirstOrDefault(); user.Id = userwithRoleBPU.ID; var operationsInBool = user.IsActive; if (operationsInBool.Equals(false)) { oldUser.User.ModifiedById = user.User.ModifiedById; DeactivateUser(oldUser, false, true); user.IsActive = false; resultInSTring = Constants.UserDeactivatedSuccessfully; } else { oldUser.User.ModifiedById = user.User.ModifiedById; DeactivateUser(oldUser, true, true); user.IsActive = true; resultInSTring = Constants.UserActivatedSuccessfully; } result = Convert.ToBoolean(DbEntity.SaveChanges()); #region Send mail //Send mail if (user.IsActive == false) { user.User.IsActive = false; SendEmailToAllEMIEUsers(MailMessageType.UserDeactivated, user.User); } else { user.User.IsActive = true; SendEmailToAllEMIEUsers(MailMessageType.UserActivated, user.User); } #endregion Send mail } else { if (user.MappingDetails != oldUser.MappingDetails) { UserRoleBPUMapping userwithRoleBPU = DbEntity.UserRoleBPUMappings.Where(o => o.ID == oldUser.Id).FirstOrDefault(); userwithRoleBPU.MappingDetails = user.MappingDetails; result = Convert.ToBoolean(DbEntity.SaveChanges()); resultInSTring = Constants.UserEditedSuccessfully; user.User.IsActive = user.IsActive; SendEmailToAllEMIEUsers(MailMessageType.UserEdited, user.User); } else { resultInSTring = String.Format(Constants.UnableToEditDuplicateUser, user.User.UserRole.RoleName, user.User.UserBPU.BPU1); } } } else { UserRoleBPUMapping userwithRoleBPU = DbEntity.UserRoleBPUMappings.Where(o => o.ID == oldUser.Id).FirstOrDefault(); if (!userwithRoleBPU.IsActive.Equals(user.IsActive)) { user.Id = userwithRoleBPU.ID; DeactivateUser(user, user.IsActive, true); } userwithRoleBPU.BPUId = user.User.UserBPU.BPUId; userwithRoleBPU.RoleId = user.User.UserRole.RoleId; userwithRoleBPU.MappingDetails = user.MappingDetails; userwithRoleBPU.ModifiedById = user.User.CreatedById; userwithRoleBPU.ModifiedDate = DateTime.Now; result = Convert.ToBoolean(DbEntity.SaveChanges()); resultInSTring = Constants.UserEditedSuccessfully; if (!result) { resultInSTring = Constants.UnableToEditUser; } user.User.IsActive = user.IsActive; SendEmailToAllEMIEUsers(MailMessageType.UserEdited, user.User); } return(resultInSTring); } catch (Exception) { throw; } }