public UserEdit GetUserEdit(string username)
 {
     User u = this.repository.GetUser(username);
     UserEdit ue = null;
     if (u != null)
     {
         ue = new UserEdit(u);
         ue.RoleID = u.RoleID;
     }
     return ue;
 }
 public bool EditUser(UserEdit user)
 {
     User u = this.GetUser(user.UserID);
     if (u != null)
     {
         u.Name = user.UserName;
         u.Password = HashPassword(user.Password);
         u.Email = user.Email;
         u.RoleID = user.RoleID;
         SPDatabase.DB.SaveChanges();
         return true;
     }
     return false;
 }
        public bool UpdateUser(UserEdit user)
        {
            if (!user.Password.Equals(user.RepeatPassword))
                user.AddError("Oba hasła muszą być identyczne");

            User u = this.repository.GetUser(user.UserName);
            if (u != null && ((u.UserID != user.UserID) && u.Name.ToLower().Equals(user.UserName.ToLower())))
                user.AddError("Użytkownik o takiej nazwie już istnieje");

            if (user.IsValid)
            {
                if (!this.repository.EditUser(user))
                {
                    user.AddError("Użytkownik nie istnieje");
                    return false;
                }
                else
                    return true;
            }
            return false;
        }
        private void gridUsers_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {

            if (e.Column.Name.Equals("editUserColumn") && !e.RowIndex.Equals(-1))
            {
                lblValidation.Text = string.Empty;
                UserEdit u = UserController.Instance.GetUserEdit(gridUsers.Rows[e.RowIndex].Cells["usernameColumn"].Value.ToString());
                if (u != null)
                {
                    userToEdit = u;

                    Role r = RoleController.Instance.GetRole(u.RoleID);
                    if (r != null)
                    {
                        int index = 0;
                        foreach (object role in cbRoles.Items)
                        {
                            if (!role.ToString().ToLower().Equals(r.Name.ToLower()))
                                index++;
                            else
                                break;
                        }

                        cbRoles.SelectedIndex = index;
                    }

                    tbNewEmail.Text = u.Email;
                    tbNewUsername.Text = u.UserName;
                    tbNewPassword.Text = string.Empty;
                    tbNewRepeatPassword.Text = string.Empty;
                    btnAddUser.Enabled = false;
                    btnUpdate.Enabled = true;
                    btnCancelEdit.Enabled = true;
                }
                else
                {
                    MessageBox.Show("U¿ytkownik nie istnieje!");
                    FillWithUsers();
                }
            }
            else if (e.Column.Name.Equals("deleteUserColumn") && !e.RowIndex.Equals(-1))
            {
                if(RadMessageBox.Show ("Czy chcesz usun¹æ u¿ytkownika?", "Usuwanie u¿ytkownika",
                   MessageBoxButtons.YesNo, RadMessageIcon.Question).Equals(DialogResult.Yes))
                {
                    if (!gridUsers.Rows[e.RowIndex].Cells["usernameColumn"].Value.ToString().Equals(logged.Name))
                    {
                        if (!UserController.Instance.DeleteUser(gridUsers.Rows[e.RowIndex].Cells["usernameColumn"].Value.ToString()))
                            RadMessageBox.Show("Nie mo¿na usun¹æ", "B³¹d");
                        else
                        {
                            gridUsers.Rows.RemoveAt(e.RowIndex);
                            Clear();
                            btnAddUser.Enabled = true;
                            btnUpdate.Enabled = false;
                            btnCancelEdit.Enabled = false;
                        }
                    }
                    else
                        RadMessageBox.Show("Nie mo¿na usun¹æ w³asnego u¿ytkownika!", "B³¹d");
                }
            }
        }
 private void btnCancelEdit_Click(object sender, EventArgs e)
 {
     userToEdit = null;
     btnAddUser.Enabled = true;
     btnUpdate.Enabled = false;
     btnCancelEdit.Enabled = false;
     Clear();
     lblValidation.Text = string.Empty;
 }
 private void btnUpdate_Click(object sender, EventArgs e)
 {
     if (EditUser(userToEdit))
     {
         btnAddUser.Enabled = false;
         btnCancelEdit.Enabled = false;
         btnAddUser.Enabled = true;
         Clear();
         userToEdit = null;
     }
 }
        private bool EditUser(UserEdit u)
        {
            string errors = string.Empty;
            lblValidation.Text = "";
            u.ClearErrors();
            if (u != null)
            {
                u.UserName = tbNewUsername.Text;
                u.Password = tbNewPassword.Text;
                u.RepeatPassword = tbNewRepeatPassword.Text;
                u.Email = tbNewEmail.Text;

                int roleId = RoleController.Instance.GetRoleId(cbRoles.SelectedItem.ToString());
                if (roleId > 0)
                    u.RoleID = roleId;

                if (!UserController.Instance.UpdateUser(u))
                {
                    foreach (string error in u.Errors)
                        errors = errors + error + "\n";
                    lblValidation.Text = errors;
                    return false;
                }
            }
            FillWithUsers();
            return true;
        }