/// <summary> /// Get the gated client as a string for logging purposes /// </summary> /// <param name="client">client</param> /// <returns>formatted string</returns> private static string GatedClientAsString(GatedClient client) { if (client == null) { return("<NULL>"); } return(string.Format(CultureInfo.InvariantCulture, "{0},{1}", client.Name ?? "<NULL>", client.Version != null ? client.Version.ToString() : "<NONE>")); }
/// <summary> /// Merge two gated clients /// </summary> /// <param name="client1">first client</param> /// <param name="client2">second client</param> /// <returns>merged gated client</returns> /// <remarks>The name must be the same on both clients. The Version returned is the /// greatest version of the two clients. If one or no client has a product code, that /// code is used, if both have a product code, the code from the client with the greatest /// version is used.</remarks> public static GatedClient MergeClient(GatedClient client1, GatedClient client2) { if (client1 == null) { return(client2); } if (client2 == null) { return(client1); } if (!string.Equals(client1.Name, client2.Name, StringComparison.OrdinalIgnoreCase)) { ULSLogging.LogTraceTag(0x23821047 /* tag_967bh */, Categories.GateSelection, Levels.Error, "The name of the clients must be the same. Client1 '{0}', Client2 '{1}'.", client1.Name, client2.Name); return(null); } GatedClient mergedClient = new GatedClient { Name = client1.Name }; if (client1.Version == null) { mergedClient.Version = client2.Version; mergedClient.ProductCode = client2.ProductCode ?? client1.ProductCode; mergedClient.AppCode = client2.AppCode; mergedClient.AudienceGroups = client2.AudienceGroups; } else if (client2.Version == null) { mergedClient.Version = client1.Version; mergedClient.ProductCode = client1.ProductCode ?? client2.ProductCode; mergedClient.AppCode = client1.AppCode; mergedClient.AudienceGroups = client1.AudienceGroups; } else if (client1.Version > client2.Version) { mergedClient.Version = client1.Version; mergedClient.ProductCode = client1.ProductCode ?? client2.ProductCode; mergedClient.AppCode = client1.AppCode; mergedClient.AudienceGroups = client1.AudienceGroups; } else { mergedClient.Version = client2.Version; mergedClient.ProductCode = client2.ProductCode ?? client1.ProductCode; mergedClient.AppCode = client2.AppCode; mergedClient.AudienceGroups = client2.AudienceGroups; } return(mergedClient); }
/// <summary> /// Is the requesting in the audience group? /// </summary> /// <param name="callingClient">GatedClient</param> /// <param name="requiredClient">RequiredClient</param> /// <returns>true if the calling client is in required client audience group, false otherwise</returns> private static bool IsInAudienceGroup(GatedClient callingClient, RequiredClient requiredClient) { if (string.IsNullOrWhiteSpace(requiredClient.AudienceGroup)) { // Return true because AudienceGroup check is not needed return(true); } if (callingClient.AudienceGroups.Count == 0) { return(false); } RequiredApplication requiredApplication; if (callingClient.AppCode != null && requiredClient.Overrides.TryGetValue(callingClient.AppCode, out requiredApplication) && !string.IsNullOrWhiteSpace(requiredApplication.AudienceGroup)) { // App Override if (callingClient.AudienceGroups.Contains(requiredApplication.AudienceGroup, StringComparer.OrdinalIgnoreCase)) { return(true); } return(false); } else { // ClientVersion if (callingClient.AudienceGroups.Contains(requiredClient.AudienceGroup, StringComparer.OrdinalIgnoreCase)) { return(true); } return(false); } }
/// <summary> /// Does the requesting client have access to the gate /// </summary> /// <param name="gate">gate</param> /// <returns>true if the client have access, false otherwise</returns> private bool DoesClientHaveAccess(IGate gate) { bool grantAccess = true; IDictionary <string, RequiredClient> allowedClients = gate.ClientVersions; if (allowedClients != null) { GatedClient client = Request?.CallingClient; RequiredClient requiredClient; if (client == null || !allowedClients.TryGetValue(client.Name, out requiredClient) || !IsVersionInRange(client, requiredClient) || !IsInAudienceGroup(client, requiredClient)) { grantAccess = false; ULSLogging.LogTraceTag(0x2382105b /* tag_967b1 */, Categories.GateSelection, Levels.Verbose, "Not allowing access to gate '{0}' as the calling client '{1}' is not one of the allowed clients '{2}'.", gate.Name ?? "<NULL>", GatedClientAsString(client), AllowedClientsAsString(allowedClients)); } } return(grantAccess); }
/// <summary> /// Is the requesting client version in range? /// </summary> /// <param name="client">GatedClient</param> /// <param name="requiredClient">RequiredClient</param> /// <returns>true if the client version is in required client version range, false otherwise</returns> private static bool IsVersionInRange(GatedClient client, RequiredClient requiredClient) { if (client.Version == null) { return(false); } RequiredApplication requiredApplication; if (client.AppCode != null && requiredClient.Overrides.TryGetValue(client.AppCode, out requiredApplication)) { if (requiredApplication.VersionRanges.Count > 0) { // VersionRanges of application foreach (ProductVersionRange range in requiredApplication.VersionRanges) { if (range.Min <= client.Version && range.Max >= client.Version) { return(true); } } return(false); } // Between MinVersion and MaxVersion of application if ((requiredApplication.MinVersion != null && requiredApplication.MinVersion > client.Version) || (requiredApplication.MaxVersion != null && requiredApplication.MaxVersion <= client.Version)) { return(false); } return(true); } else { if (requiredClient.VersionRanges.Count > 0) { // VersionRanges of platform foreach (ProductVersionRange range in requiredClient.VersionRanges) { if (range.Min <= client.Version && range.Max >= client.Version) { return(true); } } return(false); } // Between MinVersion and MaxVersion of platform if ((requiredClient.MinVersion != null && requiredClient.MinVersion > client.Version) || (requiredClient.MaxVersion != null && requiredClient.MaxVersion <= client.Version)) { return(false); } return(true); } }