/// <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 )
        {
            base.OnLoad( e );

            if ( !Page.IsPostBack )
            {
                var service = new RestActionService( new RockContext() );
                var restAction = service.Get( PageParameter( "RestActionId" ).AsInteger() );

                if ( restAction != null )
                {
                    var config = GlobalConfiguration.Configuration;
                    var explorer = config.Services.GetApiExplorer();
                    var controllerActions = explorer.ApiDescriptions.Where( a => ( a.ActionDescriptor.ControllerDescriptor.ControllerName == restAction.Controller.Name ) );
                    var apiDescription = controllerActions.FirstOrDefault( a => a.ID == restAction.ApiId );
                    if ( apiDescription != null )
                    {
                        hfUrl.Value = this.ResolveUrl( "~/" + apiDescription.RelativePath );
                        lUrlPreview.Text = apiDescription.HttpMethod.ToString().ToUpper() + " " + hfUrl.Value;
                        btnDELETE.Visible = apiDescription.HttpMethod == System.Net.Http.HttpMethod.Delete;

                        btnGET.Visible = apiDescription.HttpMethod == System.Net.Http.HttpMethod.Get;
                        rblLoadAttributes.Visible = apiDescription.HttpMethod == System.Net.Http.HttpMethod.Get;

                        btnPUT.Visible = apiDescription.HttpMethod == System.Net.Http.HttpMethod.Put;
                        btnPOST.Visible = apiDescription.HttpMethod == System.Net.Http.HttpMethod.Post;
                        tbPayload.Visible = apiDescription.HttpMethod == System.Net.Http.HttpMethod.Post || apiDescription.HttpMethod == System.Net.Http.HttpMethod.Put;

                        foreach ( var param in apiDescription.ParameterDescriptions )
                        {
                            if ( param.Source == System.Web.Http.Description.ApiParameterSource.FromUri )
                            {
                                lstParameterValues.Value += param.Name + "|";
                            }
                        }
                    }
                    else
                    {
                        // todo warning
                    }
                }
            }
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="itemKey">The item key.</param>
        /// <param name="itemKeyValue">The item key value.</param>
        public void BindGrid()
        {
            int controllerId = int.MinValue;
            if ( int.TryParse( PageParameter( "controller" ), out controllerId ) )
            {
                var service = new RestActionService( new RockContext() );
                var sortProperty = gActions.SortProperty;

                IQueryable<RestAction> qry = service.Queryable()
                    .Where( a => a.ControllerId == controllerId );

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

                gActions.DataSource = qry.ToList();
                gActions.DataBind();
            }
        }
Esempio n. 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>
        /// 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();
        }