-
Notifications
You must be signed in to change notification settings - Fork 8
Feature: Move import function from KibaliTool #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thewahome
wants to merge
2
commits into
main
Choose a base branch
from
feature/move-import-functionality-to-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| using Kibali; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Kibali | ||
| { | ||
| public class PermissionsImporter | ||
| { | ||
|
|
||
| public async Task<PermissionsDocument> Import(string inputFile, string permissionsFile) | ||
| { | ||
| var doc = new PermissionsDocument(); | ||
| JsonElement rootObject; | ||
| JsonElement permissionsObject; | ||
|
|
||
| using (var client = new HttpClient()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should make this instance a static member? |
||
| { | ||
| rootObject = await FetchAndParseJsonAsync(inputFile, client); | ||
| permissionsObject = await FetchAndParseJsonAsync(permissionsFile, client); | ||
| } | ||
|
|
||
| var apiPermissions = rootObject.GetProperty("ApiPermissions"); | ||
|
|
||
| CreatePermissions(doc, permissionsObject); | ||
|
|
||
| var entries = CreatePermissionsEntries(apiPermissions); | ||
|
|
||
| var permissionsInfoList = entries.GroupBy(pe => pe.Permission) | ||
| .Select(gr => new { Permission = gr.Key, Paths = gr.GroupBy(p => p.Path) }) | ||
| .OrderBy(gr => gr.Permission); | ||
|
|
||
| foreach (var permissionInfo in permissionsInfoList) | ||
| { | ||
| if (!doc.Permissions.ContainsKey(permissionInfo.Permission)) | ||
| { | ||
| doc.Permissions.Add(permissionInfo.Permission, new Permission()); | ||
| } | ||
|
|
||
| var perm = doc.Permissions[permissionInfo.Permission]; | ||
| foreach (var pathDetails in permissionInfo.Paths) | ||
| { | ||
| SortedSet<string> methods = new SortedSet<string>(); | ||
| SortedSet<string> schemes = new SortedSet<string>(); | ||
| foreach (var entry in pathDetails) | ||
| { | ||
| methods.Add(entry.Method); | ||
| schemes.Add(entry.Scheme); | ||
| } | ||
| var pathSet = GetOrCreatePathSet(perm, methods, schemes); | ||
| pathSet.Paths.Add(pathDetails.Key, string.Empty); | ||
| } | ||
| } | ||
| return doc; | ||
| } | ||
|
|
||
| private void CreatePermissions(PermissionsDocument doc, JsonElement permissionsObject) | ||
| { | ||
| var delegatedElement = permissionsObject.GetProperty("delegatedScopesList"); | ||
| foreach (var entry in delegatedElement.EnumerateArray()) | ||
| { | ||
| var name = entry.GetProperty("value").GetString(); | ||
| if (!doc.Permissions.TryGetValue(name, out var permission)) | ||
| { | ||
| permission = new Permission(); | ||
| } | ||
|
|
||
| var scheme = new Scheme | ||
| { | ||
| RequiresAdminConsent = entry.GetProperty("isAdmin").GetBoolean(), | ||
| AdminDescription = entry.GetProperty("adminConsentDescription").GetString(), | ||
| AdminDisplayName = entry.GetProperty("adminConsentDisplayName").GetString(), | ||
| UserDescription = entry.GetProperty("consentDescription").GetString(), | ||
| UserDisplayName = entry.GetProperty("consentDisplayName").GetString() | ||
| }; | ||
|
|
||
| permission.Schemes["DelegatedWork"] = scheme; | ||
|
|
||
| doc.Permissions.Add(name, permission); | ||
| } | ||
|
|
||
|
|
||
| var applicationElement = permissionsObject.GetProperty("applicationScopesList"); | ||
| foreach (var entry in applicationElement.EnumerateArray()) | ||
| { | ||
| var name = entry.GetProperty("value").GetString(); | ||
| if (!doc.Permissions.TryGetValue(name, out var permission)) | ||
| { | ||
| permission = new Permission(); | ||
| doc.Permissions.Add(name, permission); | ||
| } | ||
|
|
||
| var scheme = new Scheme | ||
| { | ||
| RequiresAdminConsent = entry.GetProperty("isAdmin").GetBoolean(), | ||
| AdminDescription = entry.GetProperty("consentDescription").GetString(), | ||
| AdminDisplayName = entry.GetProperty("consentDisplayName").GetString(), | ||
| }; | ||
|
|
||
| permission.Schemes["Application"] = scheme; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private List<PermissionEntry> CreatePermissionsEntries(JsonElement apiPermissions) | ||
| { | ||
| List<PermissionEntry> entries = new(); | ||
|
|
||
| foreach (var path in apiPermissions.EnumerateObject()) | ||
| { | ||
| foreach (var method in path.Value.EnumerateObject()) | ||
| { | ||
| foreach (var scheme in method.Value.EnumerateObject()) | ||
| { | ||
| foreach (var permission in scheme.Value.EnumerateArray()) | ||
| { | ||
| entries.Add(new PermissionEntry() | ||
| { | ||
| Permission = permission.GetString(), | ||
| Path = path.Name, | ||
| Method = method.Name, | ||
| Scheme = scheme.Name | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return entries; | ||
| } | ||
|
|
||
| private async Task<JsonElement> FetchAndParseJsonAsync(string path, HttpClient client) | ||
| { | ||
| Stream inputStream = path.StartsWith("http") | ||
| ? await client.GetStreamAsync(path) | ||
| : new FileStream(path, FileMode.Open); | ||
|
|
||
| using (inputStream) | ||
| { | ||
| var jsonDoc = await JsonDocument.ParseAsync(inputStream); | ||
| return jsonDoc.RootElement; | ||
| } | ||
| } | ||
|
|
||
| private PathSet GetOrCreatePathSet(Permission perm, SortedSet<string> methods, SortedSet<string> schemes) | ||
| { | ||
| var pathSet = perm.PathSets.FirstOrDefault(x => x.SchemeKeys.SetEquals(schemes) && x.Methods.SetEquals(methods)); | ||
| if (pathSet != null) | ||
| return pathSet; | ||
|
|
||
| var newPathSet = new PathSet() | ||
| { | ||
| Methods = methods, | ||
| SchemeKeys = schemes | ||
| }; | ||
| perm.PathSets.Add(newPathSet); | ||
| return newPathSet; | ||
| } | ||
| } | ||
| public class PermissionEntry | ||
| { | ||
| public string Path { get; set; } | ||
| public string Method { get; set; } | ||
| public string Scheme { get; set; } | ||
| public string Permission { get; set; } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.