Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="areaName">The name of the area containing the Emails view folder if applicable</param>
        /// <returns></returns>
#if ASPNET5
        ActionContext CreateControllerContext(string areaName, IHttpRequestFeature requsetFeature)
        {
            var routeData = new Microsoft.AspNetCore.Routing.RouteData();

            routeData.Values["controller"] = EmailViewDirectoryName;

            // if populated will add searching the named Area for the view
            if (!string.IsNullOrWhiteSpace(areaName))
            {
                routeData.DataTokens["Area"] = areaName;
            }

            var actionDescriptor = new ActionDescriptor();

            actionDescriptor.RouteValues = routeData.Values.ToDictionary(kv => kv.Key, kv => kv.Value.ToString());
            FeatureCollection featureCollection = new FeatureCollection();
            var requsetFeature_local            = new HttpRequestFeature();

            requsetFeature_local.Method   = "GET";
            requsetFeature_local.Protocol = requsetFeature?.Protocol;
            requsetFeature_local.PathBase = requsetFeature?.PathBase;
            requsetFeature_local.Scheme   = requsetFeature?.Scheme;
            featureCollection.Set <IHttpRequestFeature>(requsetFeature_local);
            featureCollection.Set <IHttpResponseFeature>(new HttpResponseFeature());
            var httpContext = new DefaultHttpContext(featureCollection);

            httpContext.RequestServices = serviceProvider;
            var actionContext = new ActionContext(httpContext, routeData, actionDescriptor);

            actionContext.RouteData = routeData;

            return(actionContext);
        }
Exemple #2
0
        /// <summary>
        /// Renders an email view.
        /// </summary>
        /// <param name="email">The email to render.</param>
        /// <param name="viewName">Optional email view name override. If null then the email's ViewName property is used instead.</param>
        /// <returns>The rendered email view output.</returns>
        public virtual async Task <string> RenderAsync(Email email, string viewName = null)
        {
            viewName = viewName ?? email.ViewName;
            //var controllerContext = CreateControllerContext(email.AreaName, url);
            //var view = CreateView(viewName, controllerContext);

            var routeData = new Microsoft.AspNetCore.Routing.RouteData();

            if (email.Route != null)
            {
                routeData.Routers.Add(email.Route);
            }
            routeData.Values["controller"] = EmailViewDirectoryName;
            if (!string.IsNullOrWhiteSpace(email.AreaName))
            {
                routeData.Values["area"]     = email.AreaName;
                routeData.DataTokens["area"] = email.AreaName;
            }

            Dictionary <string, object> viewData = new Dictionary <string, object>();

            viewData[ImageEmbedder.ViewDataKey] = email.ImageEmbedder;
            viewData.Remove(ImageEmbedder.ViewDataKey);
            var viewOutput = await _templateService.RenderTemplateAsync(routeData, viewName, email, viewData, true);

            return(viewOutput);
        }
        public static void GetViews(Microsoft.AspNetCore.Routing.RouteData route,
                                    HttpContext context, string controller,
                                    out bool isInitView, ref string viewName,
                                    ref string title)
        {
            isInitView = false;
            bool bIsAjaxSubaction = IsAjaxSubaction(context);

            title = GetControllerTitle(controller);
            if (bIsAjaxSubaction)
            {
                //if needed, can do a switch based on subaction type
                //for now, this is simple enough
                if (viewName.StartsWith(SEARCH_VIEW))
                {
                    viewName = SEARCH_AJAXRESULT_VIEW;
                }
                else
                {
                    viewName = CONTENT_AJAXRESULT_VIEW;
                }
            }
            else
            {
                //init view
                isInitView = true;
            }
        }
Exemple #4
0
        public static string GetAction(Microsoft.AspNetCore.Routing.RouteData route)
        {
            if (route == null)
            {
                return("");
            }

            return(route.Values["action"].ToString());
        }
