예제 #1
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = null;

            if (dgvUsers.SelectedRows.Count == 1)
            {
                row = dgvUsers.SelectedRows[0];
            }
            else if (dgvUsers.SelectedCells.Count == 1)
            {
                int i = dgvUsers.SelectedCells[0].RowIndex;
                row = dgvUsers.Rows[i];
            }
            else
            {
                MessageBox.Show(
                    "Debes seleccionar solo 1 usuario a eliminar",
                    "Información",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
            }

            if (row != null)
            {
                DialogResult deleteIt = MessageBox.Show(
                    "¿Estas seguro de eliminar este usuario?",
                    "Confirmar",
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question
                    );

                if (deleteIt != DialogResult.Yes)
                {
                    return;
                }

                int   id        = Int32.Parse(row.Cells[0].Value.ToString());
                users localUser = (from em in entities.users
                                   where em.id == id
                                   select em).First();

                if (localUser == null)
                {
                    MessageBox.Show(
                        "El Usuario seleccionado no existe",
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error
                        );
                    loadUsers("");
                }
                else
                {
                    int adminTotal = entities.users.Count(el => el.permission == 1);

                    if (adminTotal == 1 && localUser.permission == 1)
                    {
                        MessageBox.Show(
                            "No puedes eliminar al único administrador",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                    }
                    else
                    {
                        bool delete       = true;
                        bool sameAsLogged = false;
                        if (localUser.id == Contabilidad.user.id)
                        {
                            delete       = false;
                            sameAsLogged = true;
                            DialogResult response = MessageBox.Show(
                                "Estas a punto de eliminar tu usuario. Esta acción te expulsará del sistema\n\n¿Deseas continuar?",
                                "Confirmar",
                                MessageBoxButtons.YesNoCancel,
                                MessageBoxIcon.Question
                                );

                            if (response == DialogResult.Yes)
                            {
                                delete = true;
                            }
                        }

                        if (delete)
                        {
                            entities.users.Remove(localUser);
                            entities.SaveChanges();

                            MessageBox.Show(
                                "¡Usuario eliminado con éxito!",
                                "Información",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information
                                );

                            if (sameAsLogged)
                            {
                                IniciarSesion.getInstance().Show();
                                Contabilidad.showLogin = true;
                                Contabilidad.getInstance().Close();
                            }
                            else
                            {
                                loadUsers("");
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            users userLocal = null;

            if (user == null)
            {
                userLocal = new users
                {
                    name       = txtName.Text,
                    created_at = DateTime.Now,
                    permission = ((UserLevel)cbLevel.SelectedItem).value,
                    username   = txtUsername.Text
                };

                using (MD5 md5 = MD5.Create())
                {
                    byte[] hash = Encoding.ASCII.GetBytes(txtPassword.Text);
                    hash = md5.ComputeHash(hash);

                    StringBuilder sb = new StringBuilder();
                    foreach (var x in hash)
                    {
                        sb.Append(x.ToString("x2"));
                    }

                    userLocal.password = sb.ToString();
                }
            }
            else
            {
                user            = entities.users.First(el => el.id == user.id);
                user.name       = txtName.Text;
                user.updated_at = DateTime.Now;
                user.permission = ((UserLevel)cbLevel.SelectedItem).value;
                user.username   = txtUsername.Text;

                if (txtPassword.Modified)
                {
                    using (MD5 md5 = MD5.Create())
                    {
                        byte[] hash = Encoding.ASCII.GetBytes(txtPassword.Text);
                        hash = md5.ComputeHash(hash);

                        StringBuilder sb = new StringBuilder();
                        foreach (var x in hash)
                        {
                            sb.Append(x.ToString("x2"));
                        }

                        user.password = sb.ToString();
                    }
                }
            }

            String errors = "";

            if (userLocal == null)
            {
                errors = validate(user);
            }
            else
            {
                errors = validate(userLocal);
            }

            if (errors.Length > 0)
            {
                MessageBox.Show(
                    errors,
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
            else
            {
                if (userLocal != null)
                {
                    entities.users.Add(userLocal);
                }
                entities.SaveChanges();

                MessageBox.Show(
                    "¡Usuario guardado con éxito!",
                    "Información",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
                MnjUsers.getInstance().loadUsers("");
                this.Close();
            }
        }