Service/Data access class for Rock.Model.RestControllerService entity objects.
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var service = new RestControllerService( new RockContext() );
            var sortProperty = gControllers.SortProperty;

            var qry = service.Queryable().Select( c => new
            {
                c.Id,
                c.Name,
                c.ClassName,
                Actions = c.Actions.Count()
            } );

            if (sortProperty != null)
            {
                qry = qry.Sort(sortProperty);
            }
            else
            {
                qry = qry.OrderBy( c => c.Name);
            }

            gControllers.DataSource = qry.ToList();
            gControllers.DataBind();
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnLoad( EventArgs e )
        {
            if ( !Page.IsPostBack )
            {
                var service = new RestControllerService( new RockContext() );
                if ( !service.Queryable().Any() )
                {
                    RefreshControllerList();
                }

                BindGrid();
            }

            base.OnLoad( e );
        }
Example #3
0
        /// <summary>
        /// Registers the controllers.
        /// </summary>
        public static void RegisterControllers()
        {
            var rockContext           = new RockContext();
            var restControllerService = new RestControllerService(rockContext);
            var discoveredControllers = new List <RestController>();

            var config   = GlobalConfiguration.Configuration;
            var explorer = config.Services.GetApiExplorer();

            foreach (var apiDescription in explorer.ApiDescriptions)
            {
                var action = apiDescription.ActionDescriptor;
                var name   = action.ControllerDescriptor.ControllerName;

                var controller = discoveredControllers.Where(c => c.Name == name).FirstOrDefault();
                if (controller == null)
                {
                    controller = new RestController
                    {
                        Name      = name,
                        ClassName = action.ControllerDescriptor.ControllerType.FullName
                    };

                    discoveredControllers.Add(controller);
                }

                controller.Actions.Add(new RestAction
                {
                    ApiId  = apiDescription.ID,
                    Method = apiDescription.HttpMethod.Method,
                    Path   = apiDescription.RelativePath
                });
            }

            var actionService = new RestActionService(rockContext);

            foreach (var discoveredController in discoveredControllers)
            {
                var controller = restControllerService.Queryable("Actions")
                                 .Where(c => c.Name == discoveredController.Name).FirstOrDefault();
                if (controller == null)
                {
                    controller = new RestController {
                        Name = discoveredController.Name
                    };
                    restControllerService.Add(controller);
                }
                controller.ClassName = discoveredController.ClassName;

                foreach (var discoveredAction in discoveredController.Actions)
                {
                    var action = controller.Actions.Where(a => a.ApiId == discoveredAction.ApiId).FirstOrDefault();
                    {
                        if (action == null)
                        {
                            action = new RestAction {
                                ApiId = discoveredAction.ApiId
                            };
                            controller.Actions.Add(action);
                        }
                        action.Method = discoveredAction.Method;
                        action.Path   = discoveredAction.Path;
                    }
                }

                var actions = discoveredController.Actions.Select(d => d.ApiId).ToList();
                foreach (var action in controller.Actions.Where(a => !actions.Contains(a.ApiId)).ToList())
                {
                    actionService.Delete(action);
                    controller.Actions.Remove(action);
                }
            }

            var controllers = discoveredControllers.Select(d => d.Name).ToList();

            foreach (var controller in restControllerService.Queryable().Where(c => !controllers.Contains(c.Name)).ToList())
            {
                restControllerService.Delete(controller);
            }

            rockContext.SaveChanges();
        }
        /// <summary>
        /// Registers the controllers.
        /// </summary>
        public static void RegisterControllers()
        {
            /*
             * 12/19/2019 BJW
             *
             * There was an issue with the SecuredAttribute not calculating API ID the same as was being calculated here.
             * This caused the secured attribute to sometimes not find the RestAction record and thus not find the
             * appropriate permissions (Auth table). The new method "GetApiId" is used in both places as a standardized
             * API ID generator to ensure that this does not occur. The following code has also been modified to gracefully
             * update any old style API IDs and update them to the new format without losing any foreign key associations, such
             * as permissions.
             *
             * See task for detailed background: https://app.asana.com/0/474497188512037/1150703513867003/f
             */

            // Controller Class Name => New Format Id => Old Format Id
            var controllerApiIdMap = new Dictionary <string, Dictionary <string, string> >();

            var rockContext           = new RockContext();
            var restControllerService = new RestControllerService(rockContext);
            var discoveredControllers = new List <RestController>();

            var config   = GlobalConfiguration.Configuration;
            var explorer = config.Services.GetApiExplorer();

            foreach (var apiDescription in explorer.ApiDescriptions)
            {
                var reflectedHttpActionDescriptor = ( ReflectedHttpActionDescriptor )apiDescription.ActionDescriptor;
                var action = apiDescription.ActionDescriptor;
                var name   = action.ControllerDescriptor.ControllerName;
                var method = apiDescription.HttpMethod.Method;

                var controller = discoveredControllers.Where(c => c.Name == name).FirstOrDefault();
                if (controller == null)
                {
                    controller = new RestController
                    {
                        Name      = name,
                        ClassName = action.ControllerDescriptor.ControllerType.FullName
                    };

                    discoveredControllers.Add(controller);
                    controllerApiIdMap[controller.ClassName] = new Dictionary <string, string>();
                }

                var apiIdMap = controllerApiIdMap[controller.ClassName];
                var apiId    = GetApiId(reflectedHttpActionDescriptor.MethodInfo, method, controller.Name);

                // Because we changed the format of the stored ApiId, it is possible some RestAction records will have the old
                // style Id, which is apiDescription.ID
                apiIdMap[apiId] = apiDescription.ID;

                controller.Actions.Add(new RestAction
                {
                    ApiId  = apiId,
                    Method = method,
                    Path   = apiDescription.RelativePath
                });
            }

            var actionService = new RestActionService(rockContext);

            foreach (var discoveredController in discoveredControllers)
            {
                var apiIdMap = controllerApiIdMap[discoveredController.ClassName];

                var controller = restControllerService.Queryable("Actions")
                                 .Where(c => c.Name == discoveredController.Name).FirstOrDefault();
                if (controller == null)
                {
                    controller = new RestController {
                        Name = discoveredController.Name
                    };
                    restControllerService.Add(controller);
                }
                controller.ClassName = discoveredController.ClassName;

                foreach (var discoveredAction in discoveredController.Actions)
                {
                    var newFormatId = discoveredAction.ApiId;
                    var oldFormatId = apiIdMap[newFormatId];

                    var action = controller.Actions.Where(a => a.ApiId == newFormatId || a.ApiId == oldFormatId).FirstOrDefault();

                    if (action?.ApiId == oldFormatId)
                    {
                        // Update the ID to the new format
                        action.ApiId = newFormatId;
                    }

                    if (action == null)
                    {
                        action = new RestAction {
                            ApiId = newFormatId
                        };
                        controller.Actions.Add(action);
                    }
                    action.Method = discoveredAction.Method;
                    action.Path   = discoveredAction.Path;
                }

                var actions = discoveredController.Actions.Select(d => d.ApiId).ToList();
                foreach (var action in controller.Actions.Where(a => !actions.Contains(a.ApiId)).ToList())
                {
                    actionService.Delete(action);
                    controller.Actions.Remove(action);
                }
            }

            var controllers = discoveredControllers.Select(d => d.Name).ToList();

            foreach (var controller in restControllerService.Queryable().Where(c => !controllers.Contains(c.Name)).ToList())
            {
                restControllerService.Delete(controller);
            }

            rockContext.SaveChanges();
        }
        /// <summary>
        /// Returns breadcrumbs specific to the block that should be added to navigation
        /// based on the current page reference.  This function is called during the page's
        /// oninit to load any initial breadcrumbs
        /// </summary>
        /// <param name="pageReference">The page reference.</param>
        /// <returns></returns>
        public override List<BreadCrumb> GetBreadCrumbs( PageReference pageReference )
        {
            var breadCrumbs = new List<BreadCrumb>();

            int controllerId = int.MinValue;
            if (int.TryParse(PageParameter( "controller" ), out controllerId))
            {
                var controller = new RestControllerService( new RockContext() ).Get( controllerId );
                if (controller != null)
                {
                    string name = controller.Name.SplitCase();
                    lControllerName.Text = name + " Controller";
                    breadCrumbs.Add(new BreadCrumb(name, pageReference));
                }
            }

            return breadCrumbs;
        }
        /// <summary>
        /// Registers the controllers.
        /// </summary>
        public static void RegisterControllers()
        {
            var rockContext = new RockContext();
            var restControllerService = new RestControllerService( rockContext );

            var existingControllers = restControllerService.Queryable( "Actions" ).ToList();
            var discoveredControllers = new List<RestController>();

            var config = GlobalConfiguration.Configuration;
            var explorer = config.Services.GetApiExplorer();
            foreach(var apiDescription in explorer.ApiDescriptions)
            {
                var action = apiDescription.ActionDescriptor;
                var name = action.ControllerDescriptor.ControllerName;

                var controller = discoveredControllers.Where( c => c.Name == name ).FirstOrDefault();
                if ( controller == null )
                {
                    controller = new RestController
                    {
                        Name = name,
                        ClassName = action.ControllerDescriptor.ControllerType.FullName
                    };

                    discoveredControllers.Add( controller );
                }

                controller.Actions.Add( new RestAction
                {
                    ApiId = apiDescription.ID,
                    Method = apiDescription.HttpMethod.Method,
                    Path = apiDescription.RelativePath
                } );
            }

            var actionService = new RestActionService( rockContext );
            foreach(var discoveredController in discoveredControllers)
            {
                var controller = restControllerService.Queryable( "Actions" )
                    .Where( c => c.Name == discoveredController.Name ).FirstOrDefault();
                if ( controller == null )
                {
                    controller = new RestController { Name = discoveredController.Name };
                    restControllerService.Add( controller );
                }
                controller.ClassName = discoveredController.ClassName;

                foreach(var discoveredAction in discoveredController.Actions)
                {
                    var action = controller.Actions.Where( a => a.ApiId == discoveredAction.ApiId ).FirstOrDefault();
                    {
                        if ( action == null )
                        {
                            action = new RestAction { ApiId = discoveredAction.ApiId };
                            controller.Actions.Add( action );
                        }
                        action.Method = discoveredAction.Method;
                        action.Path = discoveredAction.Path;
                    }
                }

                var actions = discoveredController.Actions.Select( d => d.ApiId).ToList();
                foreach( var action in controller.Actions.Where( a => !actions.Contains(a.ApiId)).ToList())
                {
                    actionService.Delete( action );
                    controller.Actions.Remove(action);
                }
            }

            var controllers = discoveredControllers.Select( d => d.Name ).ToList();
            foreach ( var controller in restControllerService.Queryable().Where( c => !controllers.Contains( c.Name ) ).ToList() )
            {
                restControllerService.Delete( controller );
            }

            rockContext.SaveChanges();
        }
