private void FrmUsrInfo_Load(object sender, EventArgs e) { // Add columnts to DataGridView and set it right var textColumn = new DataGridViewTextBoxColumn(); textColumn.Name = "Группая Объектов"; textColumn.FillWeight = 16; textColumn.ReadOnly = true; dgvPrivileges.Columns.Add(textColumn); var cmbColumn = new DataGridViewComboBoxColumn(); cmbColumn.Name = "Уровень доступа"; cmbColumn.FillWeight = 8; cmbColumn.FlatStyle = FlatStyle.Flat; dgvPrivileges.Columns.Add(cmbColumn); dgvPrivileges.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgvPrivileges.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; cmbColumn.Items.AddRange(levels); // Populating the organisations combobox cmbOrganisations.Items.Clear(); foreach (xOrganisation xorg in _Dh.myOrganisations) { cmbOrganisations.Items.Add(xorg.Name); } cmbOrganisations.SelectedIndex = 0; // Should it load data to DataGridView or is it new user with no data to fill? if (wUser != null) { fillTheForm(); fillDgvObGroups(true); } else { wUser = new xUser(); fillDgvObGroups(false); } // Cam I grant moderators rights to this user? if (_Dh.myAccount.isAdmin()) { chkIsModerator.Visible = true; } // Prevent from downgrading the admins permissions by someone else if (wUser.isAdmin()) { chkIsModerator.Visible = false; } // Has this user a moderator privileges? if (wUser.IsModerator) { chkIsModerator.Checked = true; } }
public static void loadUsers() { using (MySqlConnection connection = new MySqlConnection(connectionString())) { try { connection.Open(); MySqlDataReader reader; var cmd = new MySqlCommand("SELECT * from `users`", connection); reader = cmd.ExecuteReader(); _Dh.myUsers = new List <xUser>(); while (reader.Read()) { xUser x = new xUser() { Id = sfIntReader(reader, "id"), OrgId = sfIntReader(reader, "orgid"), Firstname = sfStringReader(reader, "firstname"), Lastname = sfStringReader(reader, "lastname"), Patronym = sfStringReader(reader, "patronym"), Login = sfStringReader(reader, "login"), Password = sfStringReader(reader, "password"), Pict64 = sfStringReader(reader, "pict64"), Phone = sfStringReader(reader, "phone"), Email = sfStringReader(reader, "email"), C_Date = sfStringReader(reader, "c_date"), IsModerator = (sfIntReader(reader, "ismoderator") == 1) ? true : false }; string s = sfStringReader(reader, "permissions"); x.Permissions = getPermissions(s); _Dh.myUsers.Add(x); } reader.Close(); } catch (MySqlException ex) { if (ex.Number == 1146) { if (_Dh.msgQuestion("В выбранной БД отсутствуют необходимые таблицы, хотите создать их сейчас?") == DialogResult.Yes) { createTables(); System.Threading.Thread.Sleep(2000); loadUsers(); } } else { err_processor(ex); } } } }
private void panel1_DoubleClick(object sender, EventArgs e) { if (ofdPictPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (wUser == null) { wUser = new xUser(); } wUser.Pict64 = ImageToBase64(ofdPictPicker.FileName); } }
public static AccessLevel getGroupAccessLevel(xUser u, xObGroup g) { try { if (u.IsModerator) { return(AccessLevel.Remove); } return(u.Permissions[g.Id]); } catch (KeyNotFoundException kex) { return(AccessLevel.Read); } catch (Exception ex) { err_processor(ex); return(AccessLevel.None); } }
private void btnOk_Click(object sender, EventArgs e) { wUser.Firstname = txtFirstname.Text; wUser.Lastname = txtLastname.Text; wUser.Patronym = txtPatronymic.Text; wUser.Phone = txtPhone.Text; wUser.Email = txtEmail.Text; wUser.OrgId = _Dh.myOrganisations[cmbOrganisations.SelectedIndex].Id; // Check if it is new user or not if (wUser.Id > 0) { // Write to DB using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(_Dh.connectionString())) { wUser.Permissions.Clear(); // Prepare permissions string representation string str_permissions = ""; for (int i = 0; i < _Dh.myObGroups.Count; i++) { string si = (string)dgvPrivileges.Rows[i].Cells[1].Value; int selectedIndex = Array.IndexOf(levels, si, 0); wUser.Permissions.Add(_Dh.myObGroups[i].Id, (_Dh.AccessLevel)selectedIndex); str_permissions += _Dh.myObGroups[i].Id + "-" + ((int)wUser.Permissions[_Dh.myObGroups[i].Id]) + ";"; } try { connection.Open(); { byte isModerator = wUser.IsModerator ? (byte)1 : (byte)0; MySqlCommand comm = new MySqlCommand(); comm.CommandText = @"UPDATE users SET orgid=@orgid,login=@login,password=@password,firstname=@firstname,lastname=@lastname,patronym=@patronym,permissions=@permissions,pict64=@pict64,phone=@phone,email=@email,ismoderator=@ismoderator,c_date=@c_date WHERE id=@id"; comm.Parameters.AddWithValue("@id", wUser.Id); comm.Parameters.AddWithValue("@orgid", wUser.OrgId); comm.Parameters.AddWithValue("@login", wUser.Login); comm.Parameters.AddWithValue("@password", wUser.Password); comm.Parameters.AddWithValue("@firstname", wUser.Firstname); comm.Parameters.AddWithValue("@lastname", wUser.Lastname); comm.Parameters.AddWithValue("@patronym", wUser.Patronym); comm.Parameters.AddWithValue("@permissions", str_permissions); comm.Parameters.AddWithValue("@pict64", wUser.Pict64); comm.Parameters.AddWithValue("@phone", wUser.Phone); comm.Parameters.AddWithValue("@email", wUser.Email); comm.Parameters.AddWithValue("@ismoderator", isModerator); comm.Parameters.AddWithValue("@c_date", wUser.C_Date); _Dh.dbExecute(comm); } } catch (Exception ex) { _Dh.err_processor(ex); } } _Dh.msgInformation("Данные пользователя обновлены!"); } else { // Password var pg = new PasswordGenerator(); pg.Minimum = 6; pg.Maximum = 6; pg.ExcludeSymbols = true; txtPassword.Text = pg.Generate(); // Initialize user object from input wUser = new xUser() { Firstname = txtFirstname.Text, Lastname = txtLastname.Text, Patronym = txtPatronymic.Text, Password = txtPassword.Text, Phone = txtPhone.Text, Email = txtEmail.Text, IsModerator = chkIsModerator.Checked, C_Date = _Dh.rightNow }; // Login Random rnd = new Random(DateTime.Now.Millisecond); wUser.Login = wUser.Lastname + "_" + wUser.Firstname[0] + rnd.Next(10, 99); txtLogin.Text = wUser.Login; // Write to DB using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(_Dh.connectionString())) { // Prepare permissions string representation string str_permissions = ""; for (int i = 0; i < _Dh.myObGroups.Count; i++) { string si = (string)dgvPrivileges.Rows[i].Cells[1].Value; int selectedIndex = Array.IndexOf(levels, si, 0); wUser.Permissions.Add(_Dh.myObGroups[i].Id, (_Dh.AccessLevel)selectedIndex); str_permissions += _Dh.myObGroups[i].Id + "-" + ((int)wUser.Permissions[_Dh.myObGroups[i].Id]) + ";"; } try { connection.Open(); { byte isModerator = wUser.IsModerator ? (byte)1 : (byte)0; MySqlCommand comm = new MySqlCommand(); comm.CommandText = "INSERT INTO users(login,password,firstname,lastname,patronym,permissions,pict64,phone,email,ismoderator,c_date) VALUES(@login,@password,@firstname,@lastname,@patronym,@permissions,@pict64,@phone,@email,@ismoderator,@c_date)"; comm.Parameters.AddWithValue("@orgid", wUser.OrgId); comm.Parameters.AddWithValue("@login", wUser.Login); comm.Parameters.AddWithValue("@password", wUser.Password); comm.Parameters.AddWithValue("@firstname", wUser.Firstname); comm.Parameters.AddWithValue("@lastname", wUser.Lastname); comm.Parameters.AddWithValue("@patronym", wUser.Patronym); comm.Parameters.AddWithValue("@permissions", str_permissions); comm.Parameters.AddWithValue("@pict64", wUser.Pict64); comm.Parameters.AddWithValue("@phone", wUser.Phone); comm.Parameters.AddWithValue("@email", wUser.Email); comm.Parameters.AddWithValue("@ismoderator", isModerator); comm.Parameters.AddWithValue("@c_date", wUser.C_Date); wUser.Id = _Dh.dbExecute(comm); } } catch (Exception ex) { _Dh.err_processor(ex); } } _Dh.myUsers.Add(wUser); _Dh.msgInformation("Пользователь успешно добавлен!"); } Close(); }