📄 securityroles.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MyStarterKit.Portal.Web
{
/// <summary>
/// SecurityRoles 的摘要说明。
/// </summary>
public class SecurityRoles : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Message;
protected System.Web.UI.WebControls.TextBox windowsUserName;
protected System.Web.UI.WebControls.LinkButton addNew;
protected System.Web.UI.WebControls.DropDownList allUsers;
protected System.Web.UI.WebControls.LinkButton addExisting;
protected System.Web.UI.WebControls.DataList usersInRole;
protected System.Web.UI.WebControls.LinkButton saveBtn;
protected System.Web.UI.HtmlControls.HtmlGenericControl title;
int roleId = -1;
String roleName = "";
private void Page_Load(object sender, System.EventArgs e)
{
// 用户角色为Admins的才能访问该管理模块,否则重定向到EditAccessDenied.aspx
if (PortalSecurity.IsInRoles("Admins") == false)
{
Response.Redirect("~/Admin/EditAccessDenied.aspx");
}
// 角色Id
if (Request.Params["roleid"] != null)
{
roleId = Int32.Parse(Request.Params["roleid"]);
}
// 角色名称
if (Request.Params["rolename"] != null)
{
roleName = (String)Request.Params["rolename"];
}
if (!Page.IsPostBack)
{
BindData();
}
}
/// <summary>
/// 保存修改
/// </summary>
/// <param name="Sender"></param>
/// <param name="e"></param>
private void Save_Click(Object Sender, EventArgs e)
{
// 获取管理标签的索引adminIndex,管理标签默认为最后一个
PortalSettings portalSettings = (PortalSettings) Context.Items["PortalSettings"];
int adminIndex = portalSettings.DesktopTabs.Count-1;
// 返回管理页
Response.Redirect("~/DesktopDefault.aspx?tabindex=" + adminIndex.ToString() + "&tabid=" + ((TabStripDetails)portalSettings.DesktopTabs[adminIndex]).TabId);
}
/// <summary>
/// 添加用户到,该角色的成员中去
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddUser_Click(Object sender, EventArgs e)
{
// 用户id
int userId;
if (((LinkButton)sender).ID == "addNew")
{
// 添加新用户(用户名和Email都是windowsUserName.Text,密码是"acme")
UsersDB users = new UsersDB();
if ((userId = users.AddUser(windowsUserName.Text, windowsUserName.Text, "acme")) == -1)
{
Message.Text = "添加失败!用户 <" + "u" + ">" + windowsUserName.Text + "<" + "/u" + "> 已经存在。" + "<" + "br" + ">" + "请重新换一个。";
}
}
else
{
// 获取选中用户的Id
userId = Int32.Parse(allUsers.SelectedItem.Value);
}
if (userId != -1)
{
// 添加到角色成员中
RolesDB roles = new RolesDB();
roles.AddUserRole(roleId, userId);
}
// Rebind list
BindData();
}
/// <summary>
/// 当用户列表有按钮事件触发时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void usersInRole_ItemCommand(object sender, DataListCommandEventArgs e)
{
RolesDB roles = new RolesDB();
int userId = (int) usersInRole.DataKeys[e.Item.ItemIndex];
// 将指定用户重该角色中删除
if (e.CommandName == "delete")
{
// update database
roles.DeleteUserRole(roleId, userId);
// Ensure that item is not editable
usersInRole.EditItemIndex = -1;
// Repopulate list
BindData();
}
}
/// <summary>
/// 初始化页面控件
/// </summary>
private void BindData()
{
// 当身份验证类型不为Forms时,显示windowsUserName
if (User.Identity.AuthenticationType != "Forms")
{
windowsUserName.Visible = true;
addNew.Visible = true;
}
// add the role name to the title
if (roleName != "")
{
title.InnerText = "角色成员:" + roleName;
}
// 从数据库中读取角色信息
RolesDB roles = new RolesDB();
// 绑定角色列表
usersInRole.DataSource = roles.GetRoleMembers(roleId);
usersInRole.DataBind();
// 绑定角色用户列表
allUsers.DataSource = roles.GetUsers();
allUsers.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.addNew.Click += new System.EventHandler(this.AddUser_Click);
this.addExisting.Click += new System.EventHandler(this.AddUser_Click);
this.usersInRole.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.usersInRole_ItemCommand);
this.saveBtn.Click += new System.EventHandler(this.Save_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -