добавляет редактирование пользователя

This commit is contained in:
Maxim Slipenko 2022-05-24 09:09:56 +03:00
parent d386e8338a
commit 4527f07c89
Signed by: Maks1mS
GPG Key ID: 7461AF39A8705FB8
7 changed files with 491 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -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 пользователя для редактирования!");
}
}
}
}

View File

@ -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<User> getAllUsers()
public List<User> 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);
}
}
}

View File

@ -89,6 +89,12 @@
<Compile Include="AuthorizationForm.Designer.cs">
<DependentUpon>AuthorizationForm.cs</DependentUpon>
</Compile>
<Compile Include="EditUserForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="EditUserForm.Designer.cs">
<DependentUpon>EditUserForm.cs</DependentUpon>
</Compile>
<Compile Include="ExtactEmailsAlgorithm.cs" />
<Compile Include="FormManager.cs" />
<Compile Include="Globals.cs" />
@ -125,6 +131,9 @@
<EmbeddedResource Include="AuthorizationForm.resx">
<DependentUpon>AuthorizationForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="EditUserForm.resx">
<DependentUpon>EditUserForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="JournalForm.resx">
<DependentUpon>JournalForm.cs</DependentUpon>
</EmbeddedResource>

154
EditUserForm.Designer.cs generated Normal file
View File

@ -0,0 +1,154 @@
namespace AwesomeEmailExtractor
{
partial class EditUserForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

55
EditUserForm.cs Normal file
View File

@ -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();
}
}
}

120
EditUserForm.resx Normal file
View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>