public static object ModuleFeatureToJsonModelRef(Site site, string path)
        {
            ModulesSection section = GetModulesSection(site, path);

            ModulesId id = new ModulesId(site?.Id, path, section.IsLocallyStored);

            var obj = new {
                id    = id.Uuid,
                scope = site == null ? string.Empty : site.Name + path
            };

            return(Core.Environment.Hal.Apply(Defines.ModulesResource.Guid, obj, false));
        }
Exemplo n.º 2
0
        public object Get(string id)
        {
            ModulesId modulesId = new ModulesId(id);

            Site site = modulesId.SiteId == null ? null : SiteHelper.GetSite(modulesId.SiteId.Value);

            if (modulesId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            return(ModuleHelper.ModuleFeatureToJsonModel(site, modulesId.Path));
        }
        internal static object ModuleFeatureToJsonModel(Site site, string path)
        {
            ModulesSection section = GetModulesSection(site, path);

            ModulesId id = new ModulesId(site?.Id, path, section.IsLocallyStored);

            var obj = new {
                id       = id.Uuid,
                scope    = site == null ? string.Empty : site.Name + path,
                metadata = ConfigurationUtility.MetadataToJson(section.IsLocallyStored, section.IsLocked, section.OverrideMode, section.OverrideModeEffective),
                run_all_managed_modules_for_all_requests = section.RunAllManagedModulesForAllRequests,
                website = SiteHelper.ToJsonModelRef(site)
            };

            return(Core.Environment.Hal.Apply(Defines.ModulesResource.Guid, obj));
        }
Exemplo n.º 4
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            ModulesId modulesId = new ModulesId(id);

            Site site = modulesId.SiteId == null ? null : SiteHelper.GetSite(modulesId.SiteId.Value);

            if (modulesId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            ModuleHelper.EnsureValidScope(site, modulesId.Path);

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            // Check for config_scope
            string         configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model);
            ModulesSection section    = ModuleHelper.GetModulesSection(site, modulesId.Path, configPath);

            try {
                DynamicHelper.If <bool>((object)model.run_all_managed_modules_for_all_requests, v => section.RunAllManagedModulesForAllRequests = v);

                if (model.metadata != null)
                {
                    DynamicHelper.If <OverrideMode>((object)model.metadata.override_mode, v => section.OverrideMode = v);
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }

            ManagementUnit.Current.Commit();

            return(ModuleHelper.ModuleFeatureToJsonModel(site, modulesId.Path));
        }
Exemplo n.º 5
0
        public void Delete(string id)
        {
            ModulesId modulesId = new ModulesId(id);

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;

            Site site = (modulesId.SiteId != null) ? SiteHelper.GetSite(modulesId.SiteId.Value) : null;

            if (site == null)
            {
                return;
            }

            ModuleHelper.EnsureValidScope(site, modulesId.Path);

            ModulesSection section = ModuleHelper.GetModulesSection(site, modulesId.Path, ManagementUnit.ResolveConfigScope());

            section.RevertToParent();

            ManagementUnit.Current.Commit();
        }
Exemplo n.º 6
0
        public object Get()
        {
            string modulesUuid = Context.Request.Query[Defines.MODULES_IDENTIFIER];

            if (string.IsNullOrEmpty(modulesUuid))
            {
                return(new StatusCodeResult((int)HttpStatusCode.NotFound));
            }

            ModulesId id = new ModulesId(modulesUuid);

            Site site = id.SiteId == null ? null : SiteHelper.GetSite(id.SiteId.Value);

            List <Module> modules = ModuleHelper.GetModules(site, id.Path);

            // Set HTTP header for total count
            this.Context.Response.SetItemsCount(modules.Count());

            Fields fields = Context.Request.GetFields();

            return(new {
                entries = modules.Select(module => ModuleHelper.ModuleToJsonModelRef(module, site, id.Path, fields))
            });
        }
Exemplo n.º 7
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.modules == null || !(model.modules is JObject))
            {
                throw new ApiArgumentException("modules");
            }

            string modulesUuid = DynamicHelper.Value(model.modules.id);

            if (modulesUuid == null)
            {
                throw new ApiArgumentException("modules.id");
            }

            // Get the feature id
            Module    module    = null;
            ModulesId modulesId = new ModulesId(modulesUuid);
            Site      site      = modulesId.SiteId == null ? null : SiteHelper.GetSite(modulesId.SiteId.Value);

            if (modulesId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            ModuleHelper.EnsureValidScope(site, modulesId.Path);

            string         configPath = ManagementUnit.ResolveConfigScope(model);
            ModulesSection section    = ModuleHelper.GetModulesSection(site, modulesId.Path, configPath);

            // The post could either be creating a Managed module, or adding an existing
            // global module to the modules list.
            // This information is taken from the model's type value
            string type = DynamicHelper.Value(model.type);

            if (string.IsNullOrEmpty(type))
            {
                // The module being added is a global/native module

                string name = DynamicHelper.Value(model.name);

                if (string.IsNullOrEmpty(name))
                {
                    throw new ApiArgumentException("name");
                }

                GlobalModule existingGlobalModule = ModuleHelper.GetGlobalModules().FirstOrDefault(m => m.Name.Equals(name));

                // Adding a global module to the modules list means it must already exist in global modules
                if (existingGlobalModule == null)
                {
                    throw new NotFoundException("name");
                }

                // Add the existing global module
                module = ModuleHelper.AddExistingGlobalModule(existingGlobalModule, section);
                ManagementUnit.Current.Commit();
            }
            else
            {
                // Module being added to enabled modules is a managed module

                // Create module from model
                module = ModuleHelper.CreateManagedModule(model, section);

                // Save it
                ModuleHelper.AddManagedModule(module, section);
                ManagementUnit.Current.Commit();
            }


            //
            // Create response
            dynamic moduleEntry = ModuleHelper.ModuleToJsonModel(module, site, modulesId.Path);

            return(Created(ModuleHelper.GetModuleEntryLocation(moduleEntry.id), moduleEntry));
        }