コード例 #1
0
        public static Module CreateManagedModule(dynamic model, ModulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

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

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

            Module module = section.Modules.CreateElement();

            module.Name         = name;
            module.Type         = type;
            module.PreCondition = DynamicHelper.Value(model.precondition) ?? module.PreCondition;

            return(module);
        }
コード例 #2
0
        public static void DeleteModule(string moduleName, Site site, string path, string configPath = null)
        {
            if (moduleName == null)
            {
                throw new ArgumentNullException("moduleName");
            }

            ModulesSection section = GetModulesSection(site, path, configPath);

            ModuleCollection collection = section.Modules;

            Module module = null;

            if (!ExistsModule(moduleName, collection, out module))
            {
                return;
            }

            if (ModuleIsLocked(module) && site != null)
            {
                // Can't delete the module if it is locked
                // Unless the scope is at server level
                throw new InvalidOperationException("Lock violation");
            }

            try {
                collection.Remove(moduleName);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
コード例 #3
0
        private static ModuleCollection GetModulesCollection(Site site, string path, string configPath = null)
        {
            ModulesSection section = GetModulesSection(site, path, configPath);

            ModuleCollection collection = section.Modules;

            if (collection == null)
            {
                throw new Exception(ModulesErrors.ConfigurationError);
            }

            return(collection);
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        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));
        }
コード例 #6
0
        public static void EnableOrdering(Site site, string path)
        {
            ModulesSection section = GetModulesSection(site, path);

            // Make all the inherited modules local be re-adding them to config
            ICollection modules = section.Modules;

            Module[] list = new Module[modules.Count];

            modules.CopyTo(list, 0);

            section.Modules.Clear();

            foreach (Module action in list)
            {
                section.Modules.AddCopy(action);
            }
        }
コード例 #7
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));
        }
コード例 #8
0
        public static Module AddExistingGlobalModule(GlobalModule globalModule, ModulesSection section)
        {
            if (globalModule == null)
            {
                throw new ArgumentNullException("globalModule");
            }

            ModuleCollection        collection       = section.Modules;
            GlobalModulesCollection globalCollection = GetGlobalModulesCollection();

            GlobalModule element = null;

            if (!IsGlobalModule(globalModule.Name, globalCollection, out element))
            {
                throw new Exception(ModulesErrors.ModuleNotPresentInGlobalModulesError);
            }

            Module module = null;

            if (ExistsModule(globalModule.Name, collection, out module))
            {
                throw new Exception(ModulesErrors.ModuleAlreadyPresentError);
            }

            try {
                module = collection.Add(globalModule.Name);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }

            if (!String.IsNullOrEmpty(element.PreCondition))
            {
                module.PreCondition = element.PreCondition;
            }

            return(module);
        }
コード例 #9
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();
        }
コード例 #10
0
        public static void AddManagedModule(Module module, ModulesSection section)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }

            ModuleCollection        serverCollection = section.Modules;
            GlobalModulesCollection globalCollection = GetGlobalModulesCollection();

            GlobalModule element = null;

            if (IsGlobalModule(module.Name, globalCollection, out element))
            {
                throw new ApiArgumentException("Module already exists", "name");
            }

            Module newModule = null;

            if (ExistsModule(module.Name, serverCollection, out newModule))
            {
                throw new ApiArgumentException("Module already exists", "name");
            }

            try {
                newModule = serverCollection.Add(module.Name, module.Type);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }

            if (!String.IsNullOrEmpty(module.PreCondition))
            {
                newModule.PreCondition = module.PreCondition;
            }
        }
コード例 #11
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));
        }