Exemple #5
0
        public static string GetController(Microsoft.AspNetCore.Routing.RouteData route)
        {
            if (route == null)
            {
                return("");
            }

            return(route.Values["controller"].ToString());
        }
 public static void GetRouteValues(Microsoft.AspNetCore.Routing.RouteData route, out string controller,
                                   out string action, out string partialUriPattern)
 {
     controller = (route.Values[CONTROLLER] != null) ?
                  route.Values[CONTROLLER].ToString() : DataHelpers.GetDefaultNetworkGroupName();
     action = (route.Values[ACTION] != null) ?
              route.Values[ACTION].ToString() : DataHelpers.SERVER_ACTION_TYPES.search.ToString();
     partialUriPattern = (route.Values[CONTENTURIPATTERN] != null) ?
                         route.Values[CONTENTURIPATTERN].ToString() : DataHelpers.MakeURIPatternStart();
 }
Exemple #7
0
        ActionContext CreateActionContext()
        {
            var routeData        = new Microsoft.AspNetCore.Routing.RouteData();
            var actionDescriptor = new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor();

            var actionContext = new ActionContext(HttpContextAccessor.HttpContext, routeData, actionDescriptor);

            actionContext.RouteData.Values.Add("controller", EmailViewDirectoryName);
            actionContext.ActionDescriptor.RouteValues.Add("controller", EmailViewDirectoryName);
            actionContext.ActionDescriptor.AttributeRouteInfo = new Microsoft.AspNetCore.Mvc.Routing.AttributeRouteInfo();

            return(actionContext);
        }
Exemple #8
0
        private void Log(string methodName, Microsoft.AspNetCore.Routing.RouteData routeData)
        {
            var    controllerName = routeData.Values["controller"];
            var    actionName     = routeData.Values["action"];
            string message        = methodName + " Controller:" + controllerName + " Action:" + actionName + " Date: "
                                    + DateTime.Now.ToString() + Environment.NewLine;

            //saving the data in a text file called Log.txt
            File.AppendAllLines(Path.GetPathRoot(Assembly.GetExecutingAssembly().Location) + "/Log/Log.txt",
                                new List <string> {
                message
            });
        }
Exemple #9
0
        private void ActionLogWriter(string methodName, Microsoft.AspNetCore.Routing.RouteData routeData)
        {
            var controller  = routeData.Values["controller"];
            var action      = routeData.Values["action"];
            var description = $"{methodName}- controller:{controller} action:{action}";

            ActionFilterEntry newEntry = new ActionFilterEntry();

            newEntry.Description = description + " -> " + DateTime.Now.ToString();

            _context.ActionFilterEntry.Add(newEntry);
            _context.SaveChanges();
        }
        public static ActionContext PrepareActionContext(string actionName, HttpContext httpContext)
        {
            if (string.IsNullOrWhiteSpace(actionName))
            {
                throw new ArgumentException("message", nameof(actionName));
            }


            Microsoft.AspNetCore.Routing.RouteData routeData = new Microsoft.AspNetCore.Routing.RouteData();
            return(new ActionContext(httpContext, routeData, new ActionDescriptor()
            {
                DisplayName = actionName
            }));
        }
        private ActionExecutingContext GenerateActionExecutingContext(Container autoMockContainer)
        {
            var httpContext      = new DefaultHttpContext();
            var routData         = new RouteData();
            var actionDescriptor = new ActionDescriptor();
            var actionContext    = new ActionContext(httpContext, routData, actionDescriptor);

            var actionArguments = new Dictionary <string, object>()
            {
                { nameof(Product), new Product() }
            };
            var controller = autoMockContainer.GetInstance <Mock <ControllerBase> >();
            var context    = new ActionExecutingContext(actionContext, new List <IFilterMetadata>(), actionArguments, controller.Object);

            return(context);
        }
        public static string GetNetworkGroupFromRoute(
            Microsoft.AspNetCore.Routing.RouteData route, HttpContext context)
        {
            string sNetworkGroupName = string.Empty;

            if (route != null)
            {
                sNetworkGroupName = (!string.IsNullOrEmpty(route.Values[CONTROLLER].ToString())) ?
                                    route.Values[CONTROLLER].ToString().ToLower()
                    : DataHelpers.GetDefaultNetworkGroupName();
            }
            else
            {
                sNetworkGroupName = DataHelpers.GetDefaultNetworkGroupName();
            }
            return(sNetworkGroupName);
        }
 private DataHelpers.SERVER_ACTION_TYPES GetServerActionTypeFromRoute(Microsoft.AspNetCore.Routing.RouteData route)
 {
     DataHelpers.SERVER_ACTION_TYPES eActionType
         = DataHelpers.SERVER_ACTION_TYPES.preview;
     if (route != null)
     {
         string sActionName = route.Values[ACTION].ToString().ToLower();
         eActionType = (string.IsNullOrEmpty(sActionName))
             ? DataHelpers.SERVER_ACTION_TYPES.preview : (DataHelpers.SERVER_ACTION_TYPES)Enum.Parse(typeof(DataHelpers.SERVER_ACTION_TYPES), sActionName);
     }
     else
     {
         //tests end up here
         eActionType = DataHelpers.SERVER_ACTION_TYPES.search;
     }
     return(eActionType);
 }
 public RouteDataSnapshot(Microsoft.AspNetCore.Routing.RouteData routeData, Microsoft.AspNetCore.Routing.RouteValueDictionary dataTokens, System.Collections.Generic.IList <Microsoft.AspNetCore.Routing.IRouter> routers, Microsoft.AspNetCore.Routing.RouteValueDictionary values)
 {
     throw null;
 }
 public RouteData(Microsoft.AspNetCore.Routing.RouteData other)
 {
 }
