#region Copyright
//
// DotNetNukeŽ - http://www.dotnetnuke.com
// Copyright (c) 2002-2011
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
#region Usings
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.HttpModules.RequestFilter;
using DotNetNuke.Instrumentation;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
#endregion
namespace DotNetNuke.Modules.Admin.Host
{
///
/// The FriendlyUrls PortalModuleBase is used to edit the friendly urls
/// for the application.
///
///
///
///
/// [cnurse] 07/06/2006 Created
///
public partial class RequestFilters : PortalModuleBase
{
#region "Private Fields"
private List _Rules;
#endregion
#region "Private Properties"
/// -----------------------------------------------------------------------------
///
/// Gets and sets the mode of the control
///
///
///
/// The mode is used to determine when the user is creating a new rule
/// and allows the system to know to remove "blank" rules if the user cancels
/// the edit.
///
///
/// [jbrinkman] 5/28/2007 Created
///
/// -----------------------------------------------------------------------------
private bool AddMode
{
get
{
bool _Mode = Null.NullBoolean;
if (ViewState["Mode"] != null)
{
_Mode = Convert.ToBoolean(ViewState["Mode"]);
}
return _Mode;
}
set
{
ViewState["Mode"] = value;
}
}
/// -----------------------------------------------------------------------------
///
/// Reads and writes to the list of Request Filter rules
///
///
/// Generic List(Of RequestFilterRule)
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
/// -----------------------------------------------------------------------------
private List Rules
{
get
{
if (_Rules == null)
{
_Rules = RequestFilterSettings.GetSettings().Rules;
}
return _Rules;
}
set
{
_Rules = value;
}
}
#endregion
#region "Private Methods"
///
/// Adds a confirmation dialog to the delete button.
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
private static void AddConfirmActiontoDeleteButton(DataListItemEventArgs e)
{
var cmdDelete = (ImageButton) e.Item.FindControl("cmdDelete");
ClientAPI.AddButtonConfirm(cmdDelete, Localization.GetString("DeleteItem"));
}
///
/// Binds the selected values of the Request Filter Rule dropdown lists.
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
private void BindDropDownValues(DataListItemEventArgs e)
{
var rule = (RequestFilterRule) e.Item.DataItem;
var ddlOperator = (DropDownList) e.Item.FindControl("ddlOperator");
if (ddlOperator != null && rule != null)
{
ddlOperator.SelectedValue = rule.Operator.ToString();
}
var ddlAction = (DropDownList) e.Item.FindControl("ddlAction");
if (ddlAction != null && rule != null)
{
ddlAction.SelectedValue = rule.Action.ToString();
}
}
///
/// BindRules updates the datalist with the values of the current list of rules.
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
private void BindRules()
{
rptRules.DataSource = Rules;
rptRules.DataBind();
}
#endregion
#region "Protected methods"
///
/// Retrieves the list of rules from the viewstate rather than constantly
/// re-reading the configuration file.
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected override void LoadViewState(object savedState)
{
var myState = (object[]) savedState;
if ((myState[0] != null))
{
base.LoadViewState(myState[0]);
}
if ((myState[1] != null))
{
var configRules = new List();
//Deserialize into RewriterConfiguration
#pragma warning disable 612,618
configRules = (List) XmlUtils.Deserialize(Convert.ToString(myState[1]), configRules.GetType());
#pragma warning restore 612,618
Rules = configRules;
}
}
///
/// Saves the rules to the viewstate to avoid constantly re-reading
/// the configuration file.
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected override object SaveViewState()
{
var configRules = new List();
configRules = Rules;
object baseState = base.SaveViewState();
var allStates = new object[2];
allStates[0] = baseState;
allStates[1] = XmlUtils.Serialize(configRules);
return allStates;
}
#endregion
#region "Event Handlers"
///
/// Page_Load runs when the control is loaded.
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
cmdAddRule.Click += AddRule;
rptRules.ItemDataBound += rptRules_ItemDataBound;
rptRules.EditCommand += EditRule;
rptRules.DeleteCommand += DeleteRule;
rptRules.UpdateCommand += SaveRule;
rptRules.CancelCommand += CancelEdit;
//Bind the rules (as long as not postback)
if (!Page.IsPostBack)
{
BindRules();
}
}
/// -----------------------------------------------------------------------------
///
/// AddRule runs when the Add button is clicked
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
/// -----------------------------------------------------------------------------
protected void AddRule(object sender, EventArgs e)
{
//Add a new empty rule and set the editrow to the new row
Rules.Add(new RequestFilterRule());
rptRules.EditItemIndex = Rules.Count - 1;
//Set the AddMode to true
AddMode = true;
//Rebind the collection
BindRules();
}
/// -----------------------------------------------------------------------------
///
/// DeleteRule runs when the Delete button for a specified rule is clicked
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
/// -----------------------------------------------------------------------------
protected void DeleteRule(object source, DataListCommandEventArgs e)
{
//Get the index of the row to delete
int index = e.Item.ItemIndex;
//Remove the rule from the rules collection
Rules.RemoveAt(index);
try
{
//Save the new collection
RequestFilterSettings.Save(Rules);
}
catch (UnauthorizedAccessException exc)
{
DnnLog.Debug(exc);
lblErr.InnerText = Localization.GetString("unauthorized", LocalResourceFile);
lblErr.Visible = true;
//This forces the system to reload the settings from DotNetNuke.Config
//since we have already deleted the entry from the Rules list.
Rules = null;
}
//Rebind the collection
BindRules();
}
///
/// EditRule runs when the Edit button is clicked.
///
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected void EditRule(object source, DataListCommandEventArgs e)
{
lblErr.Visible = true;
//Set the AddMode to false
AddMode = false;
//Set the editrow
rptRules.EditItemIndex = e.Item.ItemIndex;
//Rebind the collection
BindRules();
}
///
/// SaveRule runs when the Save button is clicked.
///
///
///
///
/// The Save button is displayed for a specific request filter rule
/// when the user enters the edit mode.
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected void SaveRule(object source, DataListCommandEventArgs e)
{
//Get the index of the row to save
int index = rptRules.EditItemIndex;
RequestFilterRule rule = Rules[index];
var txtServerVar = (TextBox) e.Item.FindControl("txtServerVar");
var txtValue = (TextBox) e.Item.FindControl("txtValue");
var txtLocation = (TextBox) e.Item.FindControl("txtLocation");
var ddlOperator = (DropDownList) e.Item.FindControl("ddlOperator");
var ddlAction = (DropDownList) e.Item.FindControl("ddlAction");
if (!String.IsNullOrEmpty(txtServerVar.Text) && !String.IsNullOrEmpty(txtValue.Text))
{
rule.ServerVariable = txtServerVar.Text;
rule.Location = txtLocation.Text;
rule.Operator = (RequestFilterOperatorType) Enum.Parse(typeof (RequestFilterOperatorType), ddlOperator.SelectedValue);
rule.Action = (RequestFilterRuleType) Enum.Parse(typeof (RequestFilterRuleType), ddlAction.SelectedValue);
//A rule value may be a semicolon delimited list of values. So we need to use a helper function to
//parse the list. If this is a regex, then only one value is supported.
rule.SetValues(txtValue.Text, rule.Operator);
//Save the modified collection
RequestFilterSettings.Save(Rules);
}
else
{
if (AddMode)
{
//Remove the temporary added row
Rules.RemoveAt(Rules.Count - 1);
}
}
AddMode = false;
//Reset Edit Index
rptRules.EditItemIndex = -1;
BindRules();
}
///
/// CancelEdit runs when the Cancel button is clicked.
///
///
///
///
/// The Cancel button is displayed for a specific request filter rule
/// when the user enters the edit mode. Clicking the cancel button will
/// return the user to normal view mode with saving any of their changes
/// to the specific Request Filter Rule.
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected void CancelEdit(object source, DataListCommandEventArgs e)
{
if (AddMode)
{
//Remove the temporary added row
Rules.RemoveAt(Rules.Count - 1);
AddMode = false;
}
//Clear editrow
rptRules.EditItemIndex = -1;
//Rebind the collection
BindRules();
}
///
/// The ItemDataBound event is used to set the value of the Operator and Action
/// dropdownlists based on the current values for the specific Request Filter Rule.
///
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected void rptRules_ItemDataBound(object sender, DataListItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListItemType.AlternatingItem:
AddConfirmActiontoDeleteButton(e);
break;
case ListItemType.Item:
AddConfirmActiontoDeleteButton(e);
break;
case ListItemType.EditItem:
BindDropDownValues(e);
break;
}
}
///
/// The PreRender event is used to disable the "Add Rule" button when the user is in edit mode
///
///
///
///
///
/// [jbrinkman] 5/28/2007 Created
///
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//If the user is editing a rule, then disable the "Add Rule" button
cmdAddRule.Enabled = rptRules.EditItemIndex == -1;
}
#endregion
}
}