Beispiel #1
0
        public bool Overlaps(TimeOfWeek other)
        {
            //neither is -1 and the two aren't equal, so there can be no overlap
            if (this.DayOfWeek != -1 &&
                other.DayOfWeek != -1 &&
                this.DayOfWeek != other.DayOfWeek)
            {
                return(false);
            }

            //days of week overlap, so lets check for timeoverlap now
            if ((this.StartMins <= other.StartMins && this.EndMins >= other.StartMins) ||
                (other.StartMins <= this.StartMins && other.EndMins >= this.EndMins))
            {
                return(true);
            }

            return(false);
        }
Beispiel #2
0
        public ObservableCollection<AccessRule> GetRules(string userOrGroup, string app, string device, string location, AccessMode accessType, 
                                                int startTime, int endTime, int day)
        {
            var retRules = new ObservableCollection<AccessRule>();

            foreach (AccessRule rule in allPolicies)
            {
                //check user group membership
                UserGroupInfo queryGroup = allGroups[userOrGroup];
                UserGroupInfo ruleGroup = allGroups[rule.UserGroup];

                if (!queryGroup.Equals(ruleGroup) && 
                    !queryGroup.IsDescendant(ruleGroup) &&
                    !queryGroup.IsAncestor(ruleGroup))
                    continue;


                //check if the app matches
                if (!app.Equals("All") && 
                    !app.Equals(rule.ModuleName))
                    continue;

                //check if device is in the list membership
                if (!device.Equals("") &&
                    !rule.DeviceList.Contains(device))
                    continue;

                //check for location matches
                if (device.Equals("") && !location.Equals(""))
                {
                    bool locationMatches = false;
                    Location queryLocation = allLocations[location];

                    foreach (string deviceInRule in rule.DeviceList)
                    {
                        VPortInfo portInfo = configuredPortNames[deviceInRule];

                        if (queryLocation.ContainsPort(portInfo))
                            locationMatches = true;
                    }

                    if (!locationMatches)
                        continue;
                }

                //check for access type matches
                if (accessType != AccessMode.All &&
                    accessType != rule.AccessMode)
                    continue;

                //check for day of week matches
                TimeOfWeek queryTimeOfWeek = new TimeOfWeek(day, startTime, endTime);

                bool dayOfWeekMatches = false;

                foreach (TimeOfWeek ruleTimeOfWeek in rule.TimeList) 
                {
                    if (ruleTimeOfWeek.Overlaps(queryTimeOfWeek))
                    {
                        dayOfWeekMatches = true;
                        break;
                    }
                }

                if (!dayOfWeekMatches)
                    continue;

                retRules.Add(rule);
            }

            return retRules;
        }
        public bool Overlaps(TimeOfWeek other)
        {
            //neither is -1 and the two aren't equal, so there can be no overlap
            if (this.DayOfWeek != -1 &&
                other.DayOfWeek != -1 &&
                this.DayOfWeek != other.DayOfWeek)
            {
                return false;
            }

            //days of week overlap, so lets check for timeoverlap now
            if ((this.StartMins <= other.StartMins && this.EndMins >= other.StartMins) ||
                 (other.StartMins <= this.StartMins && other.EndMins >= this.EndMins))
                return true;

            return false;
        }
Beispiel #4
0
        private void ReadAccessRules()
        {
            string fileName = this.RulesFile;

            XmlDocument xmlDoc = new XmlDocument();

            XmlReader xmlReader = XmlReader.Create(fileName, xmlReaderSettings);
            xmlDoc.Load(xmlReader);

            XmlElement root = xmlDoc.FirstChild as XmlElement;

            if (!root.Name.Equals("Rules"))
                throw new Exception("rules file " + fileName + " does not begin with <Rules>");

            foreach (XmlElement xmlRule in root.ChildNodes)
            {
                if (!xmlRule.Name.Equals("Rule"))
                    throw new Exception("expected Rule. Got " + xmlRule.Name);

                foreach (XmlElement xmlUser in xmlRule.ChildNodes)
                {
                    if (!xmlUser.Name.Equals("User") && !xmlUser.Name.Equals("Group"))
                        throw new Exception("expected User. Got " + xmlUser.Name);

                    AccessRule accessRule = new AccessRule();

                    accessRule.RuleName = xmlRule.GetAttribute("Name");
                    
                    accessRule.ModuleName = xmlRule.GetAttribute("Module");

                    if (!allModules.ContainsKey(accessRule.ModuleName)
                        && !accessRule.ModuleName.Equals(Constants.GuiServiceSuffixWeb)
                        && !accessRule.ModuleName.Equals(Constants.GuiServiceSuffixWebSec)
                        && !accessRule.ModuleName.Equals(Constants.ScoutsSuffixWeb)
                        )
                        throw new Exception("unknown module in rules: " + accessRule.ModuleName);

                    accessRule.UserGroup = xmlUser.GetAttribute("Name").ToLower();
                    if (!allGroups.ContainsKey(accessRule.UserGroup))
                        throw new Exception("unknown user/group in rules: " + accessRule.UserGroup);

                    accessRule.AccessMode = (AccessMode)Enum.Parse(typeof(AccessMode), xmlUser.GetAttribute("Type"), true);

                    List<string> deviceList = new List<string>();
                    List<TimeOfWeek> timeList = new List<TimeOfWeek>();

                    foreach (XmlElement xmlChild in xmlUser.ChildNodes)
                    {
                        switch (xmlChild.Name)
                        {
                            case "Service":
                                {
                                    //it is a device
                                    string serviceName = xmlChild.GetAttribute("FriendlyName");

                                    if (!configuredPortNames.ContainsKey(serviceName)&& !serviceName.Equals("*") )
                                        throw new Exception("unknown service name in rules: " + serviceName);

                                    deviceList.Add(serviceName);
                                }
                                break;
                            case "Time":
                                {
                                    //it is time
                                    int dayOfWeek = int.Parse(xmlChild.GetAttribute("DayOfWeek"));

                                    string startMins = xmlChild.GetAttribute("StartMins");
                                    string endMins = xmlChild.GetAttribute("EndMins");

                                    int startMinsInt = (startMins.Equals("")) ? 0 : int.Parse(startMins);
                                    int endMinsInt = (endMins.Equals("")) ? 2400 : int.Parse(endMins);

                                    TimeOfWeek timeOfWeek = new TimeOfWeek(dayOfWeek, startMinsInt, endMinsInt);
                                    if (!timeOfWeek.Valid())
                                        throw new Exception("invalid time spec for rule " + accessRule.RuleName);

                                    timeList.Add(timeOfWeek);
                                }
                                break;
                            default:
                                throw new Exception("expected Device or Time. Got " + xmlChild.Name);
                        }
                    }

                    //assume always if no time was specified
                    if (timeList.Count == 0)
                        timeList.Add(new TimeOfWeek(-1, 0, 2400));

                    // assume access-rule applies to all ports of the module if no service specified
                    if(deviceList.Count==0)
                        deviceList.Add("*");

                    accessRule.DeviceList = deviceList;
                    accessRule.TimeList = timeList;

                    accessRule.Priority = 0;

                    AddAccessRule(accessRule, false);
                }
            }

            xmlReader.Close();
        }