示例#1
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            MappingId mappingId = new MappingId(id);

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

            if (mappingId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            string          configPath = ManagementUnit.ResolveConfigScope(model);
            HandlersSection section    = HandlersHelper.GetHandlersSection(site, mappingId.Path, configPath);
            Mapping         mapping    = section.Mappings.FirstOrDefault(u => u.Name.Equals(mappingId.Name));

            if (mapping == null)
            {
                return(NotFound());
            }

            MappingsHelper.UpdateMapping(model, mapping, section);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic m = MappingsHelper.ToJsonModel(mapping, site, mappingId.Path);

            if (m.id != id)
            {
                return(LocationChanged(MappingsHelper.GetLocation(m.id), m));
            }

            return(m);
        }
        internal static object ToJsonModel(Site site, string path)
        {
            HandlersSection section = GetHandlersSection(site, path);

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

            // Access Policy
            HandlerAccessPolicy accessPolicy = section.AccessPolicy;

            Dictionary <string, bool> allowedAccess = new Dictionary <string, bool>();

            allowedAccess.Add("read", accessPolicy.HasFlag(HandlerAccessPolicy.Read));
            allowedAccess.Add("write", accessPolicy.HasFlag(HandlerAccessPolicy.Write));
            allowedAccess.Add("execute", accessPolicy.HasFlag(HandlerAccessPolicy.Execute));
            allowedAccess.Add("source", accessPolicy.HasFlag(HandlerAccessPolicy.Source));
            allowedAccess.Add("script", accessPolicy.HasFlag(HandlerAccessPolicy.Script));

            Dictionary <string, bool> remoteAccessPrevention = new Dictionary <string, bool>();

            remoteAccessPrevention.Add("write", accessPolicy.HasFlag(HandlerAccessPolicy.NoRemoteWrite));
            remoteAccessPrevention.Add("read", accessPolicy.HasFlag(HandlerAccessPolicy.NoRemoteRead));
            remoteAccessPrevention.Add("execute", accessPolicy.HasFlag(HandlerAccessPolicy.NoRemoteExecute));
            remoteAccessPrevention.Add("script", accessPolicy.HasFlag(HandlerAccessPolicy.NoRemoteScript));

            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),
                allowed_access           = allowedAccess,
                remote_access_prevention = remoteAccessPrevention,
                website                  = SiteHelper.ToJsonModelRef(site)
            };

            return(Core.Environment.Hal.Apply(Defines.Resource.Guid, obj));
        }