Exemple #16
0
        private async Task<AuthorisationResult> EnforceRBACRequirements(AuthContext authContext, Microsoft.AspNetCore.Routing.RouteData routeData)
        {
            var userType = authContext.UserType;

            // Grab the route name via the [ActionName(...)] annotation on a controller method.
            // If you don't annotate your methods, or have not configured this route as authenticated, we won't apply any auth restrictions.
            var routeName = (string)routeData.Values["action"];

            if (routeName == null || !settings.Meta.AuthRequirements.ContainsKey(routeName))
            {
                return AuthorisationResult.InjectAndContinue;
            }

            var requirements = settings.Meta.AuthRequirements[routeName];

            Maybe<Employee> employee = null;
            Maybe<EmployeeRole> employeeRole = null;
            Maybe<User> user = null;

            if (userType != settings.Meta.AuthRequirements[routeName].UserType && UserType.Any != settings.Meta.AuthRequirements[routeName].UserType)
            {
                return AuthorisationResult.AbortUnauthorised;
            }

            if (authContext.AuthToken.HasNoValue)
            {
                return AuthorisationResult.AbortUnauthorised;
            }

            var authToken = authContext.AuthToken.Value;

            // Depending on the session type, attempt to fetch the equivalent model (User or Employee).
            if (userType == UserType.Employee)
            {
                employee = (await employeeSessions.FetchEmployeeSessionAndEmployeeFromToken(authToken).ConfigureAwait(false))
                  .OnBoth(x => x.IsSuccess
                    ? x.Value
                    : Maybe<Employee>.None
                  );

                if (employee.HasNoValue)
                {
                    return AuthorisationResult.AbortUnauthorised;
                }

                // For employees, their permissions are scoped to an employee role. First we fetch the role.
                employeeRole = (await employeeRoles.FetchEmployeeRole(employee.Value.RoleId).ConfigureAwait(false))
                  .OnBoth(x => x.IsSuccess
                    ? x.Value
                    : Maybe<EmployeeRole>.None
                  );

                // If the employee role wasn't found but the route didn't require any permissions, we let them through.
                if (employeeRole.HasNoValue && RouteRequiresAtLeastOnePermission(requirements))
                {
                    return AuthorisationResult.AbortUnauthorised;
                }

                // Verify each of the role permissions in turn, if one is missing and the route requires it, deny access.
                if (!RoleHasCorrectPermissions(employeeRole.Value, requirements))
                {
                    return AuthorisationResult.AbortUnauthorised;
                }
            }
            else if (userType == UserType.User)
            {
                user = (await userSessions.FetchUserSessionAndUserFromToken(authToken, (int)authContext.AuthType).ConfigureAwait(false))
                  .OnBoth(x => x.IsSuccess
                    ? x.Value
                    : Maybe<User>.None
                  );

                if (user.HasNoValue)
                {
                    return AuthorisationResult.AbortUnauthorised;
                }
            }

            // After verifying the user's permissions, update the context with relevant user data and continue.
            authContext.Employee = employee;
            authContext.EmployeeRole = employeeRole;
            authContext.User = user;

            return AuthorisationResult.Continue;
        }