From 4527f07c89bdb3fe02fcc8006221b93e13a189e7 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Tue, 24 May 2022 09:09:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AdministrationForm.Designer.cs | 83 ++++++++++++++++++ AdministrationForm.cs | 31 +++++++ Authorization.cs | 48 ++++++++-- AwesomeEmailExtractor.csproj | 9 ++ EditUserForm.Designer.cs | 154 +++++++++++++++++++++++++++++++++ EditUserForm.cs | 55 ++++++++++++ EditUserForm.resx | 120 +++++++++++++++++++++++++ 7 files changed, 491 insertions(+), 9 deletions(-) create mode 100644 EditUserForm.Designer.cs create mode 100644 EditUserForm.cs create mode 100644 EditUserForm.resx diff --git a/AdministrationForm.Designer.cs b/AdministrationForm.Designer.cs index 0573f14..28fcfd1 100644 --- a/AdministrationForm.Designer.cs +++ b/AdministrationForm.Designer.cs @@ -34,9 +34,20 @@ this.browseButton = new System.Windows.Forms.Button(); this.pathToJournalTextBox = new System.Windows.Forms.TextBox(); this.usersTabPage = new System.Windows.Forms.TabPage(); + this.usersDataGridView = new System.Windows.Forms.DataGridView(); this.journalTabPage = new System.Windows.Forms.TabPage(); + this.panel1 = new System.Windows.Forms.Panel(); + this.journalDataGridView = new System.Windows.Forms.DataGridView(); + this.editUserButton = new System.Windows.Forms.Button(); + this.panel2 = new System.Windows.Forms.Panel(); + this.deleteUserButton = new System.Windows.Forms.Button(); this.mainTabControl.SuspendLayout(); this.mainSettingsTabPage.SuspendLayout(); + this.usersTabPage.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.usersDataGridView)).BeginInit(); + this.journalTabPage.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.journalDataGridView)).BeginInit(); + this.panel2.SuspendLayout(); this.SuspendLayout(); // // mainTabControl @@ -93,6 +104,8 @@ // // usersTabPage // + this.usersTabPage.Controls.Add(this.usersDataGridView); + this.usersTabPage.Controls.Add(this.panel2); this.usersTabPage.Location = new System.Drawing.Point(4, 22); this.usersTabPage.Name = "usersTabPage"; this.usersTabPage.Padding = new System.Windows.Forms.Padding(3); @@ -101,14 +114,73 @@ this.usersTabPage.Text = "Пользователи"; this.usersTabPage.UseVisualStyleBackColor = true; // + // usersDataGridView + // + this.usersDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.usersDataGridView.Dock = System.Windows.Forms.DockStyle.Fill; + this.usersDataGridView.Location = new System.Drawing.Point(3, 31); + this.usersDataGridView.Name = "usersDataGridView"; + this.usersDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.usersDataGridView.Size = new System.Drawing.Size(786, 390); + this.usersDataGridView.TabIndex = 0; + // // journalTabPage // + this.journalTabPage.Controls.Add(this.panel1); + this.journalTabPage.Controls.Add(this.journalDataGridView); this.journalTabPage.Location = new System.Drawing.Point(4, 22); this.journalTabPage.Name = "journalTabPage"; this.journalTabPage.Size = new System.Drawing.Size(792, 424); this.journalTabPage.TabIndex = 2; this.journalTabPage.Text = "Журнал"; this.journalTabPage.UseVisualStyleBackColor = true; + this.journalTabPage.Click += new System.EventHandler(this.journalTabPage_Click); + // + // panel1 + // + this.panel1.Dock = System.Windows.Forms.DockStyle.Right; + this.panel1.Location = new System.Drawing.Point(497, 0); + this.panel1.Name = "panel1"; + this.panel1.Size = new System.Drawing.Size(295, 424); + this.panel1.TabIndex = 1; + // + // journalDataGridView + // + this.journalDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.journalDataGridView.Dock = System.Windows.Forms.DockStyle.Fill; + this.journalDataGridView.Location = new System.Drawing.Point(0, 0); + this.journalDataGridView.Name = "journalDataGridView"; + this.journalDataGridView.Size = new System.Drawing.Size(792, 424); + this.journalDataGridView.TabIndex = 0; + // + // editUserButton + // + this.editUserButton.Location = new System.Drawing.Point(3, 3); + this.editUserButton.Name = "editUserButton"; + this.editUserButton.Size = new System.Drawing.Size(131, 20); + this.editUserButton.TabIndex = 1; + this.editUserButton.Text = "Редактировать"; + this.editUserButton.UseVisualStyleBackColor = true; + this.editUserButton.Click += new System.EventHandler(this.editUserButton_Click); + // + // panel2 + // + this.panel2.Controls.Add(this.deleteUserButton); + this.panel2.Controls.Add(this.editUserButton); + this.panel2.Dock = System.Windows.Forms.DockStyle.Top; + this.panel2.Location = new System.Drawing.Point(3, 3); + this.panel2.Name = "panel2"; + this.panel2.Size = new System.Drawing.Size(786, 28); + this.panel2.TabIndex = 2; + // + // deleteUserButton + // + this.deleteUserButton.Location = new System.Drawing.Point(140, 3); + this.deleteUserButton.Name = "deleteUserButton"; + this.deleteUserButton.Size = new System.Drawing.Size(131, 20); + this.deleteUserButton.TabIndex = 2; + this.deleteUserButton.Text = "Удалить"; + this.deleteUserButton.UseVisualStyleBackColor = true; // // AdministrationForm // @@ -122,6 +194,11 @@ this.mainTabControl.ResumeLayout(false); this.mainSettingsTabPage.ResumeLayout(false); this.mainSettingsTabPage.PerformLayout(); + this.usersTabPage.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.usersDataGridView)).EndInit(); + this.journalTabPage.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.journalDataGridView)).EndInit(); + this.panel2.ResumeLayout(false); this.ResumeLayout(false); } @@ -135,5 +212,11 @@ private System.Windows.Forms.Label label1; private System.Windows.Forms.Button browseButton; private System.Windows.Forms.TextBox pathToJournalTextBox; + private System.Windows.Forms.DataGridView usersDataGridView; + private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.DataGridView journalDataGridView; + private System.Windows.Forms.Panel panel2; + private System.Windows.Forms.Button deleteUserButton; + private System.Windows.Forms.Button editUserButton; } } \ No newline at end of file diff --git a/AdministrationForm.cs b/AdministrationForm.cs index b08089a..b05cdcf 100644 --- a/AdministrationForm.cs +++ b/AdministrationForm.cs @@ -30,6 +30,12 @@ namespace AwesomeEmailExtractor var Row = reader.Read(); pathToJournalTextBox.Text = reader.GetString(0); + + AdminUtils adminUtils = new AdminUtils(Globals.currentUser); + + var users = adminUtils.GetAllUsers(); + + usersDataGridView.DataSource = users; } private void browseButton_Click(object sender, EventArgs e) @@ -49,5 +55,30 @@ namespace AwesomeEmailExtractor } } + + private void journalTabPage_Click(object sender, EventArgs e) + { + + } + + private void editUserButton_Click(object sender, EventArgs e) + { + if (usersDataGridView.SelectedRows.Count == 1) + { + var user = usersDataGridView.SelectedRows[0].DataBoundItem as User; + + var form = new EditUserForm(); + form.User = user; + + form.ShowDialog(); + + AdminUtils adminUtils = new AdminUtils(Globals.currentUser); + var users = adminUtils.GetAllUsers(); + usersDataGridView.DataSource = users; + } else + { + MessageBox.Show("Выберите 1 пользователя для редактирования!"); + } + } } } diff --git a/Authorization.cs b/Authorization.cs index deec387..1b8bd7c 100644 --- a/Authorization.cs +++ b/Authorization.cs @@ -86,7 +86,14 @@ namespace AwesomeEmailExtractor Login = login; Role = role; } - + + public User(User user) + { + ID = user.ID; + Login = user.Login; + Role = user.Role; + } + public void Delete() { SqliteCommand command = new SqliteCommand(); @@ -130,11 +137,6 @@ namespace AwesomeEmailExtractor } public void setRole(string login, UserRoles role) { - if (User.Role != UserRoles.ADMIN) - { - throw new Exception("Недостаточно прав!"); - } - SqliteCommand command = new SqliteCommand(); command.Connection = Globals.db; command.CommandText = "UPDATE users SET role_id = @role WHERE login = @login"; @@ -147,7 +149,6 @@ namespace AwesomeEmailExtractor command.ExecuteNonQuery(); } - public void deleteUser(string login) { if (User.Role != UserRoles.ADMIN) @@ -164,7 +165,7 @@ namespace AwesomeEmailExtractor command.ExecuteNonQuery(); } - public List getAllUsers() + public List GetAllUsers() { if (User.Role != UserRoles.ADMIN) { @@ -173,7 +174,7 @@ namespace AwesomeEmailExtractor SqliteCommand command = new SqliteCommand(); command.Connection = Globals.db; - command.CommandText = "SELECT * FROM users"; + command.CommandText = "SELECT id, login, role_id FROM users"; SqliteDataReader reader = command.ExecuteReader(); @@ -186,5 +187,34 @@ namespace AwesomeEmailExtractor return users; } + + public void editUser(User user) + { + if (User.Role != UserRoles.ADMIN) + { + throw new Exception("Недостаточно прав!"); + } + + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "UPDATE users SET login = @login, role_id = @role_id WHERE id = @id"; + + SqliteParameter idParam = new SqliteParameter("@id", user.ID); + command.Parameters.Add(idParam); + + SqliteParameter loginParam = new SqliteParameter("@login", user.Login); + command.Parameters.Add(loginParam); + + SqliteParameter roleParam = new SqliteParameter("@role_id", user.Role); + command.Parameters.Add(roleParam); + + command.ExecuteNonQuery(); + } + + public void editUser(User user, string password) + { + editUser(user); + user.ChangePassword(password); + } } } diff --git a/AwesomeEmailExtractor.csproj b/AwesomeEmailExtractor.csproj index 488936e..aabfb10 100644 --- a/AwesomeEmailExtractor.csproj +++ b/AwesomeEmailExtractor.csproj @@ -89,6 +89,12 @@ AuthorizationForm.cs + + Form + + + EditUserForm.cs + @@ -125,6 +131,9 @@ AuthorizationForm.cs + + EditUserForm.cs + JournalForm.cs diff --git a/EditUserForm.Designer.cs b/EditUserForm.Designer.cs new file mode 100644 index 0000000..9901122 --- /dev/null +++ b/EditUserForm.Designer.cs @@ -0,0 +1,154 @@ +namespace AwesomeEmailExtractor +{ + partial class EditUserForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.idTextBox = new System.Windows.Forms.TextBox(); + this.loginTextBox = new System.Windows.Forms.TextBox(); + this.passwordTextBox = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.submitButton = new System.Windows.Forms.Button(); + this.roleComboBox = new System.Windows.Forms.ComboBox(); + this.label4 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // idTextBox + // + this.idTextBox.Enabled = false; + this.idTextBox.Location = new System.Drawing.Point(65, 8); + this.idTextBox.Name = "idTextBox"; + this.idTextBox.Size = new System.Drawing.Size(129, 20); + this.idTextBox.TabIndex = 0; + // + // loginTextBox + // + this.loginTextBox.Location = new System.Drawing.Point(65, 34); + this.loginTextBox.Name = "loginTextBox"; + this.loginTextBox.Size = new System.Drawing.Size(129, 20); + this.loginTextBox.TabIndex = 1; + // + // passwordTextBox + // + this.passwordTextBox.Location = new System.Drawing.Point(65, 60); + this.passwordTextBox.Name = "passwordTextBox"; + this.passwordTextBox.Size = new System.Drawing.Size(129, 20); + this.passwordTextBox.TabIndex = 2; + this.passwordTextBox.UseSystemPasswordChar = true; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(14, 11); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(21, 13); + this.label1.TabIndex = 3; + this.label1.Text = "ID:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(14, 37); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(41, 13); + this.label2.TabIndex = 4; + this.label2.Text = "Логин:"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(14, 63); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(48, 13); + this.label3.TabIndex = 5; + this.label3.Text = "Пароль:"; + // + // submitButton + // + this.submitButton.Location = new System.Drawing.Point(17, 118); + this.submitButton.Name = "submitButton"; + this.submitButton.Size = new System.Drawing.Size(177, 23); + this.submitButton.TabIndex = 6; + this.submitButton.Text = "Подтвердить"; + this.submitButton.UseVisualStyleBackColor = true; + this.submitButton.Click += new System.EventHandler(this.submitButton_Click); + // + // roleComboBox + // + this.roleComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.roleComboBox.FormattingEnabled = true; + this.roleComboBox.Location = new System.Drawing.Point(65, 86); + this.roleComboBox.Name = "roleComboBox"; + this.roleComboBox.Size = new System.Drawing.Size(129, 21); + this.roleComboBox.TabIndex = 7; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(17, 89); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(35, 13); + this.label4.TabIndex = 8; + this.label4.Text = "Роль:"; + // + // EditUserForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(210, 153); + this.Controls.Add(this.label4); + this.Controls.Add(this.roleComboBox); + this.Controls.Add(this.submitButton); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.passwordTextBox); + this.Controls.Add(this.loginTextBox); + this.Controls.Add(this.idTextBox); + this.Name = "EditUserForm"; + this.Text = "Редактирование"; + this.Load += new System.EventHandler(this.EditUserForm_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox idTextBox; + private System.Windows.Forms.TextBox loginTextBox; + private System.Windows.Forms.TextBox passwordTextBox; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Button submitButton; + private System.Windows.Forms.ComboBox roleComboBox; + private System.Windows.Forms.Label label4; + } +} \ No newline at end of file diff --git a/EditUserForm.cs b/EditUserForm.cs new file mode 100644 index 0000000..73024ba --- /dev/null +++ b/EditUserForm.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AwesomeEmailExtractor +{ + public partial class EditUserForm : Form + { + public User User { get; set; } + + public EditUserForm() + { + InitializeComponent(); + } + + + + private void EditUserForm_Load(object sender, EventArgs e) + { + idTextBox.Text = User.ID.ToString(); + loginTextBox.Text = User.Login; + + + roleComboBox.Items.Add(UserRoles.DEFAULT.ToString()); + roleComboBox.Items.Add(UserRoles.ADMIN.ToString()); + + roleComboBox.SelectedIndex = (int)User.Role; + } + + private void submitButton_Click(object sender, EventArgs e) + { + AdminUtils adminUtils = new AdminUtils(Globals.currentUser); + + User editedUser = new User(User.ID, loginTextBox.Text, (UserRoles)roleComboBox.SelectedIndex); + + if (passwordTextBox.Text != "") + { + adminUtils.editUser(editedUser, passwordTextBox.Text); + } + else + { + + adminUtils.editUser(editedUser); + } + + this.Close(); + } + } +} diff --git a/EditUserForm.resx b/EditUserForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/EditUserForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file