root/tags/BazaReklam_1.2.9/Logowanie.cs

Wersja 936, 10.6 KB (wprowadzona przez sylwek, 16 years temu)

Re #233

Line 
1using System;
2using System.Data.SqlClient;
3using System.Windows.Forms;
4using System.Configuration;
5using Baza_Reklam.Classes.Model;
6
7namespace Baza_Reklam
8{
9    public partial class Logowanie : Form
10    {
11
12        #region Fields (2)
13
14        private bool loginResult;
15        private readonly SqlConnectionStringBuilder sqlBldr = new SqlConnectionStringBuilder();
16
17        #endregion Fields
18
19        #region Constructors (1)
20
21        public Logowanie()
22        {
23            InitializeComponent();
24
25            label4.Text = "Wersja " + Application.ProductVersion;
26
27            foreach (ConnectionStringSettings s2 in ConfigurationManager.ConnectionStrings)
28            {
29                bazyComboBox.Items.Add(s2.Name);
30            }
31
32            bazyComboBox.SelectedIndex = 0;
33        }
34
35        #endregion Constructors
36
37        #region Methods (6)
38
39        private void button1_Click(object sender, EventArgs e)
40        {
41            if (string.IsNullOrEmpty(textBox1.Text.Trim()) || string.IsNullOrEmpty(textBox2.Text.Trim())) return;
42
43            komunikatLabel.Text = string.Empty;
44
45            Cursor = Cursors.WaitCursor;
46            loginResult = LoginTest();
47
48            if (loginResult)
49            {
50                if (SprawdzWersje())
51                {
52                    if (Login())
53                        DialogResult = DialogResult.OK;
54                    else
55                        komunikatLabel.Text = "Konto nieaktywne lub brak uprawnień.";
56                }
57                else
58                {
59                    Close();
60                    return;
61                }
62            }
63
64            Cursor = Cursors.Default;
65        }
66
67        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
68        {
69            linkLabel1.LinkVisited = true;
70
71            System.Diagnostics.ProcessStartInfo n = new System.Diagnostics.ProcessStartInfo();
72            n.CreateNoWindow = true;
73            n.FileName = "www.infocity.pl/baza_reklam/instrukcja.htm";
74            System.Diagnostics.Process.Start(n);
75        }
76
77        private bool Login()
78        {
79            SqlConnection conn = null;
80            SqlCommand command = null;
81            SqlDataReader reader = null;
82            try
83            {
84                ConnString.getConnString().Value = sqlBldr.ConnectionString;
85
86                conn = new SqlConnection(sqlBldr.ConnectionString);
87
88                //sczytyje prawa uzytkownika
89                string query = "SELECT U.*, UA.AgencyId, UA.Name, UA.[Order] FROM dbo.UZYTKOWNICY U LEFT OUTER JOIN dbo.UserAgency UA ON UA.UserId=U.Id WHERE U.LOGIN=@login ORDER BY UA.[Order]";
90                command = new SqlCommand(query, conn);
91                command.Parameters.AddWithValue("@login", textBox1.Text);
92
93                conn.Open();
94
95                reader = command.ExecuteReader();
96
97                if (reader == null || !reader.HasRows) return false;
98
99                User.Instance().Password = textBox2.Text;
100
101                if (reader.Read())
102                {
103                    User.Instance().Id = reader.GetInt32(0);
104                    User.Instance().Imie = reader.GetValue(2).ToString();
105                    User.Instance().Nazwisko = reader.GetValue(3).ToString();
106                    User.Instance().IsSekretarka = (bool) reader.GetValue(5);
107                    User.Instance().IsHandlowiec = (bool) reader.GetValue(6);
108                    User.Instance().IsKsiegowosc = (bool) reader.GetValue(7);
109                    User.Instance().IsKierownik = (bool) reader.GetValue(8);
110                    User.Instance().IsDtp = (bool) reader.GetValue(9);
111                    User.Instance().IsKorekta = (bool) reader.GetValue(10);
112                    User.Instance().IsProdukcja = (bool) reader.GetValue(11);
113                    User.Instance().IsAdmin = (bool) reader.GetValue(12);
114                    if (!reader.IsDBNull(13))
115                    {
116                        User.Instance().UserAgencyList.Add(new UserAgency(reader.GetInt32(0),
117                                                                          reader.GetInt32(13),
118                                                                          reader.GetString(14),
119                                                                          reader.GetInt32(15)));
120                    }
121                }
122
123                while (reader.Read())
124                {
125                    if (reader.IsDBNull(13)) continue;
126
127                    User.Instance().UserAgencyList.Add(new UserAgency(reader.GetInt32(0),
128                                                                      reader.GetInt32(13),
129                                                                      reader.GetString(14),
130                                                                      reader.GetInt32(15)));
131                }
132
133                //jezeli handlowiec to sciaga dane agenta
134                if (User.Instance().IsHandlowiec || User.Instance().IsKierownik || User.Instance().IsSekretarka)
135                {
136                    reader.Close();
137                    reader.Dispose();
138
139                    query = "SELECT A.ID_AGENCJI, A.Symbol, A.F_ROZ, A2.miasto, A2.telefon, A2.fax, A2.Adres_Kor, A2.Symbol, A.InvoiceProviderId, A.Aktywny FROM AGENCI A "
140                            + "LEFT JOIN dbo.AGENCJE A2 ON A.[id_agencji]=A2.[id_agencji] WHERE A.Symbol=@login";
141                   
142                    command.CommandText = query;
143
144                    reader = command.ExecuteReader();
145
146                    if (reader == null || !reader.HasRows) return true;
147
148                    bool isAgentActive = true;
149
150                    while (reader.Read())
151                    {
152                        User.Instance().IdAgencji = (int) reader.GetValue(0);
153                        User.Instance().Symbol_agenta = reader.GetValue(1).ToString();
154                        User.Instance().Kod_agenta = reader.IsDBNull(2) ? "" : reader.GetValue(2).ToString();
155                        User.Instance().Miasto = reader.IsDBNull(3) ? "" : reader.GetValue(3).ToString();
156                        User.Instance().TelSekretariat = reader.IsDBNull(4) ? "" : reader.GetValue(4).ToString();
157                        User.Instance().FaxSekretariat = reader.IsDBNull(5) ? "" : reader.GetValue(5).ToString();
158                        User.Instance().AdresBiura = reader.IsDBNull(6) ? "" : reader.GetValue(6).ToString();
159                        User.Instance().SymbolAgencji = reader.IsDBNull(7) ? "" : reader.GetValue(7).ToString();
160                        User.Instance().InvoiceProviderId = reader.IsDBNull(8) ? 1 : reader.GetInt32(8);
161                        isAgentActive = reader.GetBoolean(9);
162                    }
163
164                    return isAgentActive;
165                }
166
167                return true;
168            }
169            finally
170            {
171                if (reader != null)
172                {
173                    reader.Close();
174                    reader.Dispose();
175                }
176                if (command != null) command.Dispose();
177
178                if (conn != null)
179                {
180                    conn.Close();
181                    conn.Dispose();
182                }
183
184                //reset cursor - just in case...
185                Cursor = Cursors.Default;
186                //set user name to see it on Error Log Report
187                User.Instance().Login = textBox1.Text;
188            }
189        }
190
191        private bool LoginTest()
192        {
193            sqlBldr.ConnectionString =
194                ConfigurationManager.ConnectionStrings[bazyComboBox.SelectedItem.ToString()].ConnectionString;
195
196            sqlBldr.UserID = textBox1.Text;
197            sqlBldr.Password = textBox2.Text;
198            sqlBldr.ConnectTimeout = 200000;
199            sqlBldr.ApplicationName = "BazaReklam_v" + Application.ProductVersion;
200
201            SqlConnection conn = new SqlConnection(sqlBldr.ConnectionString);
202
203            try
204            {
205                conn.Open();
206                komunikatLabel.Text = "OK";
207                ConnString.getConnString().Value = sqlBldr.ConnectionString;
208                return true;
209            }
210            catch (SqlException e1)
211            {
212                switch (e1.Number)
213                {
214                    case 18456:
215                        komunikatLabel.Text = "Nieprawidłowy login lub hasło";
216                        break;
217                    default:
218                        komunikatLabel.Text = "Brak dostępu do bazy";
219                        MessageBox.Show(e1.Message);
220                        break;
221                }
222            }
223            finally
224            {
225                conn.Close();
226            }
227            return false;
228        }
229
230        private void Logowanie_KeyPress(object sender, KeyPressEventArgs e)
231        {
232            if (e.KeyChar == 13)
233            {
234                button1.PerformClick();
235            }
236        }
237
238        private static bool SprawdzWersje()
239        {
240            SqlCommand cmd = null;
241            try
242            {
243                cmd = new SqlCommand("SELECT ver FROM dbo.app WHERE [name]='BazaReklam'");
244                cmd.Connection =
245                    new SqlConnection("Data Source=" + ConnString.GetDataSource() + ";Initial Catalog=Applications;Persist Security Info=True;User=updater;Password=UpdateSolution;Application Name=BazaReklam_v" + Application.ProductVersion + ";");
246               
247
248                cmd.Connection.Open();
249                string ver = (string) cmd.ExecuteScalar();
250                cmd.Connection.Close();
251
252                if (ver.Trim() != Application.ProductVersion)
253                {
254                    FormBadVer frm = new FormBadVer();
255                    frm.labelVer.Text += Application.ProductVersion;
256                    frm.labelSrvVer.Text += ver;
257                    frm.ShowDialog();
258                    return false;
259                }
260            }
261            finally
262            {
263                if (cmd != null)
264                {
265                    if (cmd.Connection != null)
266                    {
267                        cmd.Connection.Close();
268                        cmd.Connection.Dispose();
269                    }
270                    cmd.Dispose();
271                }
272            }
273
274            return true;
275        }
276
277        #endregion Methods
278
279        private void cbEnableDbCombo_CheckedChanged(object sender, EventArgs e)
280        {
281            bazyComboBox.Enabled = cbEnableDbCombo.Checked;
282        }
283    }
284}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.