📄 managepermissions.aspx
字号:
<%@ Page masterPageFile="~/WebAdminWithConfirmation.master" inherits="System.Web.Administration.SecurityPage"%>
<%@ MasterType virtualPath="~/WebAdminWithConfirmation.master" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Web.Administration" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Web.Hosting" %>
<script runat="server" language="cs">
private const string SELECTED_RULE = "WebAdminSelectedRule";
private const string RULES = "WebAdminRules";
private const string NO_DELETE_RULES = "WebAdminNoDeleteRules";
private const string PARENT_RULE_COUNT = "WebAdminParentRuleCount";
private const string SELECTED_ITEM = "WebAdminSelectedItem";
private int ParentRuleCount {
get {
object obj = Session[PARENT_RULE_COUNT];
return obj != null ? (int) obj : -1;
}
set {
Session[PARENT_RULE_COUNT] = value;
}
}
private ArrayList NotDeleteableRules {
get {
object obj = (ArrayList)Session[NO_DELETE_RULES];
if (obj != null) {
return (ArrayList)Session[NO_DELETE_RULES];
} else {
return (new ArrayList());
}
}
set {
Session[NO_DELETE_RULES] = value;
}
}
private ArrayList Rules {
get {
object obj = (ArrayList)Session[RULES];
if (obj != null) {
return (ArrayList)Session[RULES];
} else {
return (new ArrayList());
}
}
set {
Session[RULES] = value;
}
}
private int SelectedRule {
get {
object obj = Session[SELECTED_RULE];
return obj != null ? (int) obj : -1;
}
set {
Session[SELECTED_RULE] = value;
}
}
private void GetNotDeletableRules(Configuration config) {
AuthorizationSection notDeleteableAuth = (AuthorizationSection) config.GetSection("system.web/authorization");
ArrayList arrNoDelete = new ArrayList();
PropertyInformation propUsers = null;
PropertyInformation propRoles = null;
bool entryIsDeletable = false;
int i = 0;
foreach (AuthorizationRule entryKeep in notDeleteableAuth.Rules) {
entryIsDeletable = true;
propUsers = entryKeep.ElementInformation.Properties["users"];
propRoles = entryKeep.ElementInformation.Properties["roles"];
if (propUsers != null) {
if (propUsers.ValueOrigin == PropertyValueOrigin.Inherited) {
entryIsDeletable = false;
}
}
if (propRoles != null && entryIsDeletable) {
if (propRoles.ValueOrigin == PropertyValueOrigin.Inherited) {
entryIsDeletable = false;
}
}
if (!entryIsDeletable) {
// store the index in here as to which one is not deletable
arrNoDelete.Add(i);
}
i++;
}
Session[NO_DELETE_RULES] = arrNoDelete;
}
private string GetToolTip(string resourceName, string itemName) {
string tempString = (string) GetLocalResourceObject(resourceName);
return String.Format((string)GetGlobalResourceObject("GlobalResources","ToolTipFormat"), tempString, itemName);
}
private void BindGrid() {
string appPath = CurrentPath;
string parentPath = GetParentPath(appPath);
Configuration config = OpenWebConfiguration(appPath, true);
AuthorizationSection auth = (AuthorizationSection) config.GetSection("system.web/authorization");
Configuration parentConfig = OpenWebConfiguration(parentPath, true);
AuthorizationSection parentAuth = (AuthorizationSection) parentConfig.GetSection("system.web/authorization");
ParentRuleCount = parentAuth.Rules.Count;
GetNotDeletableRules(config);
ArrayList arr = new ArrayList();
foreach (AuthorizationRule entry in auth.Rules) {
arr.Add(entry);
}
Session[RULES] = arr;
dataGrid.DataSource = arr;
dataGrid.DataBind();
if (dataGrid.SelectedRow != null) {
UpdateRowColors(dataGrid, dataGrid.Rows[dataGrid.SelectedRow.RowIndex]);
}
}
private void DeleteRule(object sender, EventArgs e) {
LinkButton button = (LinkButton) sender;
GridViewRow item = (GridViewRow) button.Parent.Parent;
AuthorizationRule rule = (AuthorizationRule)Rules[item.RowIndex];
StringBuilder builder = new StringBuilder();
builder.Append(rule.Action);
int i = 0;
foreach (string u in rule.Users) {
if (i > 0) {
builder.Append(", " + u);
}
else {
builder.Append(" " + u);
}
i++;
}
i = 0;
foreach (string r in rule.Roles) {
if (i > 0) {
builder.Append(", " + r);
}
else {
builder.Append(" " + r);
}
i++;
}
RuleDescription.Text = builder.ToString();
Master.SetDisplayUI(true);
Session["ItemIndex"] = item.RowIndex;
}
private void Yes_Click(object sender, EventArgs e) {
Rules.RemoveAt((int)Session["ItemIndex"]);
UpdateRules();
dataGrid.SelectedIndex = -1;
BindGrid();
Master.SetDisplayUI(false);
}
private void No_Click(object sender, EventArgs e) {
Master.SetDisplayUI(false);
}
private string GetRoles(object val, bool appendImg) {
StringBuilder builder = new StringBuilder();
AuthorizationRule rule = (AuthorizationRule)val;
if (rule.Roles.Count == 0) {
return String.Empty;
}
for(int i = 0; i < rule.Roles.Count; i++) {
if (i > 0) {
builder.Append(", ");
}
string role = rule.Roles[i];
if (role == "*") {
role = (string)GetLocalResourceObject("BracketAll");
}
builder.Append(role);
}
if (appendImg) {
StringBuilder builder2 = new StringBuilder();
builder2.Append("<img src=\"../../Images/image2.gif\" alt=\"" + (string)GetGlobalResourceObject("GlobalResources", "RoleGif") + " [" + builder.ToString() + "]" + "\"/> ");
builder2.Append(builder.ToString());
return builder2.ToString();
} else {
return builder.ToString();
}
}
private string GetUsers(object val, bool appendImg) {
StringBuilder builder = new StringBuilder();
AuthorizationRule rule = (AuthorizationRule)val;
if (rule.Users.Count == 0) {
return String.Empty;
}
for(int i = 0; i < rule.Users.Count; i++) {
if (i > 0) {
builder.Append(", ");
}
string user = rule.Users[i];
if (user == "?") {
user = (string)GetLocalResourceObject("BracketAnonymous");
}
else if (user == "*") {
user = (string)GetLocalResourceObject("BracketAll");
}
builder.Append(user);
}
if (appendImg) {
StringBuilder builder2 = new StringBuilder();
builder2.Append("<img src=\"../../Images/image1.gif\" alt=\"" + (string)GetGlobalResourceObject("GlobalResources", "UserGif") + " [" + builder.ToString() + "]" + "\"/> ");
builder2.Append(builder.ToString());
return builder2.ToString();
} else {
return builder.ToString();
}
}
private string GetAction(object val) {
AuthorizationRule rule = (AuthorizationRule)val;
string ruleAction = "";
if (rule.Action == AuthorizationRuleAction.Allow) {
ruleAction = (string)GetLocalResourceObject("Allow");
} else if (rule.Action == AuthorizationRuleAction.Deny) {
ruleAction = (string)GetLocalResourceObject("Deny");
}
return ruleAction;
}
private string GetUsersAndRoles(object val, bool appendImg) {
return GetUsers(val, appendImg) + GetRoles(val, appendImg);
}
private bool IsEntryDeleteable(int rowIndex) {
bool entryIsDeleteable = false;
if (rowIndex < Rules.Count - ParentRuleCount) {
entryIsDeleteable = true;
}
if (!entryIsDeleteable) {
return entryIsDeleteable;
}
foreach (int index1 in NotDeleteableRules) {
if (index1 == rowIndex) {
entryIsDeleteable = false;
}
}
return entryIsDeleteable;
}
private bool IsIE() {
HttpBrowserCapabilities caps = Page.Request.Browser;
bool isIE = (caps.Type.IndexOf("IE") > -1);
return isIE;
}
private void ItemDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= Rules.Count - ParentRuleCount) {
return;
}
DataControlRowType itemType = e.Row.RowType;
if ((itemType == DataControlRowType.Pager) ||
(itemType == DataControlRowType.Header) ||
(itemType == DataControlRowType.Footer))
{
return;
}
if (IsIE()) {
if (IsEntryDeleteable(e.Row.RowIndex)) {
// if netscape, then selecting the the row
// will not allow the DeleteRule to fire
foreach(Control c in e.Row.Cells[0].Controls) {
LinkButton button = c as LinkButton;
if (button == null) {
continue;
}
e.Row.Attributes["onclick"] = Page.GetPostBackClientHyperlink(button, "");
}
}
}
}
private void MoveRuleDown(object sender, EventArgs e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -