Beispiel #1
0
    public async Task <ActionResult <JObject> > EditSystem([FromBody] JObject changes)
    {
        var system = await _repo.GetSystem(User.CurrentSystem());

        var patch = SystemPatch.FromJSON(changes);

        patch.AssertIsValid();
        if (patch.Errors.Count > 0)
        {
            var err = patch.Errors[0];
            if (err is FieldTooLongError)
            {
                return(BadRequest($"Field {err.Key} is too long "
                                  + $"({(err as FieldTooLongError).ActualLength} > {(err as FieldTooLongError).MaxLength})."));
            }

            return(BadRequest($"Field {err.Key} is invalid."));
        }

        system = await _repo.UpdateSystem(system !.Id, patch);

        return(Ok(system.ToJson(User.ContextFor(system))));
    }
Beispiel #2
0
    public async Task <IActionResult> DoSystemPatch(string systemRef, [FromBody] JObject data)
    {
        var system = await ResolveSystem(systemRef);

        if (system == null)
        {
            throw Errors.SystemNotFound;
        }
        if (ContextFor(system) != LookupContext.ByOwner)
        {
            throw Errors.GenericMissingPermissions;
        }
        var patch = SystemPatch.FromJSON(data);

        patch.AssertIsValid();
        if (patch.Errors.Count > 0)
        {
            throw new ModelParseError(patch.Errors);
        }

        var newSystem = await _repo.UpdateSystem(system.Id, patch);

        return(Ok(newSystem.ToJson(LookupContext.ByOwner)));
    }
    private async Task <ImportResultNew> ImportPluralKit(JObject importFile)
    {
        var patch = SystemPatch.FromJSON(importFile, isImport: true);

        patch.AssertIsValid();
        if (patch.Errors.Count > 0)
        {
            var err = patch.Errors[0];
            if (err is FieldTooLongError)
            {
                throw new ImportException($"Field {err.Key} in export file is too long "
                                          + $"({(err as FieldTooLongError).ActualLength} > {(err as FieldTooLongError).MaxLength}).");
            }
            if (err.Text != null)
            {
                throw new ImportException(err.Text);
            }
            throw new ImportException($"Field {err.Key} in export file is invalid.");
        }

        await _repo.UpdateSystem(_system.Id, patch, _conn);

        if (importFile.ContainsKey("config"))
        {
            var configPatch = SystemConfigPatch.FromJson(importFile.Value <JObject>("config"));

            if (importFile.ContainsKey("timezone"))
            {
                configPatch.UiTz = importFile.Value <string>("timezone");
            }

            configPatch.AssertIsValid();
            if (configPatch.Errors.Count > 0)
            {
                throw new ImportException($"Field config.{patch.Errors[0].Key} in export file is invalid.");
            }

            await _repo.UpdateSystemConfig(_system.Id, configPatch, _conn);
        }

        var members  = importFile.Value <JArray>("members");
        var switches = importFile.Value <JArray>("switches");
        var groups   = importFile.Value <JArray>("groups");

        var newMembers = members.Count(m =>
        {
            var(found, _) = TryGetExistingMember(m.Value <string>("id"), m.Value <string>("name"));
            return(found == null);
        });

        await AssertMemberLimitNotReached(newMembers);

        if (groups != null)
        {
            var newGroups = groups.Count(g =>
            {
                var(found, _) = TryGetExistingGroup(g.Value <string>("id"), g.Value <string>("name"));
                return(found == null);
            });
            await AssertGroupLimitNotReached(newGroups);
        }

        foreach (JObject member in members)
        {
            await ImportMember(member);
        }

        if (groups != null)
        {
            foreach (JObject group in groups)
            {
                await ImportGroup(group);
            }
        }

        if (switches.Any(sw =>
                         sw.Value <JArray>("members").Any(m => !_knownMemberIdentifiers.ContainsKey((string)m))))
        {
            throw new ImportException("One or more switches include members that haven't been imported.");
        }

        await ImportSwitches(switches);

        return(_result);
    }