public static bool ChangePassword(HuntingEntities dataContext, AclUser aclUser, ChangePasswordModel model, bool isConfirm) { try { aclUser.EmailCode = null; aclUser.EmailCodeExpire = null; aclUser.PasswordHash = PasswordStorage.CreateHash(model.Password); var newSession = new UserSession() { AclUser = aclUser, SysCreated = DateTime.Now, Session = Guid.NewGuid().ToString(), }; dataContext.UserSessions.Add(newSession); if (isConfirm) { foreach (var territoryUser in aclUser.TerritoryUsers) { if (territoryUser.UserRoleEx == TerritoryUserRoleEnum.Invited) { territoryUser.UserRoleEx = TerritoryUserRoleEnum.Member; } } } dataContext.SaveChanges(); return(true); } catch (Exception exception) { logger.Error(exception, "ChangePassword"); return(false); } }
public static RegisterResultEnum RegisterUser(HuntingEntities dataContext, RegisterModel model, out UserSession session) { session = null; try { var lowerEmail = model.Email.Trim().ToLower(); var isAny = dataContext.AclUsers.Any(item => item.Email == lowerEmail); if (isAny) { return(RegisterResultEnum.AlreadyUsed); } var newUser = new AclUser() { Email = model.Email, PasswordHash = PasswordStorage.CreateHash(model.Password), SysCreated = DateTime.Now, }; dataContext.AclUsers.Add(newUser); var newSession = new UserSession() { AclUser = newUser, SysCreated = DateTime.Now, Session = Guid.NewGuid().ToString(), }; dataContext.UserSessions.Add(newSession); dataContext.SaveChanges(); session = newSession; return(RegisterResultEnum.Success); } catch (Exception exception) { logger.Error(exception, "RegisterUser"); return(RegisterResultEnum.Error); } }
public static AclUser CreateAdmin() { using (var dataContext = new HuntingEntities()) { var adminUser = dataContext.AclUsers.FirstOrDefault(item => item.Email == Settings.Default.AdminEmail); if (adminUser == null) { adminUser = new AclUser() { AccountTypeEx = AccountTypeEnum.Admin, Email = Settings.Default.AdminEmail, PasswordHash = PasswordStorage.CreateHash(Settings.Default.AdminPassword), SysCreated = DateTime.Now, MaxTerritoryCount = -1, Fullname = Settings.Default.AdminFullname, }; dataContext.AclUsers.Add(adminUser); } else { adminUser.AccountTypeEx = AccountTypeEnum.Admin; adminUser.PasswordHash = PasswordStorage.CreateHash(Settings.Default.AdminPassword); } dataContext.SaveChanges(); return(adminUser); } }
public static int?Update(HuntingEntities dataContext, MapItemType updateItem, MapItemTypeUpdateModel model, AclUser user) { try { if (model.IsCreate) { updateItem = new MapItemType() { SysCreated = DateTime.Now, SysCreator = user.Id, TerritoryId = model.TerritoryId, }; dataContext.MapItemTypes.Add(updateItem); } updateItem.Name = model.Name; updateItem.Description = model.Description; updateItem.SysEditor = user.Id; updateItem.SysUpdated = DateTime.Now; dataContext.SaveChanges(); return(updateItem.Id); } catch (Exception exception) { logger.Error(exception, "Update"); return(null); } }
// GET: Territory/UpdateMap public ActionResult UpdateMap(int id) { try { using (var dataContext = new HuntingEntities()) { var user = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (user == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } var territory = TerritoryContext.GetDetail(dataContext, id); if (user.CanUpdateTerritory(territory) == false) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } var model = new TerritoryDetailModel(territory, user); return(View(model)); } } catch (Exception exception) { logger.Error(exception, "TerritoryController"); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION); return(RedirectToAction("Index", "Home")); } }
public static List <UserLocation> GetUserLocationList(HuntingEntities dataContext, Territory territory) { try { var userLocationList = new List <UserLocation>(); var stewardLocation = territory.AclUserSteward.UserLocations.OrderByDescending(item => item.SysCreated).FirstOrDefault(); if (stewardLocation != null && stewardLocation.SysCreated > DateTime.Now.AddMinutes(-Settings.Default.UserLocationLastMinutes)) { userLocationList.Add(stewardLocation); } foreach (var territoryUser in territory.TerritoryUsers) { var userLocation = territoryUser.AclUser.UserLocations.OrderByDescending(item => item.SysCreated).FirstOrDefault(); if (userLocation != null && userLocation.SysCreated > DateTime.Now.AddMinutes(-Settings.Default.UserLocationLastMinutes)) { userLocationList.Add(userLocation); } } return(userLocationList); } catch (Exception exception) { logger.Error(exception, "GetUserLocationList"); return(null); } }
public void ProcessJob() { using (var dataContext = new HuntingEntities()) { AclUserContext.ClearExpiredEmailCodes(dataContext); } }
public ActionResult SaveMapAjax(int id, string mapData) { var model = new TerritoryMapSavedModel(); try { using (var dataContext = new HuntingEntities()) { var user = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (user == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(PartialView(model)); } var territory = TerritoryContext.GetDetail(dataContext, id); if (user.CanUpdateTerritory(territory) == false) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(PartialView(model)); } var saveModel = JsonConvert.DeserializeObject <MapSaveDataModel>(mapData); model = TerritoryContext.SaveMap(dataContext, id, saveModel, user.Id); return(PartialView(model)); } } catch (Exception exception) { logger.Error(exception, "TerritoryController"); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION); return(PartialView(model)); } }
public IHttpActionResult TerritoryDetail(int id) { try { using (var dataContext = new HuntingEntities()) { var session = GetSession(); if (session == null) { return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE)); } var userSession = AclUserContext.GetUserSession(dataContext, session); if (userSession == null) { return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE)); } var territory = TerritoryContext.GetDetail(dataContext, id); if (userSession.AclUser.CanViewTerritory(territory) == false) { return(Content(HttpStatusCode.Forbidden, FORBIDDEN_MESSAGE)); } var model = new TerritoryDetailModel(territory, userSession.AclUser); return(Ok(model)); } } catch (Exception exception) { logger.Error(exception, "MobileController"); return(InternalServerError()); } }
// GET: Admin public ActionResult Index(AclUserFilter filter, int?page) { try { using (var dataContext = new HuntingEntities()) { int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID]; var userName = User.Identity.Name; var user = AclUserContext.GetDetail(dataContext, userName); if (user.AccountTypeEx != AccountTypeEnum.Admin) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } filter.PrepareFilter(languageId); int pageIndex = page ?? 0; var itemList = AclUserContext.GetList(dataContext, filter, pageIndex); var itemListModel = new AclUserListModel(itemList, pageIndex); var model = new AclUserPageModel(itemListModel, filter); return(View(model)); } } catch (Exception exception) { logger.Error(exception, "AdminController"); return(RedirectToAction("Index", "Home")); } }
public IHttpActionResult Login(LoginModel model) { try { using (var dataContext = new HuntingEntities()) { UserSession userSession; var result = AclUserContext.LoginUser(dataContext, model.Email, model.Password, out userSession); switch (result) { case LoginResultEnum.Error: return(InternalServerError()); case LoginResultEnum.NotFound: return(NotFound()); case LoginResultEnum.WrongPassword: return(NotFound()); case LoginResultEnum.NotAllowed: return(NotFound()); case LoginResultEnum.Success: var responseModel = new LoginResponseModel(userSession); return(Ok(responseModel)); } return(NotFound()); } } catch (Exception exception) { logger.Error(exception, "MobileController - Login, Model = {0}", (model != null ? model.Email : "N/A")); return(InternalServerError()); } }
public ActionResult OptionTableAjax(QuestionUpdateModel model, string formAction, int?index) { try { using (var dataContext = new HuntingEntities()) { var user = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (user == null) { return(PartialView(@"AjaxError", GlobalRes.ERROR_NOT_ALLOWED)); } var questionnaire = QuestionnaireContext.GetDetail(dataContext, model.QuestionnaireId); if (questionnaire == null) { return(PartialView(@"AjaxError", GlobalRes.ERROR_NOT_FOUND)); } if (user.CanUpdateTerritory(questionnaire.MapItemType.Territory) == false) { return(PartialView(@"AjaxError", GlobalRes.ERROR_NOT_ALLOWED)); } QuestionContext.OptionTableAction(dataContext, model, formAction, index); ModelState.Clear(); return(PartialView("~/Views/Question/CommonLayout/OptionList.cshtml", model)); } } catch (Exception exception) { logger.Error(exception, "QuestionController"); return(PartialView(@"AjaxError", GlobalRes.ERROR_EXCEPTION)); } }
public ActionResult DemoLogin(string returnUrl) { try { using (var dataContext = new HuntingEntities()) { UserSession userSession; var demoUser = dataContext.AclUsers.FirstOrDefault(item => item.IsDeleted == false && item.AccountType == (int)AccountTypeEnum.Demo); var result = AclUserContext.LoginDemoUser(dataContext, demoUser, out userSession); if (result) { FormsAuthentication.SetAuthCookie(userSession.AclUser.Email, false); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, AccountRes.SUCCESS_LOGIN_DEMO); return(RedirectToLocal(returnUrl)); } ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, AccountRes.ERROR_LOGIN_DEMO); return(RedirectToAction("Login", "Account")); } } catch (Exception exception) { logger.Error(exception, "AccountController"); return(RedirectToAction("Index", "Home")); } }
public ActionResult ContactAction(int id, bool accept) { try { using (var dataContext = new HuntingEntities()) { var user = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (user == null) { return(Content("NOT_ALLOWED")); } var contact = TerritoryContext.GetTerritoryUserContact(dataContext, id); if (contact == null) { return(Content("NOT_FOUND")); } if (contact.Territory.StewardId != user.Id) { return(Content("NOT_ALLOWED")); } var result = TerritoryContext.ContactAction(dataContext, contact, accept, user); if (result == true) { return(Content("OK")); } return(Content("ERROR")); } } catch (Exception exception) { logger.Error(exception, "TerritoryController"); return(Content("ERROR")); } }
public IHttpActionResult UserLocation(UserLocationParamModel model) { try { using (var dataContext = new HuntingEntities()) { var session = GetSession(); if (session == null) { return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE)); } var userSession = AclUserContext.GetUserSession(dataContext, session); if (userSession == null) { return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE)); } var locationId = UserLocationContext.CreateUserLocation(dataContext, model.TrackId, model.LocationX, model.LocationY, userSession.AclUserId); if (locationId == null) { return(Content(HttpStatusCode.InternalServerError, FAILED_MESSAGE)); } return(Ok()); } } catch (Exception exception) { logger.Error(exception, "MobileController"); return(InternalServerError()); } }
// GET: Territory/PersonList/id public ActionResult PersonList(int id) { try { using (var dataContext = new HuntingEntities()) { var user = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (user == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } var territory = TerritoryContext.GetDetail(dataContext, id); if (user.CanUpdateTerritory(territory) == false) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Territory", new { id = id })); } int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID]; var model = new TerritoryPersonListModel(territory, languageId); return(View(model)); } } catch (Exception exception) { logger.Error(exception, "TerritoryController"); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION); return(RedirectToAction("Index", "Territory", new { id = id })); } }
public static int?Create(HuntingEntities dataContext, CreateMapItemModel model, int userId) { try { MapItem updateItem = new MapItem() { SysCreated = DateTime.Now, SysCreator = userId, TerritoryId = model.TerritoryId, MapItemType = model.MapItemTypeId, }; dataContext.MapItems.Add(updateItem); updateItem.Name = model.Name; updateItem.Description = model.Description; updateItem.LocationX = model.LocationX; updateItem.LocationY = model.LocationY; updateItem.SysEditor = userId; updateItem.SysUpdated = DateTime.Now; dataContext.SaveChanges(); return(updateItem.Id); } catch (Exception exception) { logger.Error(exception, "Create"); return(null); } }
public static bool ContactAction(HuntingEntities dataContext, TerritoryUserContact contact, bool accept, AclUser user) { try { if (accept) { var territoryUser = new TerritoryUser() { AclUserId = contact.AclUserId, UserRoleEx = TerritoryUserRoleEnum.Member, }; contact.Territory.TerritoryUsers.Add(territoryUser); } contact.IsDeleted = true; contact.SysEditor = user.Id; contact.SysUpdated = DateTime.Now; dataContext.SaveChanges(); return(true); } catch (Exception exception) { logger.Error(exception, "GetTerritoryUserContact"); return(false); } }
public ActionResult ForgottenPasswordConfirm(string code) { try { using (var dataContext = new HuntingEntities()) { int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID]; if (string.IsNullOrWhiteSpace(User.Identity.Name) == false) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } var aclUser = AclUserContext.GetDetailByEmailCode(dataContext, code); if (aclUser == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, AccountRes.ERROR_CODE_INVALID); return(RedirectToAction("Index", "Home")); } if (aclUser.AccountTypeEx == AccountTypeEnum.Admin) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, AccountRes.ERROR_FORGOTTEN_PASSWORD_ADMIN); return(RedirectToAction("Index", "Home")); } var model = new ChangePasswordModel(aclUser); return(View(model)); } } catch (Exception exception) { logger.Error(exception, "AccountController"); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION); return(RedirectToAction("Index", "Home")); } }
public ActionResult ChangePassword(ChangePasswordModel model) { try { using (var dataContext = new HuntingEntities()) { int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID]; var aclUser = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (aclUser == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { var isSuccess = AclUserContext.ChangePassword(dataContext, aclUser, model, false); if (isSuccess) { FormsAuthentication.SetAuthCookie(aclUser.Email, false); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Success, AccountRes.SUCCESS_PASSWORD_CHANGED); return(RedirectToAction("Manage", "Account")); } } model = new ChangePasswordModel(aclUser); return(View(model)); } } catch (Exception exception) { logger.Error(exception, "AccountController"); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION); return(RedirectToAction("Index", "Home")); } }
public ActionResult ChangePassword() { try { using (var dataContext = new HuntingEntities()) { int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID]; var aclUser = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (aclUser == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } var model = new ChangePasswordModel(aclUser); return(View(model)); } } catch (Exception exception) { logger.Error(exception, "AccountController"); ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION); return(RedirectToAction("Index", "Home")); } }
// GET: UserDetail/id public ActionResult UserDetail(int id) { try { using (var dataContext = new HuntingEntities()) { int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID]; var userName = User.Identity.Name; var user = AclUserContext.GetDetail(dataContext, userName); if (user.AccountTypeEx != AccountTypeEnum.Admin) { return(RedirectToAction("Index", "Home")); } var aclUser = AclUserContext.GetDetail(dataContext, id); var model = new AclUserDetailModel(aclUser); return(View(model)); } } catch (Exception exception) { logger.Error(exception, "AdminController"); return(RedirectToAction("Index", "Home")); } }
public static void OptionTableAction(HuntingEntities dataContext, QuestionUpdateModel model, string formAction, int?index) { if (formAction == "add") { model.OptionList.Add(new OptionUpdateModel()); } if (formAction == "move-up" && index.HasValue && index.Value > 0 && index < model.OptionList.Count) { var option = model.OptionList[index.Value]; model.OptionList[index.Value] = model.OptionList[index.Value - 1]; model.OptionList[index.Value - 1] = option; } if (formAction == "move-down" && index.HasValue && index.Value >= 0 && index < model.OptionList.Count - 1) { var option = model.OptionList[index.Value]; model.OptionList[index.Value] = model.OptionList[index.Value + 1]; model.OptionList[index.Value + 1] = option; } if (formAction == "delete" && index.HasValue && index.Value >= 0 && index < model.OptionList.Count) { model.OptionList.RemoveAt(index.Value); } if (model.OptionList.Count > 0) { model.OptionList.First().IsFirst = true; model.OptionList.Last().IsLast = true; } }
public ActionResult InviteUser(int id, string email, string name) { try { using (var dataContext = new HuntingEntities()) { var user = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (user == null) { return(Content("ERROR")); } var territory = TerritoryContext.GetDetail(dataContext, id); if (user.CanUpdateTerritory(territory) == false) { return(Content("ERROR")); } var result = TerritoryContext.InviteUser(dataContext, territory, email, name, user); if (result == true) { return(Content("OK")); } return(Content("CREATE")); } } catch (Exception exception) { logger.Error(exception, "TerritoryController"); return(Content("ERROR")); } }
public IHttpActionResult TerritoryList() { try { using (var dataContext = new HuntingEntities()) { var session = GetSession(); if (session == null) { return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE)); } var userSession = AclUserContext.GetUserSession(dataContext, session); if (userSession == null) { return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE)); } var territoryList = TerritoryContext.GetListForUser(dataContext, userSession.AclUser); var model = new TerritoryListModel(territoryList, userSession.AclUser); return(Ok(model)); } } catch (Exception exception) { logger.Error(exception, "MobileController"); return(InternalServerError()); } }
public void ProcessJob() { using (var dataContext = new HuntingEntities()) { var systemAccount = AclUserContext.GetAdminAccount(dataContext); TerritoryContext.RefreshDemoAccount(dataContext, systemAccount); } }
public static List <Language> LoadLanguageList() { using (var context = new HuntingEntities()) { var languageList = context.Languages.Where(item => item.IsDeleted == false).OrderBy(item => item.Order).ToList(); return(languageList); } }
public static AclUser GetAdminAccount(HuntingEntities dataContext) { var adminUser = dataContext.AclUsers.FirstOrDefault(item => item.Email == Settings.Default.AdminEmail); if (adminUser == null) { return(CreateAdmin()); } return(adminUser); }
public static void ProcessFailure(HuntingEntities dataContext, EmailInfo emailInfo, string errorMessage) { emailInfo.SendDate = DateTime.Now; emailInfo.ErrorMessage = errorMessage; emailInfo.RetryCount++; if (emailInfo.RetryCount == MAX_RETRY_COUNT) { emailInfo.SendStateEx = SendStateEnum.Failed; } dataContext.SaveChanges(); }
//POST: Territory/Contact public ActionResult Contact(int id, string message) { try { using (var dataContext = new HuntingEntities()) { var user = AclUserContext.GetDetail(dataContext, User.Identity.Name); if (user == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("Index", "Home")); } var territory = TerritoryContext.GetDetail(dataContext, id); if (territory == null) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_FOUND); return(RedirectToAction("List", "Territory")); } if (territory.StewardId == user.Id) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("List", "Territory")); } if (territory.TerritoryUsers.Any(item => item.AclUserId == user.Id)) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("List", "Territory")); } if (territory.TerritoryUserContacts.Any(item => item.IsDeleted == false && item.AclUserId == user.Id)) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED); return(RedirectToAction("List", "Territory")); } var result = TerritoryContext.Contact(dataContext, territory, user, message); if (result) { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Success, TerritoryRes.SUCCESS_CONTACT); } else { ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, TerritoryRes.ERROR_CONTACT); } return(RedirectToAction("List", "Territory")); } } catch (Exception exception) { logger.Error(exception, "TerritoryController"); return(RedirectToAction("List", "Territory")); } }