public bool CreateGroup(IPermissionGroup group)
        {
            GuardLoaded();
            GuardTarget(group);

            GroupPermissionSection section = GetConfigSection <GroupPermissionSection>(group, true);

            section.Name     = group.Name;
            section.Priority = group.Priority;
            section.Save();
            return(true);
        }
        public async Task <bool> CreateGroupAsync(IPermissionGroup group)
        {
            GuardLoaded();
            GuardTarget(group);

            GroupPermissionSection section = await GetConfigSectionAsync <GroupPermissionSection>(group, true);

            section.Name     = group.Name;
            section.Priority = group.Priority;
            await section.SaveAsync();

            return(true);
        }
        public bool UpdateGroup(IPermissionGroup group)
        {
            GuardLoaded();
            GuardTarget(group);

            GroupPermissionSection section = GetConfigSection <GroupPermissionSection>(group, false);

            if (section == null)
            {
                return(false);
            }

            section.Name     = group.Name;
            section.Priority = group.Priority;
            section.Save();
            return(true);
        }
        public async Task <T> GetConfigSectionAsync <T>(IPermissionActor target, bool createIfNotFound) where T : PermissionSection
        {
            GuardTarget(target);

            bool isPermissionGroup = target is IPermissionGroup;

            IConfiguration config = null;
            string         path   = null;

            if (isPermissionGroup)
            {
                config = GroupsConfig;
                path   = "Groups";
            }
            else
            {
                config = PlayersConfig;
                path   = "Users";
            }

            if (createIfNotFound && !config.ChildExists(path))
            {
                config.CreateSection(path, SectionType.Array);
                await config.SaveAsync();
            }
            else if (!createIfNotFound && !config.ChildExists(path))
            {
                return(null);
            }

            IConfigurationElement configElement = config[path];

            if (configElement.Type != SectionType.Array)
            {
                throw new Exception("Expected array type but got " + configElement.Type);
            }

            List <PermissionSection> values = configElement.Get <PermissionSection[]>().ToList();

            if (!values.Any(c => c.Id.Equals(target.Id)))
            {
                if (!createIfNotFound)
                {
                    return(null);
                }

                PermissionSection toCreate;
                if (target is IPermissionGroup)
                {
                    toCreate = new GroupPermissionSection(target.Id, configElement);
                }
                else
                {
                    toCreate = new PlayerPermissionSection(target.Id, configElement);
                }

                await toCreate.SaveAsync();
            }

            T section = configElement.Get <T[]>().FirstOrDefault(c => c.Id == target.Id);

            section?.SetConfigElement(configElement);
            return(section);
        }