示例#3
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.handler == null || !(model.handler is JObject))
            {
                throw new ApiArgumentException("handler");
            }
            string handlersUuid = DynamicHelper.Value(model.handler.id);

            if (handlersUuid == null)
            {
                throw new ApiArgumentException("handler.id");
            }

            // Get the feature id
            HandlersId handlersId = new HandlersId(handlersUuid);

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

            string          configPath = ManagementUnit.ResolveConfigScope(model);
            HandlersSection section    = HandlersHelper.GetHandlersSection(site, handlersId.Path, configPath);

            Mapping mapping = MappingsHelper.CreateMapping(model, section);

            MappingsHelper.AddMapping(mapping, section);

            ManagementUnit.Current.Commit();

            dynamic m = MappingsHelper.ToJsonModel(mapping, site, handlersId.Path);

            return(Created(MappingsHelper.GetLocation(m.id), m));
        }
        public static void AddMapping(Mapping mapping, HandlersSection section)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }
            if (string.IsNullOrEmpty(mapping.Name))
            {
                throw new ArgumentNullException("mapping.Name");
            }
            if (string.IsNullOrEmpty(mapping.Path))
            {
                throw new ArgumentNullException("mapping.Path");
            }

            if (section.Mappings.Any(m => m.Name.Equals(mapping.Name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new AlreadyExistsException("mapping");
            }

            try {
                section.Mappings.Add(mapping);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void UpdateMapping(dynamic model, Mapping mapping, HandlersSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            try {
                string name = DynamicHelper.Value(model.name);
                if (!string.IsNullOrEmpty(name))
                {
                    // Check if trying to change to a different name, if so make sure it doesn't already exist
                    if (section.Mappings.Any(m => m.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) &&
                        !mapping.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new AlreadyExistsException("mapping.name");
                    }

                    mapping.Name = name;
                }

                SetMapping(model, mapping);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
示例#6
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            HandlersId handlersId = new HandlersId(id);

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

            if (handlersId.SiteId != null && site == null)
            {
                return(NotFound());
            }

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

            // Check for config_scope
            string          configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model);;
            HandlersSection section    = HandlersHelper.GetHandlersSection(site, handlersId.Path, configPath);

            HandlersHelper.UpdateFeatureSettings(model, section);

            ManagementUnit.Current.Commit();

            return(HandlersHelper.ToJsonModel(site, handlersId.Path));
        }
        public static object ToJsonModelRef(Site site, string path)
        {
            HandlersSection section = GetHandlersSection(site, path);

            HandlersId id = new HandlersId(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.Resource.Guid, obj, false));
        }
示例#8
0
        public void Delete(string id)
        {
            HandlersId handlersId = new HandlersId(id);

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

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

            if (site == null)
            {
                return;
            }

            HandlersSection section = HandlersHelper.GetHandlersSection(site, handlersId.Path, ManagementUnit.ResolveConfigScope());

            section.RevertToParent();

            ManagementUnit.Current.Commit();
        }
        public static void DeleteMapping(Mapping mapping, HandlersSection section)
        {
            if (mapping == null)
            {
                return;
            }

            // To remove it we must first make sure we pulled it from this exact section
            mapping = section.Mappings.FirstOrDefault(m => m.Name.Equals(mapping.Name, StringComparison.OrdinalIgnoreCase));

            if (mapping != null)
            {
                try {
                    section.Mappings.Remove(mapping);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
        public static Mapping CreateMapping(dynamic model, HandlersSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            string name = DynamicHelper.Value(model.name);

            if (string.IsNullOrEmpty(name))
            {
                throw new ApiArgumentException("name");
            }
            if (string.IsNullOrEmpty(DynamicHelper.Value(model.path)))
            {
                throw new ApiArgumentException("path");
            }

            Mapping mapping = section.Mappings.CreateElement();

            mapping.Name = name;
            SetMapping(model, mapping);

            return(mapping);
        }
        public static void UpdateFeatureSettings(dynamic model, HandlersSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }

            try {
                if (model.allowed_access != null)
                {
                    Dictionary <string, bool> accessPolicyDictionary = null;

                    try {
                        accessPolicyDictionary = JsonConvert.DeserializeObject <Dictionary <string, bool> >(model.allowed_access.ToString());
                    }
                    catch (JsonSerializationException e) {
                        throw new ApiArgumentException("allowed_access", e);
                    }

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

                    Dictionary <string, HandlerAccessPolicy> accessPolicyMap = new Dictionary <string, HandlerAccessPolicy>()
                    {
                        { "read", HandlerAccessPolicy.Read },
                        { "write", HandlerAccessPolicy.Write },
                        { "execute", HandlerAccessPolicy.Execute },
                        { "source", HandlerAccessPolicy.Source },
                        { "script", HandlerAccessPolicy.Script }
                    };

                    foreach (var key in accessPolicyMap.Keys)
                    {
                        if (accessPolicyDictionary.ContainsKey(key))
                        {
                            if (accessPolicyDictionary[key])
                            {
                                section.AccessPolicy |= accessPolicyMap[key];
                            }
                            else
                            {
                                section.AccessPolicy &= ~accessPolicyMap[key];
                            }
                        }
                    }
                }

                if (model.remote_access_prevention != null)
                {
                    Dictionary <string, bool> remoteAccessDictionary = null;

                    try {
                        remoteAccessDictionary = JsonConvert.DeserializeObject <Dictionary <string, bool> >(model.remote_access_prevention.ToString());
                    }
                    catch (JsonSerializationException e) {
                        throw new ApiArgumentException("remote_access_prevention", e);
                    }

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

                    Dictionary <string, HandlerAccessPolicy> remoteAccessMap = new Dictionary <string, HandlerAccessPolicy>()
                    {
                        { "read", HandlerAccessPolicy.NoRemoteRead },
                        { "write", HandlerAccessPolicy.NoRemoteWrite },
                        { "execute", HandlerAccessPolicy.NoRemoteExecute },
                        { "script", HandlerAccessPolicy.NoRemoteScript }
                    };

                    foreach (var key in remoteAccessMap.Keys)
                    {
                        if (remoteAccessDictionary.ContainsKey(key))
                        {
                            if (remoteAccessDictionary[key])
                            {
                                section.AccessPolicy |= remoteAccessMap[key];
                            }
                            else
                            {
                                section.AccessPolicy &= ~remoteAccessMap[key];
                            }
                        }
                    }
                }

                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);
            }
        }
        public static List <Mapping> GetMappings(Site site, string path, string configPath = null)
        {
            HandlersSection section = HandlersHelper.GetHandlersSection(site, path, configPath);

            return(section.Mappings.ToList());
        }