private async Task <ActionResult> Show(string domain, string apiToken) { var apiUri = new UriBuilder("https", domain, 443, "api/v2").Uri; var apiClient = new ManagementApiClient(apiToken, apiUri); var appsTask = apiClient.Clients.GetAllAsync(fields: "name,client_id,global"); var rulesTask = apiClient.Rules.GetAllAsync(fields: "name,script,id,enabled,order"); // if we want rules for all stages: //var rulesStages = new[] { "login_success", "login_failure", "pre_authorize", "user_registration", "user_blocked" }; //var rulesTask = // Task.WhenAll( // rulesStages.Select( // stage => apiClient.Rules.GetAllAsync(fields: "name,script,id,enabled,order", stage: stage))) // .ContinueWith(t => t.Result.SelectMany(r => r)); await Task.WhenAll(appsTask, rulesTask); var apps = appsTask.Result // "All Applications" is a special client app, not meant to be managed by the user. .Where(a => a.Name != "All Applications"); var rules = rulesTask.Result; var matches = RulesMatcher.FindMatches(rules, apps); var model = new AppsViewModel() { Clients = apps.Select( c => new ClientToRulesViewModel() { Client = c, Rules = matches.Where( p => p.Value == null || p.Value.MatchedClients.Contains(c)) .Select( p => new RelatedRuleViewModel(p.Key) { SpecificForApp = p.Value != null }) }), Rules = matches }; this.ViewBag.Domain = domain; return(this.View("Index", model)); }
public void FindsMatches() { var apps = new[] { new Client() { ClientId = "Id0", Name = "Name0" }, new Client() { ClientId = "Id1", Name = "Name1" }, new Client() { ClientId = "Id2", Name = "Name2" }, new Client() { ClientId = "Id3", Name = "RepeatedName" }, new Client() { ClientId = "Id4", Name = "RepeatedName" } }; var rules = new[] { new Rule() { Id = "r0", Script = "xxxx if (context.clientId === 'Id0') xxx" }, new Rule() { Id = "r1", Script = "xxx if (context.clientId === 'Id1') xxx" }, new Rule() { Id = "r2", Script = "xxx if (context.clientId !== 'Id0') xxx" }, new Rule() { Id = "r3", Script = "xxx if (context.clientName === 'Name0') xxx" }, new Rule() { Id = "r4", Script = "xxx NoAppReference xxx" }, new Rule() { Id = "r5", Script = "xxx if (context.clientId === 'Id9') xxx" }, new Rule() { Id = "r6", Script = "xxx if (context.clientName === 'RepeatedName') xxx" } }; var result = RulesMatcher.FindMatches(rules, apps); Assert.IsTrue(result[rules[0]].MatchedClients.Contains(apps[0])); Assert.IsTrue(result[rules[2]].MatchedClients.Contains(apps[0])); Assert.IsTrue(result[rules[3]].MatchedClients.Contains(apps[0])); Assert.IsTrue(result[rules[1]].MatchedClients.Contains(apps[1])); Assert.IsTrue(result[rules[5]].Reference.ClientId == "Id9" && !result[rules[5]].MatchedClients.Any(), "There should be a mention to Rule 5, even if the client reference doesn't match any client."); Assert.IsNull(result[rules[4]], "Rule 4 should have a null value because it has no reference."); Assert.IsTrue(result[rules[6]].MatchedClients.Contains(apps[3]) && result[rules[6]].MatchedClients.Contains(apps[4]), "Rule 6 should contain reference to both applications that have the same name."); Assert.AreEqual(rules.Length, result.Count); }