/// <summary>
 /// Clones this RestController object to a new RestController object
 /// </summary>
 /// <param name="source">The source.</param>
 /// <param name="deepCopy">if set to <c>true</c> a deep copy is made. If false, only the basic entity properties are copied.</param>
 /// <returns></returns>
 public static RestController Clone(this RestController source, bool deepCopy)
 {
     if (deepCopy)
     {
         return(source.Clone() as RestController);
     }
     else
     {
         var target = new RestController();
         target.CopyPropertiesFrom(source);
         return(target);
     }
 }
Example #2
0
 /// <summary>
 /// Copies the properties from another RestController object to this RestController object
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="source">The source.</param>
 public static void CopyPropertiesFrom(this RestController target, RestController source)
 {
     target.Id                      = source.Id;
     target.ClassName               = source.ClassName;
     target.ForeignGuid             = source.ForeignGuid;
     target.ForeignKey              = source.ForeignKey;
     target.Name                    = source.Name;
     target.CreatedDateTime         = source.CreatedDateTime;
     target.ModifiedDateTime        = source.ModifiedDateTime;
     target.CreatedByPersonAliasId  = source.CreatedByPersonAliasId;
     target.ModifiedByPersonAliasId = source.ModifiedByPersonAliasId;
     target.Guid                    = source.Guid;
     target.ForeignId               = source.ForeignId;
 }
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();
        }