⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manageusers.aspx.cs

📁 一个采用VS2008+Sql2000开发的任务管理系统
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
namespace BronzeMonkey.GeneralTaskList
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public partial class WebForm1 : System.Web.UI.Page
	{
	
		private TaskList tl = new TaskList();
		private UserInformation CurrentUser = new UserInformation();

		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if (Session["CurrentUser"] == null) 
				Response.Redirect("login.aspx", false);
			else
				CurrentUser = (UserInformation)Session["CurrentUser"];

			if (!Page.IsPostBack)
			{
				// Populate the task list drop down box
				this.LoadUserListDropDown();

				// Now load the task list properties part of the page
				this.lnkUserProperties_Click(lnkUserProperties, new EventArgs());

				// Add a javascript confirm for the "Delete this User" link.  We want to warn them first!
				lnkUserPropertiesDeleteUser.Attributes.Add(
					"onclick",
					@"return confirm (""Are you sure you want to delete this User?"");"
				);

				// Now hide stuff if the user doesn't have correct permissions
				OtherTasksHeader.Visible = false;
				if (CurrentUser.IsManager) OtherTasksHeader.Visible = true;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void lnkLogOff_Click(object sender, System.EventArgs e)
		{
			Session.Abandon();
			System.Web.Security.FormsAuthentication.SignOut();
			Response.Redirect("login.aspx", false);
		}

		protected void btnCreateUser_Click(object sender, System.EventArgs e)
		{			
			// Set up the user's username and password
			UserInformation NewUser = new UserInformation(
				0,
				txtUserName.Text,
				FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword1.Text, "md5"),
				chkIsManager.Checked,
				chkIsAdministrator.Checked
			);
	
			// Call the web service to add this user
			tl.AddUser(this.CurrentUser, NewUser);
			// and show the user's properties
			LoadUserListDropDown();
			cboUserList.SelectedIndex = -1;
			cboUserList.Items.FindByText(NewUser.Username).Selected = true;
			lnkUserProperties_Click(sender, e);
		}

		protected void lnkCreateUser_Click(object sender, System.EventArgs e)
		{
			HideAllOtherPanels(pnlCreateUser);
		}

		private void HideAllOtherPanels(Panel VisiblePanel)
		{
			if (VisiblePanel != null) VisiblePanel.Visible = true;

			if (pnlCreateUser != VisiblePanel) pnlCreateUser.Visible = false;
			if (pnlUserProperties != VisiblePanel) pnlUserProperties.Visible = false;
			if (pnlChangeUsername != VisiblePanel) pnlChangeUsername.Visible = false;
			if (pnlChangePassword != VisiblePanel) pnlChangePassword.Visible = false;
			if (pnlChangePermissions != VisiblePanel) pnlChangePermissions.Visible = false;
		}

		/// <summary>
		/// Retrieves a list of users from the database
		/// </summary>
		private void LoadUserListDropDown()
		{
			SqlDataReader dr = tl.GetUserList(CurrentUser);

			cboUserList.Items.Clear();
			while (dr.Read())
			{
				cboUserList.Items.Add(new ListItem(dr["Username"].ToString(), dr["UserID"].ToString()));
			}
		}

		protected void lnkUserProperties_Click(object sender, System.EventArgs e)
		{
			HideAllOtherPanels(pnlUserProperties);
			ShowUserProperties(Convert.ToInt32(cboUserList.SelectedItem.Value));
		}

		protected void cboUserList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			lnkUserProperties_Click(sender, e); //view the user properties for the newly selected user
		}

		private void ShowUserProperties(int UserID)
		{
			// First get the task list name
			lblUserPropertiesName.Text = "User Properties - " + cboUserList.SelectedItem.Text;

			// Second get the task lists this user is assignedto
			SqlDataReader drTaskLists = tl.GetUserTaskLists(CurrentUser, Convert.ToInt32(cboUserList.SelectedItem.Value));
			string AssignedTaskLists = "";

			while (drTaskLists.Read())
			{
				AssignedTaskLists += drTaskLists["TaskListName"] + ", ";
			}
			if (AssignedTaskLists.Length > 0) AssignedTaskLists = AssignedTaskLists.Substring(0, AssignedTaskLists.Length - 2);
			lblUserPropertiesAssignedTaskLists.Text = "<b>Assigned Task lists:</b> " + AssignedTaskLists;

			//Now get the rest of the data by calling a stored proc
			SqlDataReader drProperties = tl.GetUserProperties(CurrentUser, UserID);

			DateTime LastModified = DateTime.Now;
			DateTime LastCreated = DateTime.Now;
			
			lblUserPropertiesTotalTasks.Text = "0";
			lblUserPropertiesLastTaskModified.Text = String.Empty;
			lblUserPropertiesLastTaskCreated.Text = String.Empty;
			
			while (drProperties.Read())
			{
				lblUserPropertiesTotalTasks.Text = drProperties["TaskListItems"].ToString();

				if (drProperties["LastModified"] != System.DBNull.Value) 
				{
					LastModified = (DateTime)drProperties["LastModified"];
					lblUserPropertiesLastTaskModified.Text = LastModified.ToString("MM/dd/yy hh:mm");
				}
				if (drProperties["LastCreated"] != System.DBNull.Value) 
				{
					LastCreated = (DateTime)drProperties["LastCreated"];
					lblUserPropertiesLastTaskCreated.Text = LastCreated.ToString("MM/dd/yy hh:mm");
				}
			}
		}


		protected void btnChangeUsername_Click(object sender, System.EventArgs e)
		{
			// First retrieve a user object from the database for the user we want to modify
			UserInformation OldUser = tl.GetUserItem(CurrentUser, Convert.ToInt32(cboUserList.SelectedItem.Value));
			
			// Then change it's user name
			UserInformation NewUser = new UserInformation(
				OldUser.UserID, 
				txtNewUserName.Text, 
				OldUser.PasswordHash,
				OldUser.IsManager,
				OldUser.IsAdministrator
			);

			// And let the web service change it
			tl.ModifyUser(CurrentUser, OldUser, NewUser);

			// Now refresh the screen by reloading the user drop down, and going to user properties
			LoadUserListDropDown();
			// And select the proper user out of the drop down list
			cboUserList.SelectedIndex = -1;
			cboUserList.Items.FindByText(NewUser.Username).Selected = true;

			lnkUserProperties_Click(sender, e);
		}

		protected void btnChangePassword_Click(object sender, System.EventArgs e)
		{
			UserInformation OldUser = tl.GetUserItem(CurrentUser, Convert.ToInt32(cboUserList.SelectedItem.Value));

			// If they didn't enter the right password, bomb out
			if (FormsAuthentication.HashPasswordForStoringInConfigFile(txtOldPassword.Text, "md5") != OldUser.PasswordHash) 
			{
				OldPasswordValidator.IsValid = false;
				return;
			}

			// Then change it's password
			UserInformation NewUser = new UserInformation(
				OldUser.UserID, 
				OldUser.Username, 
				FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword1.Text, "md5"),
				OldUser.IsManager,
				OldUser.IsAdministrator
			);

			// And let the web service change it
			tl.ModifyUser(CurrentUser, OldUser, NewUser);

			lnkUserProperties_Click(sender, e);
		}

		protected void lnkUserPropertiesChangeUsername_Click(object sender, System.EventArgs e)
		{
			HideAllOtherPanels(pnlChangeUsername);
			txtCurrentUserName.Text = cboUserList.SelectedItem.Text;
			txtNewUserName.Text = String.Empty;
		}

		private void lnkUsrePropertiesChangePassword_Click(object sender, System.EventArgs e)
		{
			HideAllOtherPanels(pnlChangePassword);
			txtChangePasswordCurrentUserName.Text = cboUserList.SelectedItem.Text;
			txtOldPassword.Text = String.Empty;
			txtNewPassword1.Text = String.Empty;
			txtNewPassword2.Text = String.Empty;
		}

		protected void lnkUserPropertiesDeleteUser_Click(object sender, System.EventArgs e)
		{
			tl.DeleteUser(CurrentUser, tl.GetUserItem(CurrentUser, Convert.ToInt32(cboUserList.SelectedItem.Value)));
			LoadUserListDropDown();
			lnkUserProperties_Click(sender, e);
		}

		protected void lnkUserPropertiesChangeUserPermissions_Click(object sender, System.EventArgs e)
		{
			HideAllOtherPanels(pnlChangePermissions);
			UserInformation ThisUser = tl.GetUserItem(CurrentUser, Convert.ToInt32(cboUserList.SelectedItem.Value));
			chkUserPermissions.Items.FindByValue("Administrator").Selected = ThisUser.IsAdministrator;
			chkUserPermissions.Items.FindByValue("Manager").Selected = ThisUser.IsManager;
		}

		protected void btnChangePermissions_Click(object sender, System.EventArgs e)
		{
			UserInformation OldUser = tl.GetUserItem(CurrentUser, Convert.ToInt32(cboUserList.SelectedItem.Value));
			UserInformation NewUser = new UserInformation(
				OldUser.UserID, 
				OldUser.Username,
				OldUser.PasswordHash, 
				chkUserPermissions.Items.FindByValue("Manager").Selected,
				chkUserPermissions.Items.FindByValue("Administrator").Selected
			);

			tl.ModifyUser(CurrentUser, OldUser, NewUser);
			lnkUserProperties_Click(sender, e);
		}

		protected void lnkManageTaskLists_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("ManageTaskList.aspx", false);
		}

		protected void lnkUserPropertiesChangePassword_Click(object sender, System.EventArgs e)
		{
			HideAllOtherPanels(pnlChangePassword);
			UserInformation ThisUser = tl.GetUserItem(CurrentUser, Convert.ToInt32(cboUserList.SelectedItem.Value));
			txtChangePasswordCurrentUserName.Text = ThisUser.Username;
		}

		protected void chkUserPermissions_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -