Exemplo n.º 1
0
        /// <summary> Populates this IP_Restriction range information with all of the IP Restriction ranges for this library </summary>
        /// <param name="All_Ranges"> DataTable containing all of the IP ranges </param>
        public void Populate_IP_Ranges(DataTable All_Ranges)
        {
            DataColumn startColumn = All_Ranges.Columns["StartIP"];
            DataColumn endColumn   = All_Ranges.Columns["EndIP"];

            ipRanges.Clear();
            IP_Restriction_Range currentRange = null;

            foreach (DataRow thisRow in All_Ranges.Rows)
            {
                // Check if this is the same range, or add a new one
                int ipRangeId = Convert.ToInt32(thisRow[1]);
                if ((currentRange == null) || (currentRange.RangeID != ipRangeId))
                {
                    currentRange = new IP_Restriction_Range(ipRangeId, thisRow[0].ToString(), thisRow[2].ToString());
                    ipRanges.Add(currentRange);
                }

                // Add all the IP addresses to this
                string start = thisRow[startColumn].ToString().Trim();
                if (start.Length > 0)
                {
                    currentRange.Add_IP_Address(start, thisRow[endColumn].ToString().Trim());
                }
            }
        }
        /// <summary> Populates this IP_Restriction range information with all of the IP Restriction ranges for this library </summary>
        /// <param name="All_Ranges"> DataTable containing all of the IP ranges </param>
        public void Populate_IP_Ranges(DataTable All_Ranges)
        {
            DataColumn startColumn = All_Ranges.Columns["StartIP"];
            DataColumn endColumn = All_Ranges.Columns["EndIP"];

            ipRanges.Clear();
            IP_Restriction_Range currentRange = null;
            foreach (DataRow thisRow in All_Ranges.Rows)
            {
                // Check if this is the same range, or add a new one
                int ipRangeId = Convert.ToInt32(thisRow[1]);
                if ((currentRange == null) || (currentRange.RangeID != ipRangeId))
                {
                    currentRange = new IP_Restriction_Range(ipRangeId, thisRow[0].ToString(), thisRow[2].ToString());
                    ipRanges.Add(currentRange);
                }

                // Add all the IP addresses to this
                string start = thisRow[startColumn].ToString().Trim();
                if (start.Length > 0)
                {
                    currentRange.Add_IP_Address(start, thisRow[endColumn].ToString().Trim());
                }
            }
        }
        /// <summary> Constructor for a new instance of the IP_Restrictions_AdminViewer class </summary>
        /// <param name="User"> Authenticated user information </param>
        /// <param name="CurrentMode"> Mode / navigation information for the current request</param>
        /// <param name="IP_Restrictions"> List of all IP restrictions ranges used in this digital library to restrict access to certain digital resources </param>
        /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <remarks> Postback from handling an edit or new item aggregation alias is handled here in the constructor </remarks>
        public IP_Restrictions_AdminViewer( User_Object User, SobekCM_Navigation_Object CurrentMode, IP_Restriction_Ranges IP_Restrictions, Custom_Tracer Tracer )
            : base(User)
        {
            Tracer.Add_Trace("IP_Restrictions_AdminViewer.Constructor", String.Empty);

            ipRestrictionInfo = IP_Restrictions;
            currentMode = CurrentMode;

            // Ensure the user is the system admin
            if ((User == null) || ((!User.Is_System_Admin) && ( !User.Is_Portal_Admin )))
            {
                currentMode.Mode = Display_Mode_Enum.My_Sobek;
                currentMode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                currentMode.Redirect();
                return;
            }

            // Determine if there is an specific IP address range for editing
            index = -1;
            if (currentMode.My_Sobek_SubMode.Length > 0)
            {
                if ( !Int32.TryParse(currentMode.My_Sobek_SubMode, out index ))
                    index = -1;
            }

            // If there was an index included, try to pull the information about it
            thisRange = null;
            details = null;
            if ((index >= 1) && (index <= ipRestrictionInfo.Count))
            {
                thisRange = ipRestrictionInfo[index - 1];
                if (thisRange != null)
                {
                    details = SobekCM_Database.Get_IP_Restriction_Range_Details(thisRange.RangeID, Tracer);
                }
            }

            if ((currentMode.isPostBack) && ( user.Is_System_Admin ))
            {
                // Get a reference to this form
                NameValueCollection form = HttpContext.Current.Request.Form;

                string action = form["action"].Trim();

                if (action == "new")
                {
                    // Pull the main values
                    string title = form["new_admin_title"].Trim();
                    string notes = form["new_admin_notes"].Trim();
                    string message = form["new_admin_message"].Trim();

                    if ((title.Length == 0) || (message.Length == 0))
                    {
                        actionMessage = "Both title and message are required fields";
                    }
                    else
                    {
                        if ( SobekCM_Database.Edit_IP_Range(-1, title, notes, message, Tracer))
                            actionMessage = "Saved new IP range '" + title + "'";
                        else
                            actionMessage = "Error saving new IP range '" + title + "'";
                    }
                }
                else if (( details != null ) && ( thisRange != null ))
                {
                    try
                    {
                        // Pull the main values
                        string title = form["admin_title"].Trim();
                        string notes = form["admin_notes"].Trim();
                        string message = form["admin_message"].Trim();

                        if (title.Length == 0)
                        {
                            title = thisRange.Title;
                        }

                        // Edit the main values in the database
                        SobekCM_Database.Edit_IP_Range(thisRange.RangeID, title, notes, message, Tracer);
                        thisRange.Title = title;
                        thisRange.Notes = notes;
                        thisRange.Item_Restricted_Statement = message;

                        // Now check each individual IP address range
                        string[] getKeys = form.AllKeys;
                        int single_ip_index = 0;
                        foreach (string thisKey in getKeys)
                        {
                            // Is this for a new ip address?
                            if (thisKey.IndexOf("admin_ipstart_") == 0)
                            {
                                // Get the basic information for this single ip address
                                string ip_index = thisKey.Replace("admin_ipstart_", "");
                                string thisIpStart = form["admin_ipstart_" + ip_index].Trim();
                                string thisIpEnd = form["admin_ipend_" + ip_index].Trim();
                                string thisIpNote = form["admin_iplabel_" + ip_index].Trim();

                                // Does this match an existing IP range?
                                if ((ip_index.IndexOf("new") < 0) && (single_ip_index < details.Tables[1].Rows.Count))
                                {
                                    // Get the pre-existing IP row
                                    DataRow ipRow = details.Tables[1].Rows[single_ip_index];
                                    int singleIpId = Convert.ToInt32(ipRow[0]);
                                    if (thisIpStart.Length == 0)
                                    {
                                        SobekCM_Database.Delete_Single_IP(singleIpId, Tracer);
                                    }
                                    else
                                    {
                                        // Is this the same?
                                        if ((thisIpStart != ipRow[1].ToString().Trim()) || (thisIpEnd != ipRow[2].ToString().Trim()) || (thisIpNote != ipRow[3].ToString().Trim()))
                                        {
                                            int edit_point_count = thisIpStart.Count(ThisChar => ThisChar == '.');

                                            if (edit_point_count == 3)
                                            {
                                                SobekCM_Database.Edit_Single_IP(singleIpId, thisRange.RangeID, thisIpStart, thisIpEnd, thisIpNote, Tracer);
                                            }
                                        }
                                    }

                                    // Be ready to look at the next pre-existing IP range
                                    single_ip_index++;
                                }
                                else
                                {
                                    // Just add this as a new single ip address
                                    if (thisIpStart.Length > 0)
                                    {
                                        int add_point_count = thisIpStart.Count(ThisChar => ThisChar == '.');

                                        if (add_point_count == 3)
                                        {
                                            SobekCM_Database.Edit_Single_IP(-1, thisRange.RangeID, thisIpStart, thisIpEnd, thisIpNote, Tracer);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        actionMessage = "Error saving IP range";
                    }
                }

                // Repopulate the restriction table
                DataTable ipRestrictionTbl = SobekCM_Database.Get_IP_Restriction_Ranges(Tracer);
                if (ipRestrictionTbl != null)
                {
                    IP_Restrictions.Populate_IP_Ranges(ipRestrictionTbl);
                }

                // Forward back to the main form
                if (String.IsNullOrEmpty(actionMessage))
                {
                    currentMode.My_Sobek_SubMode = String.Empty;
                    currentMode.Redirect();
                }
            }
        }