Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using System.IO;
  10. using System.Resources;
  11. using Npgsql;
  12. namespace Rekod
  13. {
  14.     public partial class Login : Form
  15.     {
  16.         XmlDocument xml = new XmlDocument();
  17.         XmlNode node;
  18.         public NpgsqlConnection conn;
  19.         private NpgsqlCommand qwCmd;
  20.         private NpgsqlDataReader zxReader;
  21.         string base_text = "";
  22.         string host_text = "";
  23.         string port_text = "";
  24.  
  25.         public int id;
  26.         public string nameuser, connStr;
  27.         public bool close = false, admin = false, visible_noactive = false;
  28.         public Login()
  29.         {
  30.             InitializeComponent();
  31.             qwCmd = new NpgsqlCommand();
  32.         }
  33.         public Login(string user, string pass, string host, string port, string baza)
  34.         {
  35.  
  36.             InitializeComponent();
  37.             comboBox2.Text = user;
  38.             textBox1.Text = pass;
  39.             base_text = baza;
  40.             host_text = host;
  41.             port_text = port;
  42.             comboBox1.Text = baza + "@" + host + ":" + port;
  43.             Program.ipString = host;
  44.             Program.dbString = baza;
  45.             Program.portString = port;
  46.             qwCmd = new NpgsqlCommand();
  47.             conn = getConn();
  48.             if (conn != null)
  49.             {
  50.                 qwCmd.Connection = conn;
  51.                 getUser();
  52.             }
  53.             else
  54.             {
  55.                 ShowDialog();
  56.             }
  57.         }
  58.         private void eror()
  59.         {
  60.             panel1.Visible = true;
  61.             panel2.Visible = false;
  62.             panel2.Enabled = false;
  63.             panel1.Enabled = true;
  64.  
  65.         }
  66.         private void komponets()
  67.         {
  68.  
  69.             string line = comboBox1.Text;//.ToString();
  70.             char[] sep;
  71.             sep = new char[1];
  72.             sep[0] = '@';
  73.             string[] array_line = line.Split(sep);
  74.             base_text = array_line[0];
  75.  
  76.             try
  77.             {
  78.                 sep[0] = ':';
  79.                 array_line = array_line[1].Split(sep);
  80.                 port_text = array_line[1];
  81.  
  82.             }
  83.             catch
  84.             { }
  85.             finally
  86.             {
  87.                 host_text = array_line[0];
  88.                 if (port_text == "")
  89.                 {
  90.                     port_text = "5432";
  91.                 }
  92.             }
  93.  
  94.         }
  95.  
  96.         private string[] partitionedIntoSubstrings(char SeparatesSymbol, string SharedLine)
  97.         { char[] sep;
  98.                 sep = new char[1];
  99.                 sep[0] = SeparatesSymbol;
  100.              string[] array_line = SharedLine.Split(sep);
  101.             return array_line;
  102.         }
  103.  
  104.         private int[] SelectCountBasePortUser(XmlNode nod, string UserName)
  105.         {
  106.             int schetchik_base = 0;
  107.             int schetchik_port = 0;
  108.             int schetchik_user = 0;
  109.             foreach (XmlNode nod2 in nod.ChildNodes)
  110.             {
  111.                 if (nod2.Name == "dataBase")
  112.                 {
  113.                     if (nod2.FirstChild.Value == base_text)
  114.                     {
  115.                         schetchik_base++;
  116.                     }
  117.                 }
  118.                 if (nod2.Name == "users")
  119.                 {
  120.                     if (nod2.FirstChild.Value == UserName && UserName != "")
  121.                     {
  122.                         schetchik_user++;
  123.                     }
  124.                 }
  125.                 if (nod2.Name == "port")
  126.                 {
  127.                     if (nod2.FirstChild != null)
  128.                     {
  129.                         if (nod2.FirstChild.Value == port_text)
  130.                         {
  131.                             schetchik_port++;
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.             int[] Mas = {schetchik_base, schetchik_user,schetchik_port};
  137.             return Mas;
  138.         }
  139.         private void insert_xml(string StringConnect, string UserName)
  140.         {
  141.             if (StringConnect != "")
  142.             {
  143.               string base_text = partitionedIntoSubstrings('@', StringConnect)[0];
  144.               string host_text = partitionedIntoSubstrings(':', partitionedIntoSubstrings('@', StringConnect)[1])[0];
  145.                 try
  146.                 {
  147.                     string port_text = partitionedIntoSubstrings(':', partitionedIntoSubstrings('@', StringConnect)[1])[1];
  148.                 }
  149.                 catch
  150.                 {
  151.                     port_text = "";
  152.                 }
  153.                 string user_text = "";
  154.              
  155.                 int schetchik_host = 0;
  156.  
  157.                 foreach (XmlNode nod in node.ChildNodes)
  158.                 {
  159.                     if (host_text == nod.FirstChild.Value)
  160.                     {
  161.                         schetchik_host++;
  162.  
  163.                       int[] countSchetchik=  SelectCountBasePortUser(nod, UserName);
  164.                       AddXml(countSchetchik, nod, port_text);
  165.                        
  166.                     }
  167.                 }
  168.                 if (schetchik_host == 0)
  169.                 {
  170.                     XmlElement user = xml.CreateElement("users");
  171.                     user.InnerText = user_text;
  172.  
  173.                     XmlElement port = xml.CreateElement("port");
  174.                     port.InnerText = port_text;
  175.  
  176.                     XmlElement nameBase = xml.CreateElement("dataBase");
  177.                     nameBase.InnerText = base_text;
  178.  
  179.                     XmlElement host = xml.CreateElement("host");
  180.                     host.InnerText = host_text;
  181.  
  182.                     host.AppendChild(nameBase);
  183.                     host.AppendChild(user);
  184.                     if (port_text != "")
  185.                     {
  186.                         host.AppendChild(port);
  187.                     }
  188.                     node.InsertAfter(host, node.FirstChild);
  189.                     try
  190.                     {
  191.                         xml.Save(Program.path_string + "\\" + Program.setting_file);
  192.                     }
  193.                     catch
  194.                     {
  195.                         MessageBox.Show("Не найдена конфигурация!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  196.                     }
  197.                 }
  198.             }
  199.         }
  200.         private void AddXml(int[] countSchetchik,XmlNode nod, string port_text )
  201.         {
  202.  
  203.             string user_text = "";
  204.             bool change = false;
  205.             int schetchik_base = countSchetchik[0];
  206.             int schetchik_port = countSchetchik[1];
  207.             int schetchik_user = countSchetchik[2];
  208.             if (schetchik_user + schetchik_port + schetchik_base != 0)
  209.             {
  210.                 if (schetchik_base == 0)
  211.                 {
  212.                     XmlElement nameBase = xml.CreateElement("dataBase");
  213.                     nameBase.InnerText = base_text;
  214.                     nod.InsertAfter(nameBase, nod["users"]);
  215.                     change = true;
  216.                 }
  217.                 if (schetchik_port == 0)
  218.                 {
  219.                     if (port_text != "" && port_text != " " && port_text != null)
  220.                     {
  221.                         XmlElement port = xml.CreateElement("port");
  222.                         port.InnerText = port_text;
  223.                         nod.InsertAfter(port, nod["users"]);
  224.                         change = true;
  225.                     }
  226.                 }
  227.                 if (schetchik_user == 0)
  228.                 {
  229.                     XmlElement user = xml.CreateElement("users");
  230.                     user.InnerText = user_text;
  231.  
  232.                     nod.InsertAfter(user, nod["users"]);
  233.                     change = true;
  234.                 }
  235.                 try
  236.                 {
  237.                     if (change)
  238.                     {
  239.                         xml.Save(Program.path_string + "\\" + Program.setting_file);
  240.                     }
  241.                 }
  242.                 catch
  243.                 {
  244.                     MessageBox.Show("Ошибка сохранения файла настроек! " +
  245.                         "Возможно у вас недостаточно прав доступа к рабочей папке!",
  246.                         "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  247.                 }
  248.             }
  249.         }
  250.         private void fill_combobox()
  251.         {
  252.  
  253.             string line = "";
  254.             foreach (XmlNode nod1 in node.ChildNodes)
  255.             {
  256.                 line = "@" + nod1.FirstChild.Value;
  257.                 foreach (XmlNode nod2 in nod1.ChildNodes)
  258.                 {
  259.                     if (nod2.Name == "dataBase")
  260.                     {
  261.                         string line_name = line;
  262.                         line = nod2.InnerText + line;
  263.                         int i = 0;
  264.                         foreach (XmlNode port in nod1.ChildNodes)
  265.                         {
  266.  
  267.                             if (port.Name == "port")
  268.                             {
  269.                                 if (port.InnerText != "")
  270.                                 {
  271.                                     string line_port = line + ":" + port.InnerText;
  272.                                     comboBox1.Items.Add(line_port);
  273.                                     i++;
  274.                                 }
  275.                                 else
  276.                                 {
  277.                                     if (i == 0)
  278.                                     {
  279.                                         comboBox1.Items.Add(line);
  280.                                         i++;
  281.                                     }
  282.                                 }
  283.  
  284.                             }
  285.                             else
  286.                             {
  287.                                 if (i == 0)
  288.                                 {
  289.                                     comboBox1.Items.Add(line);
  290.                                     i++;
  291.                                 }
  292.                             }
  293.  
  294.  
  295.                         }
  296.                         line = line_name;
  297.                     }
  298.                 }
  299.             }
  300.         }
  301.  
  302.         private void Login_Load(object sender, EventArgs e)
  303.         {
  304.  
  305.  
  306.             System.Resources.ResourceManager resources = new System.Resources.ResourceManager("Images", this.GetType().Assembly);
  307.  
  308.  
  309. #if gs        
  310.            pictureBox1.Location = new System.Drawing.Point(26, 23);
  311.            this.pictureBox1.Image = global::Rekod.Properties.Resources.a_c48d37de1;
  312.            this.Text = "ООО \"Градосервис\" MapEditor";
  313. #endif
  314. #if rls
  315.             this.pictureBox1.Image = global::Rekod.Properties.Resources.upload_logotip_cku22;
  316.             this.Text = "ОАО \"НПК РЕКОД\" РЕКОД Инфраструктура";
  317. #endif
  318.            try
  319.            {
  320.                xml.Load(Program.path_string + "\\" + Program.setting_file);
  321.                node = xml.DocumentElement;
  322.                fill_combobox();
  323.            }
  324.            catch
  325.            {
  326.  
  327.                MessageBox.Show("Не найдена конфигурация!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  328.                OpenFileDialog openFile = new OpenFileDialog();
  329.                openFile.Title = "Укажите файл конфигурации!";
  330.                openFile.FileName = "settings.aews";
  331.                openFile.Filter = "Файл конфигурации *.aews|*.aews";
  332.                if (openFile.ShowDialog() == DialogResult.OK)
  333.                {
  334.                        //Program.path_string = Environment.CurrentDirectory;
  335.                        try
  336.                        {
  337.                            Program.path_string = System.IO.Path.GetDirectoryName(openFile.FileName);
  338.                            xml.Load(openFile.FileName);
  339.                            node = xml.DocumentElement;
  340.                            Program.setting_file = openFile.SafeFileName;
  341.                            fill_combobox();
  342.                        }
  343.                        catch
  344.                        {
  345.                            MessageBox.Show("Данный файл не является файлом конфигурации!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  346.                            Application.ExitThread();
  347.                        }
  348.                }
  349.                else
  350.                {
  351.                    Application.ExitThread();
  352.                }
  353.            }
  354.         }
  355.  
  356.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  357.         {
  358.             comboBox2.Items.Clear();
  359.             string name_dirty = comboBox1.SelectedItem.ToString();
  360.             char[] sep;
  361.             sep = new char[1];
  362.             sep[0] = '@';
  363.             string[] name_frash = name_dirty.Split(sep);
  364.             foreach (XmlNode nod1 in node.ChildNodes)
  365.             {
  366.                 sep[0] = ':';
  367.                 try
  368.                 {
  369.                     name_frash = name_frash[1].Split(sep);
  370.                 }
  371.                 catch
  372.                 { }
  373.                 if (nod1.FirstChild.Value == name_frash[0])
  374.                 {
  375.                     foreach (XmlNode nod2 in nod1.ChildNodes)
  376.                     {
  377.                         if (nod2.Name == "users")
  378.                         {
  379.                             comboBox2.Items.Add(nod2.InnerText);
  380.  
  381.                         }
  382.                     }
  383.                 }
  384.             }
  385.         }
  386.         private NpgsqlConnection getConn()
  387.         {
  388.             conn = new NpgsqlConnection();
  389.  
  390.             if (comboBox1 != null)
  391.             {
  392.                 komponets();
  393.                 Program.ipString = host_text;
  394.                 Program.dbString = base_text;
  395.                 Program.portString = port_text;
  396.  
  397.                 string constr = "Server=" + host_text + ";Port=" + port_text + ";database=" + base_text +
  398.                     ";User Id=" + comboBox2.Text + "; Password=" + textBox1.Text + ";";
  399.                 conn = new NpgsqlConnection();
  400.                 conn.ConnectionString = constr;
  401.                 connStr = conn.ConnectionString;
  402.                 try
  403.                 {
  404.                     conn.Open();
  405.                 }
  406.                 catch (Exception ex)
  407.                 {
  408.                     if (textBox2.Text == "")
  409.                         textBox2.Text = ex.Message;
  410.                     return null;
  411.                 }
  412.                 qwCmd = new NpgsqlCommand();
  413.                 qwCmd.Connection = conn;
  414.                 Program.conn = conn;
  415.                 Program.sqlCmd = new SQLCommand();
  416.  
  417.                 return conn;
  418.             }
  419.             Program.conn = conn;
  420.             Program.sqlCmd = new SQLCommand();
  421.             return conn;
  422.         }
  423.         private void button1_Click(object sender, EventArgs e)
  424.         {
  425.             bool err = false;
  426.             textBox2.Text = "";
  427.             try
  428.             {
  429.                 qwCmd = new NpgsqlCommand();
  430.                 conn = getConn();
  431.                 if (conn != null)
  432.                 {
  433.                     qwCmd.Connection = conn;
  434.                     insert_xml(comboBox1.Text, comboBox2.Text);
  435.                     getUser();
  436.                 }
  437.                 else
  438.                 {
  439.                     err = true;
  440.                     if (textBox2.Text == "")
  441.                         textBox2.Text = "Отсутствует подключение к базе данных.";
  442.                 }
  443.             }
  444.             catch (Exception exe)
  445.             {
  446.                 err = true;
  447.                 if (textBox2.Text == "")
  448.                     textBox2.Text = exe.ToString();
  449.  
  450.             }
  451.             finally
  452.             {
  453.                 if (textBox2.Text != "")
  454.                     eror();
  455.                 int i = 0;
  456.                 switch (textBox2.Text)
  457.                 {
  458.                     case "Connection string argument missing!\r\nИмя параметра: HOST":
  459.                         textBox2.Text = "Отсутствует адрес сервера в строке соединения.";
  460.                         i++;
  461.                         break;
  462.                     case "Connection string argument missing!\r\nИмя параметра: USER ID":
  463.                         textBox2.Text = "Отсутствует имя пользователя.";
  464.                         i++;
  465.                         break;
  466.                 }
  467.                 if (i == 0)
  468.                 {
  469.                     try
  470.                     {
  471.                         string[] erorr = textBox2.Text.Split(' ');
  472.                         switch (erorr[1])
  473.                         {
  474.                             case "28000:":
  475.                                 textBox2.Text = "Неверный логин, или пароль для пользователя '" + comboBox2.Text + "'";
  476.                                 break;
  477.                             case "3D000:":
  478.                                 textBox2.Text = "Базы данных '" + base_text + "' не существует.";
  479.                                 break;
  480.                         }
  481.                     }
  482.                     catch
  483.                     { }
  484.                 }
  485.             }
  486.             if (!err)
  487.             {
  488.                 Close();
  489.                 close = true;
  490.             }
  491.         }
  492.         private void button3_Click(object sender, EventArgs e)
  493.         {
  494.             close = false;
  495.             if (qwCmd != null)
  496.             {
  497.                 qwCmd.Dispose();
  498.             }
  499.             Close();
  500.         }
  501.         private void label3_Click(object sender, EventArgs e)
  502.         {
  503.  
  504.         }
  505.         private void button2_Click(object sender, EventArgs e)
  506.         {
  507.             close = false;
  508.             if (qwCmd != null)
  509.             {
  510.                 qwCmd.Dispose();
  511.             }
  512.             Close();
  513.         }
  514.         private void button4_Click(object sender, EventArgs e)
  515.         {
  516.             this.Size = new System.Drawing.Size(514, 233);
  517.             textBox2.Size = new System.Drawing.Size(502, 65);
  518.             textBox2.Visible = true;
  519.         }
  520.         public void getUser()
  521.         {
  522.             if (conn == null)
  523.             {
  524.                 getConn();
  525.             }
  526.  
  527.             qwCmd.CommandText = "SELECT id, name_full, admin FROM " + Program.scheme + ".user_db WHERE login = @file_blob AND pass = @file_blob2";
  528.  
  529.             NpgsqlParameter param = new NpgsqlParameter("@file_blob", DbType.String);
  530.             param.Value = comboBox2.Text;
  531.             qwCmd.Parameters.Add(param);
  532.             param = new NpgsqlParameter("@file_blob2", DbType.String);
  533.             param.Value = textBox1.Text;
  534.             qwCmd.Parameters.Add(param);
  535.  
  536.             zxReader = qwCmd.ExecuteReader();
  537.             if (zxReader.Read())
  538.             {
  539.                 id = Convert.ToInt32(zxReader.GetValue(0).ToString());
  540.                 Program.id_user = id;
  541.                 nameuser = zxReader.GetValue(1).ToString();
  542.                 admin = zxReader.GetBoolean(2);
  543.                 zxReader.Close();
  544.                 getInfoSystem();
  545.             }
  546.             else
  547.             {
  548.                 textBox2.Text = "Пользователь " + comboBox2.Text + " не найден!";
  549.                 zxReader.Close();
  550.                 return;
  551.             }
  552.             qwCmd.Dispose();
  553.             close = true;
  554.             Close();
  555.         }
  556.         private void getInfoSystem()
  557.         {
  558.             classesOfMetods cls = new classesOfMetods(conn);
  559.             cls.reloadInfo();
  560.         }
  561.  
  562.         private void textBox2_KeyUp(object sender, KeyEventArgs e)
  563.         {
  564.             if (e.KeyCode == Keys.Enter)
  565.             {
  566.                 button2_Click(button2, null);
  567.             }
  568.         }
  569.  
  570.         private void comboBox1_TextChanged(object sender, EventArgs e)
  571.         {
  572.             comboBox2.Items.Clear();
  573.         }
  574.  
  575.         private void button5_Click(object sender, EventArgs e)
  576.         {
  577.             panel1.Visible = false;
  578.             panel2.Visible = true;
  579.             this.Size = new System.Drawing.Size(496, 161);
  580.             textBox2.Size = new System.Drawing.Size(502, 65);
  581.             textBox2.Visible = false;
  582.             panel2.Enabled = true;
  583.             panel1.Enabled = false;
  584.  
  585.         }
  586.  
  587.  
  588.  
  589.     }
  590. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement