Exemplo n.º 1
0
		private void ShowIdCardContent(string idCard)
		{			
			if (string.IsNullOrWhiteSpace(idCard))
				return;

			try
			{
				using (var db = DbFactory.GetConnection())
				{
					var dal = new Dal(db);
					var user = dal.GetUserByIdCard(idCard);

					// Data validated
					var statusValid = string.Equals(user.Status, "A", StringComparison.OrdinalIgnoreCase);
					labelFlashCard.Hide();
					while (listViewUserInfo.Items.Count > 0) { listViewUserInfo.Items.RemoveAt(0); }
					listViewUserInfo.Show();
					listViewUserInfo.Items.AddRange(new ListViewItem[] {
						new ListViewItem(new string[] { "登录ID", user.LoginId }),
						new ListViewItem(new string[] { "姓名", user.Name }),
						new ListViewItem(new string[] { "状态", statusValid ? "正常" : "异常" })
					});

					buttonLogin.Enabled = statusValid;

					if (statusValid)
					{
						CurrentUser = user;
					}
				}
			}
			catch (Exception ex)
			{
				Logger.Error(ex, "Failed to get user by ID card.");
				MessageBox.Show("无法获取ID Card信息.");
				return;
			}

		}
Exemplo n.º 2
0
		private void DoLogin()
		{
			if (loginMode == LoginMode.IdCard)
			{
				DialogResult = DialogResult.OK;
				return;
			}

			var loginId = textBoxUserId.Text.Trim();
			var pwd = textBoxPwd.Text;

			// TODO: encrypt the inputted pwd and compare with the one in db
			try
			{
				using (var db = DbFactory.GetConnection())
				{
					var dal = new Dal(db);
					var user = dal.GetUser(loginId);
					if (user == null)
					{
						MessageBox.Show(string.Format("用户[{0}]不存在。", loginId));
						return;
					}
					
					if (user.Status != "A")
					{
						MessageBox.Show("用户[{0}]状态异常,不允许登录。", loginId);
						return;
					}

					// todo encrypt inputted pwd
					var encryptedPwd = pwd;
					if (encryptedPwd != user.Password)
					{
						MessageBox.Show("用户名或密码错误,请重新输入。");
						textBoxPwd.Select();
						return;
					}

					// Validate passed
					CurrentUser = user;
					DialogResult = DialogResult.OK;

					OpenNextForm();

					Close();
				}
			}
			catch (Exception ex)
			{
				Logger.Error(ex, "Failed to get user info.");
				MessageBox.Show("登录时遇到错误,请重试.");
			}
		}
Exemplo n.º 3
0
		public User UpdateUser(User user)
		{
			// User dataset for updating User since dataset is able to check if the data is changed or not
			var sql = string.Format(@"SELECT * FROM [User] WHERE UserId=CONVERT(uniqueidentifier,'{0}')", user.UserId);
			var ds = new DataSet();
			var adapter = new SqlDataAdapter();
			adapter.SelectCommand = new SqlCommand(sql, connection);
			var scb = new SqlCommandBuilder(adapter);
			
			adapter.Fill(ds);
			if (ds.Tables[0].Rows.Count < 1)
				throw new DllNotFoundException();

			var columns = ds.Tables[0].Columns;
			var row = ds.Tables[0].Rows[0];
			var properties = typeof(User).GetProperties()
										.Where(p => p.CanRead && !(p.PropertyType.IsGenericType));

			foreach (var prop in properties)
			{
				if (!columns.Contains(prop.Name))
					continue;

				row[prop.Name] = prop.GetValue(user);
			}

			adapter.Update(ds);

			return user;
		}
Exemplo n.º 4
0
		public bool DeleteUser(User user)
		{
			using (var trans = connection.BeginTransaction())
			{				
				using (var cmd = connection.CreateCommand())
				{
					var sql = string.Format("DELETE FROM UserInRole WHERE UserId=CONVERT(uniqueidentifier,'{0}')", user.UserId);
					cmd.CommandText = sql;
					cmd.ExecuteNonQuery();

					sql = string.Format("DELETE FROM [User] WHERE UserId=CONVERT(uniqueidentifier,'{0}')", user.UserId);
					cmd.CommandText = sql;
					var ret = (cmd.ExecuteNonQuery() == 0);

					trans.Commit();

					return ret;
				}				
			}
		}
Exemplo n.º 5
0
		public User AddUser(User user)
		{
			if (string.IsNullOrWhiteSpace(user.LoginId) || string.IsNullOrWhiteSpace(user.Name))
				throw new ArgumentNullException("LoginId or Name");

			var count = GetUserCountByLoginId(user.LoginId);
			if (count > 0)
				throw new DuplicateNameException("The LoginId is duplicated.");

			var newId = Guid.NewGuid().ToString();
			var sql = string.Format("INSERT INTO [User] (UserId,LoginId,Name,IDCard,Status) VALUES(CONVERT(uniqueidentifier,'{0}'),'{1}','{2}','{3}','A')", 
							newId, user.LoginId, user.Name, user.IDCard ?? "");

			using (var cmd = connection.CreateCommand())
			{
				cmd.CommandText = sql;
				cmd.ExecuteNonQuery();
			}

			user.UserId = newId;

			return user;
		}
Exemplo n.º 6
0
		private User InternalGetUser(string condition)
		{
			var sql = @"SELECT UserId,LoginId,Name,Password,IDCard,Status FROM [User]";
			if (!string.IsNullOrWhiteSpace(condition))
			{
				sql += string.Format(" WHERE {0}", condition);
			}

			var ds = new DataSet();
			var adapter = new SqlDataAdapter(sql, connection);
			adapter.Fill(ds);

			if (ds.Tables[0].Rows.Count < 1)
				return null;

			var row = ds.Tables[0].Rows[0];
			var user = new User
			{
				UserId = row["UserId"].ToString(),
				LoginId = row["LoginId"].ToString(),
				Name = row["Name"].ToString(),
				Password = row["Password"].ToString(),
				IDCard = row["IDCard"].ToString(),
				Status = row["Status"].ToString()
			};

			user.Roles = new List<string>();
			var roleSql = string.Format(@"SELECT RoleId FROM UserInRole WHERE UserId=CONVERT(uniqueidentifier, '{0}')", user.UserId);
			adapter = new SqlDataAdapter(roleSql, connection);
			adapter.Fill(ds, "RoleList");
			foreach (DataRow r in ds.Tables["RoleList"].Rows)
			{
				user.Roles.Add(r[0].ToString());
			}
			
			return user;
		}
Exemplo n.º 7
0
		public ICollection<User> GetUsers()
		{
			var users = new List<User>();
			var sql = @"SELECT UserId,LoginId,Name,Password,IDCard,Status FROM [User]";

			var ds = new DataSet();
			var adapter = new SqlDataAdapter(sql, connection);
			adapter.Fill(ds);

			if (ds.Tables[0].Rows.Count < 1)
				return users;

			foreach (DataRow row in ds.Tables[0].Rows)
			{
				var user = new User
				{
					UserId = row["UserId"].ToString(),
					LoginId = row["LoginId"].ToString(),
					Name = row["Name"].ToString(),
					Password = row["Password"].ToString(),
					IDCard = row["IDCard"].ToString(),
					Status = row["Status"].ToString()
				};

				users.Add(user);
			}

			return users;
		}