| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.ComponentModel;
|
|---|
| 4 | using System.Data;
|
|---|
| 5 | using System.Data.SqlClient;
|
|---|
| 6 | using System.Drawing;
|
|---|
| 7 | using System.Text;
|
|---|
| 8 | using System.Windows.Forms;
|
|---|
| 9 | using System.Configuration;
|
|---|
| 10 |
|
|---|
| 11 | namespace Baza_Reklam
|
|---|
| 12 | {
|
|---|
| 13 | public partial class Logowanie : Form
|
|---|
| 14 | {
|
|---|
| 15 |
|
|---|
| 16 | #region Fields (2)
|
|---|
| 17 |
|
|---|
| 18 | private bool loginResult = false;
|
|---|
| 19 | private SqlConnectionStringBuilder sqlBldr = new SqlConnectionStringBuilder();
|
|---|
| 20 |
|
|---|
| 21 | #endregion Fields
|
|---|
| 22 |
|
|---|
| 23 | #region Constructors (1)
|
|---|
| 24 |
|
|---|
| 25 | public Logowanie()
|
|---|
| 26 | {
|
|---|
| 27 | InitializeComponent();
|
|---|
| 28 |
|
|---|
| 29 | label4.Text = "Wersja " + Application.ProductVersion;
|
|---|
| 30 | //+ Baza_Reklam.Classes.Version.nrWersji;
|
|---|
| 31 |
|
|---|
| 32 | foreach (ConnectionStringSettings s2 in ConfigurationManager.ConnectionStrings)
|
|---|
| 33 | {
|
|---|
| 34 | bazyComboBox.Items.Add(s2.Name);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | bazyComboBox.SelectedIndex = 0;
|
|---|
| 38 |
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | #endregion Constructors
|
|---|
| 42 |
|
|---|
| 43 | #region Methods (6)
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | // Private Methods (6)
|
|---|
| 47 |
|
|---|
| 48 | private void button1_Click(object sender, EventArgs e)
|
|---|
| 49 | {
|
|---|
| 50 | if (textBox1.Text == "") { return; }
|
|---|
| 51 | if (textBox2.Text == "") { return; }
|
|---|
| 52 |
|
|---|
| 53 | komunikatLabel.Text = "";
|
|---|
| 54 |
|
|---|
| 55 | this.Cursor = Cursors.WaitCursor;
|
|---|
| 56 | loginResult = loginTest();
|
|---|
| 57 | this.Cursor = Cursors.Default;
|
|---|
| 58 |
|
|---|
| 59 | if (loginResult)
|
|---|
| 60 | {
|
|---|
| 61 | this.Cursor = Cursors.WaitCursor;
|
|---|
| 62 |
|
|---|
| 63 | if (sprawdzWersje())
|
|---|
| 64 | {
|
|---|
| 65 | if (Login())
|
|---|
| 66 | {
|
|---|
| 67 | this.DialogResult = DialogResult.OK;
|
|---|
| 68 | }
|
|---|
| 69 | else
|
|---|
| 70 | {
|
|---|
| 71 | komunikatLabel.Text = "Konto nieaktywne lub brak uprawnień.";
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | else
|
|---|
| 75 | {
|
|---|
| 76 | this.Close();
|
|---|
| 77 | return;
|
|---|
| 78 | }
|
|---|
| 79 | this.Cursor = Cursors.Default;
|
|---|
| 80 |
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
|
|---|
| 85 | {
|
|---|
| 86 | linkLabel1.LinkVisited = true;
|
|---|
| 87 |
|
|---|
| 88 | System.Diagnostics.ProcessStartInfo n = new System.Diagnostics.ProcessStartInfo();
|
|---|
| 89 | n.CreateNoWindow = true;
|
|---|
| 90 | n.FileName = "www.infocity.pl/baza_reklam/instrukcja.htm";
|
|---|
| 91 | System.Diagnostics.Process.Start(n);
|
|---|
| 92 |
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | private bool Login()
|
|---|
| 96 | {
|
|---|
| 97 | //textBox1.Text = "krzyzaniak";
|
|---|
| 98 |
|
|---|
| 99 | ConnString.getConnString().Value = sqlBldr.ConnectionString;
|
|---|
| 100 |
|
|---|
| 101 | SqlConnection conn = new SqlConnection(sqlBldr.ConnectionString);
|
|---|
| 102 |
|
|---|
| 103 | /*
|
|---|
| 104 | Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
|---|
| 105 |
|
|---|
| 106 | _config.ConnectionStrings.ConnectionStrings["Baza_Reklam.Properties.Settings.testowe"].ConnectionString = sqlBldr.ConnectionString;
|
|---|
| 107 |
|
|---|
| 108 | _config.Save(ConfigurationSaveMode.Modified);
|
|---|
| 109 |
|
|---|
| 110 | ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name);
|
|---|
| 111 |
|
|---|
| 112 | Properties.Settings.Default.Reload();
|
|---|
| 113 |
|
|---|
| 114 | */
|
|---|
| 115 |
|
|---|
| 116 | //zczytyje prawa uzytkownika
|
|---|
| 117 |
|
|---|
| 118 | SqlCommand command = new SqlCommand();
|
|---|
| 119 |
|
|---|
| 120 | command.CommandType = CommandType.Text;
|
|---|
| 121 | command.CommandText = "select * from UZYTKOWNICY where LOGIN = @login";
|
|---|
| 122 | command.Parameters.AddWithValue("@login", textBox1.Text);
|
|---|
| 123 | command.Connection = conn;
|
|---|
| 124 |
|
|---|
| 125 | conn.Open();
|
|---|
| 126 |
|
|---|
| 127 | SqlDataReader reader = command.ExecuteReader();
|
|---|
| 128 |
|
|---|
| 129 | User.getUser().Login = textBox1.Text;
|
|---|
| 130 | User.getUser().Password = textBox2.Text;
|
|---|
| 131 |
|
|---|
| 132 | if (reader.HasRows)
|
|---|
| 133 | {
|
|---|
| 134 | while (reader.Read())
|
|---|
| 135 | {
|
|---|
| 136 | User.getUser().Imie = reader.GetValue(2).ToString();
|
|---|
| 137 | User.getUser().Nazwisko = reader.GetValue(3).ToString();
|
|---|
| 138 | User.getUser().St_sekretarka = (bool)reader.GetValue(5);
|
|---|
| 139 | User.getUser().St_handlowiec = (bool)reader.GetValue(6);
|
|---|
| 140 | User.getUser().St_subhandlowiec = (bool)reader.GetValue(7);
|
|---|
| 141 | User.getUser().St_kierownik = (bool)reader.GetValue(8);
|
|---|
| 142 | User.getUser().St_dtp = (bool)reader.GetValue(9);
|
|---|
| 143 | User.getUser().St_korekta = (bool)reader.GetValue(10);
|
|---|
| 144 | User.getUser().St_produkcja = (bool)reader.GetValue(11);
|
|---|
| 145 | User.getUser().St_admin = (bool)reader.GetValue(12);
|
|---|
| 146 | }
|
|---|
| 147 | conn.Close();
|
|---|
| 148 | } else
|
|---|
| 149 | { return false; }
|
|---|
| 150 |
|
|---|
| 151 | //jezeli handlowiec to sciaga dane agenta
|
|---|
| 152 |
|
|---|
| 153 | if (User.getUser().St_handlowiec|User.getUser().St_kierownik|User.getUser().St_subhandlowiec|User.getUser().St_sekretarka)
|
|---|
| 154 | {
|
|---|
| 155 | command.CommandText = "select A.ID_AGENCJI,A.Symbol, A.F_ROZ, A2.miasto, A2.telefon, A2.fax, A2.Adres_Kor,A2.Symbol from AGENCI ";
|
|---|
| 156 | command.CommandText += "as A left join dbo.AGENCJE as A2 on A.[id_agencji]=A2.[id_agencji] where A.Symbol = @login";
|
|---|
| 157 |
|
|---|
| 158 | command.Connection = conn;
|
|---|
| 159 |
|
|---|
| 160 | conn.Open();
|
|---|
| 161 |
|
|---|
| 162 | reader = command.ExecuteReader();
|
|---|
| 163 | while (reader.Read())
|
|---|
| 164 | {
|
|---|
| 165 | User.getUser().IdAgencji = (int)reader.GetValue(0);
|
|---|
| 166 | User.getUser().Symbol_agenta = reader.GetValue(1).ToString();
|
|---|
| 167 | User.getUser().Kod_agenta = reader.IsDBNull(2) ? "" : reader.GetValue(2).ToString();
|
|---|
| 168 | User.getUser().Miasto = reader.IsDBNull(3) ? "" : reader.GetValue(3).ToString();
|
|---|
| 169 | User.getUser().TelSekretariat = reader.IsDBNull(4) ? "" : reader.GetValue(4).ToString();
|
|---|
| 170 | User.getUser().FaxSekretariat = reader.IsDBNull(5) ? "" : reader.GetValue(5).ToString();
|
|---|
| 171 | User.getUser().AdresBiura = reader.IsDBNull(6) ? "" : reader.GetValue(6).ToString();
|
|---|
| 172 | User.getUser().SymbolAgencji = reader.IsDBNull(7) ? "" : reader.GetValue(7).ToString();
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | conn.Close();
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | return true;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | private bool loginTest() {
|
|---|
| 182 |
|
|---|
| 183 | sqlBldr.ConnectionString= ConfigurationManager.ConnectionStrings[bazyComboBox.SelectedItem.ToString()].ConnectionString;
|
|---|
| 184 |
|
|---|
| 185 | /*
|
|---|
| 186 | sqlBldr.DataSource = Baza_Reklam.Properties.Settings.Default.SERVER;
|
|---|
| 187 | sqlBldr.InitialCatalog = Baza_Reklam.Properties.Settings.Default.DATABASE;
|
|---|
| 188 | */
|
|---|
| 189 |
|
|---|
| 190 | sqlBldr.UserID = textBox1.Text;
|
|---|
| 191 | sqlBldr.Password = textBox2.Text;
|
|---|
| 192 | // sqlBldr.IntegratedSecurity = true;
|
|---|
| 193 | sqlBldr.ConnectTimeout = 200000;
|
|---|
| 194 |
|
|---|
| 195 | SqlConnection conn = new SqlConnection(sqlBldr.ConnectionString);
|
|---|
| 196 |
|
|---|
| 197 | try
|
|---|
| 198 | {
|
|---|
| 199 | conn.Open();
|
|---|
| 200 | komunikatLabel.Text = "OK";
|
|---|
| 201 | ConnString.getConnString().Value = sqlBldr.ConnectionString;
|
|---|
| 202 | conn.Close();
|
|---|
| 203 | return true;
|
|---|
| 204 | }
|
|---|
| 205 | catch (SqlException e1) {
|
|---|
| 206 |
|
|---|
| 207 | switch (e1.Number) {
|
|---|
| 208 | case 18456:
|
|---|
| 209 | komunikatLabel.Text = "Nieprawidłowy login lub hasło";
|
|---|
| 210 | break;
|
|---|
| 211 | default:
|
|---|
| 212 | komunikatLabel.Text = "Brak dostępu do bazy";
|
|---|
| 213 | MessageBox.Show(e1.Message);
|
|---|
| 214 | break;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | return false;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | private void Logowanie_KeyPress(object sender, KeyPressEventArgs e)
|
|---|
| 221 | {
|
|---|
| 222 | if (e.KeyChar == 13)
|
|---|
| 223 | {
|
|---|
| 224 | button1.PerformClick();
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | private bool sprawdzWersje()
|
|---|
| 229 | {
|
|---|
| 230 | SqlCommand cmd = new SqlCommand("SELECT TOP 1 current_version FROM Config2");
|
|---|
| 231 | cmd.Connection = new SqlConnection(ConnString.getConnString().Value);
|
|---|
| 232 |
|
|---|
| 233 | string ver;
|
|---|
| 234 |
|
|---|
| 235 | cmd.Connection.Open();
|
|---|
| 236 | ver = (string)cmd.ExecuteScalar();
|
|---|
| 237 | cmd.Connection.Close();
|
|---|
| 238 |
|
|---|
| 239 | if (ver.Trim() != Application.ProductVersion)
|
|---|
| 240 | {
|
|---|
| 241 | FormBadVer frm = new FormBadVer();
|
|---|
| 242 | frm.labelVer.Text += Application.ProductVersion;
|
|---|
| 243 | frm.labelSrvVer.Text += ver;
|
|---|
| 244 | frm.ShowDialog();
|
|---|
| 245 | return false;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | return true;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | #endregion Methods
|
|---|
| 253 |
|
|---|
| 254 | }
|
|---|
| 255 | } |
|---|