public UserTileControl(UserData userData, ObservableCollection<RoleData> roles, bool isNewUser, Action<bool, UserData> AddSuccessAction) { try { InitializeComponent(); this._IsNewUser = isNewUser; this._user = userData; this._roles = roles; this.RoleName_List.ItemsSource = this._roles; this._UpdateSuccessAction = AddSuccessAction; if (isNewUser) { this.UserName.Text = ""; this.NewPassword.Password = string.Empty; this.Confirm.Password = string.Empty; } else { this.NewPassword.Password = "******"; this.Confirm.Password = "******"; this.UserName.Text = userData.UserName; foreach (RoleData item in userData.Roles) { this.RoleName_List.SelectedItems.Add(item); } } } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "UserTileControl.\r\n{0}", ex.ToString()); } }
public UserTileControl(Guid userId, string userName, int roleId,string roleName,bool isNewUser,Action<UserData> AddSuccessAction) { this._isNewUser = isNewUser; this._user = new UserData(); this._user.UserId = userId; this._user.RoleId = roleId; this._user.UserName = userName; this._user.RoleName = roleName; this._AddSuccessAction = AddSuccessAction; InitializeComponent(); if (isNewUser) { this.Edit_Click(null, null); this.ChangePassword_Click(null, null); this.Cancel.Visibility = System.Windows.Visibility.Hidden; } else { this.UserName.Text = userName; this.RoleName_Text.Text = roleName; this.UserName.IsReadOnly = true; this.NewPassword.IsReadOnly = true; this.RoleName_List.SelectedValue = roleId; } }
public static bool AddNewUser(UserData user, string password) { string encryptPassword = UserDataAccess.GetMd5EncryptPassword(password); using (SqlConnection sqlConnection = DataAccess.GetSqlConnection()) { SqlCommand command = sqlConnection.CreateCommand(); command.CommandText = string.Format("INSERT INTO dbo.Users(Id,[Name],[Password]) VALUES('{0}','{1}','{2}') INSERT INTO dbo.UserInRole(UserId,RoleId) VALUES ('{3}',{4})",user.UserId,user.UserName,encryptPassword,user.UserId,user.RoleId); command.CommandType = System.Data.CommandType.Text; command.ExecuteNonQuery(); } return true; }
public void DeleteUser(bool isSuccess,UserData user) { try { if (isSuccess) { this.UserManager.Items.Remove(new UserTileControl(user, false, DeleteUser)); } } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "UserManager/DeleteUser.\r\n{0}", ex.ToString()); } }
public void AddUserSuccess(UserData user) { try { XamTile tile = new XamTile(); tile.CloseAction = TileCloseAction.DoNothing; tile.Header = user.UserName; tile.Content = new UserTileControl(user.UserId, user.UserName, user.RoleId, user.RoleName, false, AddUserSuccess); this.UserManager.Items.Add(tile); } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "AddUserSuccess.\r\n{0}", ex.ToString()); } }
public UserTileControl(UserData userData, bool isNewUser, Action<bool,UserData> DeleteUser) { InitializeComponent(); this._isNewUser = isNewUser; this._user = new UserData(); this._user.UserId = userData.UserId; this._user.RoleId = userData.RoleId; this._user.UserName = userData.UserName; this._user.RoleName = userData.RoleName; this._DeleteUser = DeleteUser; this.UserName.Text = userData.UserName; this.RoleName_Text.Text = userData.RoleName; this.UserName.IsReadOnly = true; this.NewPassword.IsReadOnly = true; this.RoleName_List.SelectedValue = userData.RoleId; }
public void AddUserSuccess(bool isNewUser, UserData user) { try { UserModel newUser = new UserModel(); newUser.UserId = user.UserId; newUser.UserName = user.UserName; foreach (RoleData item in user.Roles) { newUser.Roles += item.RoleName + ";"; } this._users.Add(newUser); //this.UserManager.Items.Add(tile); } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "AddUserSuccess.\r\n{0}", ex.ToString()); } }
public UserTileControl(Guid userId, string userName, int roleId, string roleName, bool isNewUser, ObservableCollection<RoleData> role, Action<bool, UserData> AddSuccessAction) { this._IsNewUser = isNewUser; this._user = new UserData(); this._user.UserId = userId; this._user.UserName = userName; this._roles = role; this._UpdateSuccessAction = AddSuccessAction; InitializeComponent(); this.RoleName_List.ItemsSource = this._roles; if (isNewUser) { this.Cancel.Visibility = System.Windows.Visibility.Hidden; } else { this.UserName.Text = userName; this.RoleName_List.SelectedItem = roleId; this.RoleName_List.ItemsSource = this._roles; } }
public bool UpdateUsers(UserData user, string password, bool isNewUser) { try { if (isNewUser) { UserDataAccess.AddNewUser(user, password); return true; } else { UserDataAccess.EditUser(user, password); return false; } } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "UpdateUsers.\r\n{0}", ex.ToString()); return false; } }
public static List<UserData> GetUserData() { List<UserData> data = new List<UserData>(); using (SqlConnection sqlConnection = DataAccess.GetSqlConnection()) { SqlCommand command = sqlConnection.CreateCommand(); command.CommandText = "SELECT u.Id,u.[Name],r.Id,r.RoleName FROM dbo.Users u INNER JOIN dbo.UserInRole uir ON uir.UserId = u.Id INNER JOIN dbo.Roles r ON r.Id = uir.RoleId"; command.CommandType = System.Data.CommandType.Text; SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { UserData user = new UserData(); user.UserId = (Guid)reader.GetValue(0); user.UserName = reader.GetValue(1).ToString(); user.RoleId = (int)reader.GetValue(2); user.RoleName = reader.GetValue(3).ToString(); data.Add(user); } } return data; }
public static bool EditUser(UserData user, string password) { string commandText = "UPDATE dbo.Users SET"; if (!string.IsNullOrEmpty(user.UserName)) { commandText += string.Format("[Name]='{0}'",user.UserName); } if (string.IsNullOrEmpty(password)) { commandText += string.Format("[Password]=N'{0}'", UserDataAccess.GetMd5EncryptPassword(password)); } commandText += string.Format("WHERE Id='{0}'", user.UserId); if (user.RoleId!=0) { commandText += string.Format("UPDATE dbo.UserInRole SET RoleId = {0} WHERE UserId='{1}'", user.RoleId, user.UserId); } using (SqlConnection sqlConnection = DataAccess.GetSqlConnection()) { SqlCommand command = sqlConnection.CreateCommand(); command.CommandText = commandText; command.CommandType = System.Data.CommandType.Text; command.ExecuteNonQuery(); } return true; }
private void Button_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; try { Guid userId = (Guid)btn.Tag; UserModel userModel = this._users.Single(u => u.UserId == userId); if (userId != Guid.Empty) { if (btn.Name == "Edit") { UserData user = new UserData(); user.UserId = userModel.UserId; user.UserName = userModel.UserName; string[] roles = userModel.Roles.Split(';'); foreach (string item in roles) { RoleData role = this._roles.SingleOrDefault(r => r.RoleName == item); if (role != null) { user.Roles.Add(role); } } UserTileControl tile = new UserTileControl(user, this._roles, false, this.AddUserSuccess); this.UseManagerFrame.Children.Add(tile); tile.IsModal = true; tile.Show(); tile.BringToFront(); } else if (btn.Name == "Delete") { if (MessageBox.Show(App.MainFrameWindow, string.Format("确认删除{0}用户吗", userModel.UserName), "Warnning", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { ConsoleClient.Instance.DeleteUser(userId, this.DeleteUser); } } } } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "UserManagerButton_Click.\r\n{0}{1}", btn.Name, ex.ToString()); } }
public static bool AddNewUser(UserData user, string password) { string encryptPassword = UserDataAccess.GetMd5EncryptPassword(password); string roles = string.Empty; bool isSuccess = false; foreach (RoleData role in user.Roles) { roles += (role.RoleId + ","); } SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@userId", user.UserId), new SqlParameter("@userName", user.UserName), new SqlParameter("@password",encryptPassword), new SqlParameter("@roles",roles) }; using (SqlConnection sqlConnection = DataAccess.GetInstance().GetSqlConnection()) { using (SqlTransaction transaction = sqlConnection.BeginTransaction()) { using (SqlCommand command = sqlConnection.CreateCommand()) { command.CommandText = "[dbo].[User_AddNew]"; command.Transaction = transaction; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@userId", user.UserId)); command.Parameters.Add(new SqlParameter("@userName", user.UserName)); command.Parameters.Add(new SqlParameter("@password", encryptPassword)); command.Parameters.Add(new SqlParameter("@roles", roles)); command.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue }); command.ExecuteNonQuery(); int returnValue = (int)command.Parameters["@RETURN_VALUE"].Value; isSuccess = (returnValue == 0); if (isSuccess) { transaction.Commit(); } } } } return isSuccess; }
private void EndAddNewUser(bool isSuccess) { try { this.Dispatcher.BeginInvoke((Action<bool>)delegate(bool result) { if (!result) { this.Message.Foreground = Brushes.Red; this.Message.Content = "录入失败"; } else { this.UserName.Text = ""; this.NewPassword.Text = ""; this.Confirm.Text = ""; this.Message.Foreground = Brushes.Green; this.Message.Content = "录入成功"; UserData user = new UserData(); this._AddSuccessAction(this._user); } }, isSuccess); } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "EndAddNewUser.\r\n{0}", ex.ToString()); } }
private void Submit_Click(object sender, System.Windows.RoutedEventArgs e) { try { if (this.CheckSubmitData()) { UserData user = new UserData(); string password = string.Empty; foreach (var item in this.RoleName_List.SelectedItems) { user.Roles.Add((RoleData)item); } if (this._IsNewUser) { this._user.UserId = Guid.NewGuid(); password = this.NewPassword.Password; this._user.Roles=user.Roles; this._user.UserName = this.UserName.Text; } else { if (this.NewPassword.Password != "12345678") { password = this.NewPassword.Password; } } user.UserId = this._user.UserId; user.UserName = this._user.UserName; ConsoleClient.Instance.UpdateUser(user, password, this._IsNewUser, EndUpdateUser); } } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "Submit_Click.\r\n{0}", ex.ToString()); } }
public static bool EditUser(UserData user, string password, out Dictionary<string, List<FuncPermissionStatus>> permissions) { permissions = new Dictionary<string, List<FuncPermissionStatus>>(); string roles = string.Empty; bool isSuccess = false; foreach (RoleData role in user.Roles) { roles += (role.RoleId + ","); } using (SqlConnection sqlConnection = DataAccess.GetInstance().GetSqlConnection()) { using (SqlTransaction transaction = sqlConnection.BeginTransaction()) { using (SqlCommand command = sqlConnection.CreateCommand()) { command.CommandText = "[dbo].[Users_Update]"; command.Transaction = transaction; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@userId", user.UserId)); if (!string.IsNullOrEmpty(user.UserName)) { command.Parameters.Add(new SqlParameter("@userName", user.UserName)); } if (!string.IsNullOrEmpty(password)) { string encryptPassword = UserDataAccess.GetMd5EncryptPassword(password); command.Parameters.Add(new SqlParameter("@password", encryptPassword)); } if (user.Roles.Count != 0) { command.Parameters.Add(new SqlParameter("@roles", roles)); } command.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue }); using (SqlDataReader reader = command.ExecuteReader()) { if (!string.IsNullOrEmpty(roles)) { while (reader.Read()) { FuncPermissionStatus func = new FuncPermissionStatus { Code = reader["Code"].ToString(), HasPermission = (bool)reader["Status"] }; if (int.Parse(reader["Level"].ToString()) == 1) { if (!permissions.ContainsKey("root")) { permissions.Add("root", new List<FuncPermissionStatus>()); } permissions["root"].Add(func); } else { if (!permissions.ContainsKey(reader["Parent"].ToString())) { permissions.Add(reader["Parent"].ToString(), new List<FuncPermissionStatus>()); } permissions[reader["Parent"].ToString()].Add(func); //permissions.Add(reader["Parent"].ToString(), ); } } } } int returnValue = (int)command.Parameters["@RETURN_VALUE"].Value; isSuccess = (returnValue == 0); if (isSuccess) { transaction.Commit(); } } } } return isSuccess; }
public static List<UserData> GetUserData() { List<UserData> data = new List<UserData>(); using (SqlConnection sqlConnection = DataAccess.GetInstance().GetSqlConnection()) { SqlCommand command = sqlConnection.CreateCommand(); command.CommandText = "SELECT u.Id,u.[Name] FROM dbo.Users u SELECT uir.UserId,uir.RoleId,r.RoleName FROM dbo.UserInRole uir INNER JOIN dbo.Roles r ON r.Id = uir.RoleId"; command.CommandType = System.Data.CommandType.Text; SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { UserData user = new UserData(); user.UserId = (Guid)reader["Id"]; user.UserName = reader["Name"].ToString(); data.Add(user); } reader.NextResult(); while (reader.Read()) { RoleData role = new RoleData(); role.RoleId = (int)reader["RoleId"]; role.RoleName = reader["RoleName"].ToString(); data.Find(u=>u.UserId==(Guid)reader["UserId"]).Roles.Add(role); } } return data; }
private void Submit_Click(object sender, System.Windows.RoutedEventArgs e) { try { if (this.CheckSubmitData()) { UserData user = new UserData(); RoleData role = (RoleData)this.RoleName_List.SelectedItem; if (this._isNewUser) { user.UserId = Guid.NewGuid(); user.UserName = this.UserName.Text; user.RoleId = role.RoleId; user.RoleName = role.RoleName; this._user = user; string password = this.NewPassword.Text; ConsoleClient.Instance.UpdateUser(user, password, this._isNewUser, EndAddNewUser); } else { user.UserId = this._user.UserId; user.UserName = this.UserName.Text; if (this._user.UserName == this.UserName.Text) { user.UserName = string.Empty; } if (role.RoleId != this._user.RoleId) { user.RoleId = role.RoleId; user.RoleName = role.RoleName; } else { user.RoleId = 0; user.RoleName = string.Empty; } string password = string.Empty; if (this.ChangePassword.Visibility == System.Windows.Visibility.Hidden) { password = this.NewPassword.Text; } ConsoleClient.Instance.UpdateUser(user, password, false, EndAddNewUser); } } } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "Submit_Click.\r\n{0}", ex.ToString()); } }
public void UpdateUser(UserData user, string password,bool isNewUser, Action<bool> EndUpdateUser) { this._ServiceProxy.BeginUpdateUsers(user, password,isNewUser, delegate(IAsyncResult ar) { bool isSuccess = this._ServiceProxy.EndUpdateUsers(ar); EndUpdateUser(isSuccess); }, null); }
private void AddUser_Click(object sender, RoutedEventArgs e) { try { UserData user = new UserData(); UserTileControl newUserDialog = new UserTileControl(user, this._roles, true, AddUserSuccess); this.UseManagerFrame.Children.Add(newUserDialog); newUserDialog.IsModal = true; newUserDialog.Show(); newUserDialog.BringToFront(); } catch (Exception ex) { Logger.TraceEvent(System.Diagnostics.TraceEventType.Error, "AddUser_Click.\r\n{0}", ex.ToString()); } }