public void IPAddressNotInRangeTest()
        {
            string         ip    = "127.10.0.0/28";
            IPAddressRange range = IPAddressRange.FromCidrAddress(ip);

            IPAddress.TryParse("192.168.0.1", out IPAddress candidate);

            Assert.IsFalse(range.IPInRange(candidate));
        }
Example #2
0
        public Toggle GetFlag(string name, ToggleData userData)
        {
            if (!toggles.ContainsKey(name))
            {
                return(Toggle.Empty);
            }

            ToggleElement element = toggles[name];

            if (!string.IsNullOrWhiteSpace(userData.UserRoles))
            {
                List <string> roles = element.roles.Select(x => x.role.name).ToList();
                bool          found = false;
                foreach (string role in userData.UserRoles.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (roles.Contains(role))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(new Toggle(name, false));
                }
            }

            if (!string.IsNullOrWhiteSpace(userData.UserId))
            {
                List <string> users = element.users.Select(x => x.user.name).ToList();

                if (!users.Contains(userData.UserId))
                {
                    return(new Toggle(name, false));
                }
            }

            if (!string.IsNullOrWhiteSpace(userData.IpAddress))
            {
                if (!IPAddress.TryParse(userData.IpAddress, out IPAddress candidate))
                {
                    return(new Toggle(name, false));
                }

                List <string> addresses = element.ipaddresses.Select(x => x.ipaddress.value).ToList();
                bool          found     = false;
                foreach (string address in addresses)
                {
                    if (string.IsNullOrWhiteSpace(address))
                    {
                        continue;
                    }

                    IPAddressRange range = IPAddressRange.FromCidrAddress(address);
                    if (range == IPAddressRange.Empty)
                    {
                        continue;
                    }

                    if (range.IPInRange(candidate))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(new Toggle(name, false));
                }
            }

            return(new Toggle(name, true));
        }