Exemple #1
0
        public static MnjUsers getInstance()
        {
            if (instance == null)
            {
                instance = new MnjUsers();
            }

            return(instance);
        }
 private void usuariosToolStripMenuItem_Click(object sender, EventArgs e)
 {
     MnjUsers.getInstance().ShowDialog(this);
 }
Exemple #3
0
 private void MnjUsers_FormClosing(object sender, FormClosingEventArgs e)
 {
     instance = null;
 }
        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();
            }
        }