public BreadcrumbModel CreateBreadcrumb(Controller currentController, ActionExecutingContext filterContext) { var result = new BreadcrumbModel(); var controllerType = currentController.GetType(); var actionName = filterContext.RouteData.Values["action"].ToString(); var routeKey = GetRouteKey(controllerType, actionName); //Add the current route var currrentAction = GetItemByKey(routeKey); if (currrentAction == null) return result; result.Items.Add(BreadcrumbActionToMenuItem(currrentAction, filterContext.RouteData, true)); while (currrentAction?.ParentKey.IsNotBlank() ?? false) { currrentAction = GetItemByKey(currrentAction.ParentKey); //TODO improve, there is no need to go to the next loop if (currrentAction == null) continue; var ancestorMenuItem = BreadcrumbActionToMenuItem(currrentAction, filterContext.RouteData, false); result.Items.Insert(0, ancestorMenuItem); } return result; }
public ServerRenderedAppViewResult(Controller controller, string pageTitle, ReduxServerRenderState model = null) { controller.ViewData["Title"] = pageTitle; model = model ?? new ReduxServerRenderState(controller); controller.ViewData.Model = model; ViewName = "~/Web/Shared/ServerRenderedApp.cshtml"; ViewData = controller.ViewData; TempData = controller.TempData; }
public void WebControllerMessage(Controller controller, string message, params object[] args) { if (this.IsEnabled()) { string finalMessage = string.Format(message, args); this.ServiceMessage( controller.ActionContext.HttpContext.Connection.LocalPort, FabricRuntime.GetNodeContext().NodeName, finalMessage); } }
private static void MakeUserOrganizationAdminUser(Controller controller, string organizationId) { var orgAdminClaims = new List<Claim> { new Claim(AllReady.Security.ClaimTypes.UserType, Enum.GetName(typeof(UserType), UserType.OrgAdmin)), new Claim(AllReady.Security.ClaimTypes.Organization, organizationId) }; var httpContext = new Mock<HttpContext>(); var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(orgAdminClaims)); httpContext.Setup(x => x.User).Returns(claimsPrincipal); controller.ActionContext.HttpContext = httpContext.Object; }
private static void SetFakeHttpContextIfNotAlreadySet(Controller controller) { if (controller.ActionContext.HttpContext == null) controller.ActionContext.HttpContext = FakeHttpContext(); }
public ReduxServerRenderState(Controller controller, ReduxAppState app = null) { Location = $"{controller.Request.Path}{controller.Request.QueryString}"; App = app ?? new ReduxAppState(controller.User); Routing = new ReduxRoutingState(controller.Request); }
public static T Service <T>(this Microsoft.AspNet.Mvc.Controller controller) { return(controller.HttpContext.ApplicationServices.GetService <T>()); }
public bool ValidateAndMapCreateCategory(CategoryViewModel vm, Controller controller, string userId, out Category category, out ErrorViewModel errors) { var modelState = controller.ModelState; category = null; errors = null; if (!modelState.IsValid) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryInvalidError, Errors = modelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList() }; this.logger.LogWarning("Invalid category model:" + string.Join(";", modelState.Values.SelectMany(v => v.Errors) .Select(e => e.ErrorMessage))); return false; } if (this.categoryRepo.FindAllWhere(c => string.Equals(c.NormalizedName, Category.NormalizeName(vm.Name))).Any()) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryNameAlreadyExistsError, vm.Name) }; return false; } CategoryType catType; if ((catType = this.categoryTypeRepo.FindById(vm.CategoryTypeId)) == null) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryNoCategoryTypeError }; return false; } Category parentCategory = null; if(vm.ParentCategoryId.HasValue){ parentCategory = this.categoryRepo.FindById(vm.ParentCategoryId.Value, c => c.CategoryType); if (parentCategory == null || !string.Equals(parentCategory.AccountingUserId, userId)) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryParentCategoryNotFoundError, vm.ParentCategoryId) }; return false; } if (parentCategory.CategoryType.Id != catType.Id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryParentTypeMismatchError }; return false; } if (parentCategory.ParentCategoryId.HasValue && this.categoryRepo.FindById(parentCategory.ParentCategoryId.Value).ParentCategoryId.HasValue){ errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryDepthTooGreatError }; return false; } } category = Mapper.Map<Category>(vm); category.Id = 0; category.Total = 0; category.NormalizedName = Category.NormalizeName(category.Name); category.ParentCategoryId = null; category.ParentCategory = parentCategory; category.CategoryType = catType; category.ChildCategories = null; category.ExitingTransactions = null; category.AccountingUserId = userId; return true; }
public bool ValidateAndMapUpdateCategory(long id, CategoryViewModel vm, Category previous, Controller controller, string userId, out Category category, out ErrorViewModel errors) { var modelState = controller.ModelState; category = null; errors = null; if (vm.Id != id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryInvalidRouteError }; return false; } if (!modelState.IsValid) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryInvalidError, Errors = modelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList() }; this.logger.LogWarning("Invalid category model:" + string.Join(";", modelState.Values.SelectMany(v => v.Errors) .Select(e => e.ErrorMessage))); return false; } // Does a different category exist with the same name? if (this.categoryRepo.FindAllWhere(c => string.Equals(c.NormalizedName, Category.NormalizeName(vm.Name)) && c.Id != id).Any()) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryNameAlreadyExistsError, vm.Name) }; return false; } if (vm.CategoryTypeId != previous.CategoryType.Id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryCannotChangeTypeError }; return false; } if (vm.ParentCategoryId.HasValue && vm.ParentCategoryId.Value == id) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryCannotBeOwnParentError }; return false; } Category parentCategory = null; if (vm.ParentCategoryId.HasValue || previous.ParentCategoryId.HasValue) { var hasChildren = this.categoryRepo.FindAllWhere(c => c.ParentCategoryId == id).Any(); if (hasChildren && vm.ParentCategoryId != previous.ParentCategoryId) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryMoveWithChildrenError }; return false; } if (vm.ParentCategoryId.HasValue) { parentCategory = this.categoryRepo.FindById(vm.ParentCategoryId.Value, c => c.CategoryType); if (parentCategory == null || !string.Equals(parentCategory.AccountingUserId, userId)) { errors = new ErrorViewModel { Error = string.Format(CategoryErrors.ValidationErrors.CategoryParentCategoryNotFoundError, vm.ParentCategoryId) }; return false; } if (parentCategory.CategoryType.Id != vm.CategoryTypeId) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryParentTypeMismatchError }; return false; } if (parentCategory.ParentCategoryId.HasValue && this.categoryRepo.FindById(parentCategory.ParentCategoryId.Value).ParentCategoryId.HasValue) { errors = new ErrorViewModel { Error = CategoryErrors.ValidationErrors.CategoryDepthTooGreatError }; return false; } } } category = previous; category.Name = vm.Name; category.NormalizedName = Category.NormalizeName(category.Name); category.ParentCategoryId = parentCategory?.Id; category.ParentCategory = parentCategory; return true; }
public bool ValidateAndMapCreateTransaction(TransactionViewModel vm, Controller controller, string userId, out Transaction transaction, out ErrorViewModel errors) { var modelState = controller.ModelState; transaction = null; errors = null; if (vm.Total == null || vm.Total <= 0) { errors = new ErrorViewModel { Error = TransactionErrors.ValidationErrors.TransactionInvalidTotalError }; return false; } var transactionType = this.transTypeRepo.FindById(vm.TransactionTypeId, tt => tt.From, tt => tt.To); if (transactionType == null) { errors = new ErrorViewModel { Error = string.Format(TransactionErrors.ValidationErrors.TransactionTypeNotFoundError, vm.TransactionTypeId) }; return false; } if (vm.FromId == vm.ToId) { errors = new ErrorViewModel { Error = TransactionErrors.ValidationErrors.TransactionToSameCategoryError }; return false; } var fromCategory = this.categoryRepo.FindById(vm.FromId, c => c.CategoryType); if (fromCategory == null || !string.Equals(fromCategory.AccountingUserId, userId)) { errors = new ErrorViewModel { Error = string.Format(TransactionErrors.ValidationErrors.TransactionFromCategoryNotFoundError, vm.FromId) }; return false; } var toCategory = this.categoryRepo.FindById(vm.ToId, c => c.CategoryType); if (toCategory == null || !string.Equals(toCategory.AccountingUserId, userId)) { errors = new ErrorViewModel { Error = string.Format(TransactionErrors.ValidationErrors.TransactionToCategoryNotFoundError, vm.ToId) }; return false; } if (toCategory.CategoryType.Id != transactionType.To.Id || fromCategory.CategoryType.Id != transactionType.From.Id) { errors = new ErrorViewModel { Error = TransactionErrors.ValidationErrors.TransactionTypeCategoryTypeMismatchError }; return false; } if (!modelState.IsValid) { modelState.Clear(); controller.TryValidateModel(vm); } if (!modelState.IsValid) { errors = new ErrorViewModel { Error = TransactionErrors.ValidationErrors.TransactionInvalidError, Errors = modelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList() }; this.logger.LogWarning("Invalid transaction model:" + string.Join(";", modelState.Values.SelectMany(v => v.Errors) .Select(e => e.ErrorMessage))); return false; } transaction = Mapper.Map<Transaction>(vm); transaction.Id = 0; transaction.Total = Math.Round(transaction.Total, 2); transaction.From = fromCategory; transaction.To = toCategory; transaction.TransactionType = transactionType; transaction.AccountingUserId = userId; return true; }
public string GetPageTitle(Controller controller, ActionExecutingContext filterContext) { //Default Page title var pageTitle = filterContext.ActionDescriptor.Name.ToDisplayText(); var actionValue = filterContext.RouteData.Values["action"]; //Try to get the action name, default to Index var actionName = actionValue != null && actionValue.ToString().IsNotBlank() ? actionValue.ToString() : "Index"; var controllerType = controller.GetType(); var actionMember = controllerType.GetMethods().FirstOrDefault(m => m.Name == actionName); //Find any custom `TitleAttribute` values var titleAttributes = actionMember?.GetCustomAttributes(typeof(TitleAttribute), true); if (titleAttributes?.Any() ?? false) { var titleAttribute = (TitleAttribute)titleAttributes.First(); if (titleAttribute != null) pageTitle = titleAttribute.PageTitle; } else if (actionName == "Index") { //If there is no attribute and the default Index action //is in use, use the Controller name as the page title pageTitle = controller.GetType().Name.Replace("Controller", "").ToDisplayText(); } //Inject any route data into the page title foreach (var routeData in filterContext.RouteData.Values) { pageTitle = pageTitle.Replace("{" + routeData.Key + "}", routeData.Value.ToString()); } return pageTitle; }