Example #7
0
        /// <summary>
        /// Returns breadcrumbs specific to the block that should be added to navigation
        /// based on the current page reference.  This function is called during the page's
        /// oninit to load any initial breadcrumbs
        /// </summary>
        /// <param name="pageReference">The page reference.</param>
        /// <returns></returns>
        public override List<BreadCrumb> GetBreadCrumbs( PageReference pageReference )
        {
            var breadCrumbs = new List<BreadCrumb>();

            int controllerId = int.MinValue;
            if ( int.TryParse( PageParameter( "controller" ), out controllerId ) )
            {
                var controller = new RestControllerService( new RockContext() ).Get( controllerId );
                if ( controller != null )
                {
                    string name = controller.Name.SplitCase();
                    var controllerType = Reflection.FindTypes( typeof( Rock.Rest.ApiControllerBase ) )
                        .Where( a => a.Key.Equals( controller.ClassName ) ).Select( a => a.Value ).FirstOrDefault();
                    if ( controllerType != null )
                    {
                        var obsoleteAttribute = controllerType.GetCustomAttribute<System.ObsoleteAttribute>();
                        if (obsoleteAttribute != null)
                        {
                            hlblWarning.Text = string.Format( "Obsolete: {1}", controller.Name.SplitCase(), obsoleteAttribute.Message );
                        }
                    }

                    lControllerName.Text = name + " Controller";
                    breadCrumbs.Add( new BreadCrumb( name, pageReference ) );
                }
            }

            return breadCrumbs;
        }