|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +using System.Drawing; |
| 4 | +using System.IO; |
| 5 | +using System.Windows.Forms; |
| 6 | + |
| 7 | +namespace DataSmart.MailServer.AccessManager |
| 8 | +{ |
| 9 | + public class MainForm : Form |
| 10 | + { |
| 11 | + private TabControl m_pTab; |
| 12 | + |
| 13 | + private Button m_pCancel; |
| 14 | + |
| 15 | + private Button m_pOk; |
| 16 | + |
| 17 | + private ListView m_pTab_IPAccess_List; |
| 18 | + |
| 19 | + private Button m_pTab_IPAccess_Add; |
| 20 | + |
| 21 | + private Button m_pTab_IPAccess_Edit; |
| 22 | + |
| 23 | + private Button m_pTab_IPAccess_Delete; |
| 24 | + |
| 25 | + private ListView m_pTab_Users_List; |
| 26 | + |
| 27 | + private Button m_pTab_Users_Add; |
| 28 | + |
| 29 | + private Button m_pTab_Users_Edit; |
| 30 | + |
| 31 | + private Button m_pTab_Users_Delete; |
| 32 | + |
| 33 | + private DataSet m_pDsSettings; |
| 34 | + |
| 35 | + public MainForm() |
| 36 | + { |
| 37 | + this.InitializeComponent(); |
| 38 | + this.LoadSettings(); |
| 39 | + } |
| 40 | + |
| 41 | + private void InitializeComponent() |
| 42 | + { |
| 43 | + base.ClientSize = new Size(400, 275); |
| 44 | + base.StartPosition = FormStartPosition.CenterScreen; |
| 45 | + this.Text = "DataSmart Mail Server Administration Access Configurator:"; |
| 46 | + this.m_pTab = new TabControl(); |
| 47 | + this.m_pTab.Size = new Size(392, 230); |
| 48 | + this.m_pTab.Location = new Point(5, 5); |
| 49 | + this.m_pTab.TabPages.Add(new TabPage("IP Access")); |
| 50 | + this.m_pTab.TabPages.Add(new TabPage("Users")); |
| 51 | + this.m_pCancel = new Button(); |
| 52 | + this.m_pCancel.Size = new Size(70, 20); |
| 53 | + this.m_pCancel.Location = new Point(245, 245); |
| 54 | + this.m_pCancel.Text = "Cancel"; |
| 55 | + this.m_pCancel.Click += new EventHandler(this.m_pCancel_Click); |
| 56 | + this.m_pOk = new Button(); |
| 57 | + this.m_pOk.Size = new Size(70, 20); |
| 58 | + this.m_pOk.Location = new Point(320, 245); |
| 59 | + this.m_pOk.Text = "Ok"; |
| 60 | + this.m_pOk.Click += new EventHandler(this.m_pOk_Click); |
| 61 | + base.Controls.Add(this.m_pTab); |
| 62 | + base.Controls.Add(this.m_pCancel); |
| 63 | + base.Controls.Add(this.m_pOk); |
| 64 | + this.m_pTab_IPAccess_List = new ListView(); |
| 65 | + this.m_pTab_IPAccess_List.Size = new Size(300, 150); |
| 66 | + this.m_pTab_IPAccess_List.Location = new Point(5, 30); |
| 67 | + this.m_pTab_IPAccess_List.View = View.Details; |
| 68 | + this.m_pTab_IPAccess_List.HideSelection = false; |
| 69 | + this.m_pTab_IPAccess_List.FullRowSelect = true; |
| 70 | + this.m_pTab_IPAccess_List.SelectedIndexChanged += new EventHandler(this.m_pTab_IPAccess_List_SelectedIndexChanged); |
| 71 | + this.m_pTab_IPAccess_List.Columns.Add("Start IP", 145, HorizontalAlignment.Center); |
| 72 | + this.m_pTab_IPAccess_List.Columns.Add("End IP", 145, HorizontalAlignment.Center); |
| 73 | + this.m_pTab_IPAccess_Add = new Button(); |
| 74 | + this.m_pTab_IPAccess_Add.Size = new Size(70, 20); |
| 75 | + this.m_pTab_IPAccess_Add.Location = new Point(310, 30); |
| 76 | + this.m_pTab_IPAccess_Add.Text = "Add"; |
| 77 | + this.m_pTab_IPAccess_Add.Click += new EventHandler(this.m_pTab_IPAccess_Add_Click); |
| 78 | + this.m_pTab_IPAccess_Edit = new Button(); |
| 79 | + this.m_pTab_IPAccess_Edit.Size = new Size(70, 20); |
| 80 | + this.m_pTab_IPAccess_Edit.Location = new Point(310, 55); |
| 81 | + this.m_pTab_IPAccess_Edit.Text = "Edit"; |
| 82 | + this.m_pTab_IPAccess_Edit.Click += new EventHandler(this.m_pTab_IPAccess_Edit_Click); |
| 83 | + this.m_pTab_IPAccess_Delete = new Button(); |
| 84 | + this.m_pTab_IPAccess_Delete.Size = new Size(70, 20); |
| 85 | + this.m_pTab_IPAccess_Delete.Location = new Point(310, 80); |
| 86 | + this.m_pTab_IPAccess_Delete.Text = "Delete"; |
| 87 | + this.m_pTab_IPAccess_Delete.Click += new EventHandler(this.m_pTab_IPAccess_Delete_Click); |
| 88 | + this.m_pTab.TabPages[0].Controls.Add(this.m_pTab_IPAccess_List); |
| 89 | + this.m_pTab.TabPages[0].Controls.Add(this.m_pTab_IPAccess_Add); |
| 90 | + this.m_pTab.TabPages[0].Controls.Add(this.m_pTab_IPAccess_Edit); |
| 91 | + this.m_pTab.TabPages[0].Controls.Add(this.m_pTab_IPAccess_Delete); |
| 92 | + this.m_pTab_Users_List = new ListView(); |
| 93 | + this.m_pTab_Users_List.Size = new Size(300, 150); |
| 94 | + this.m_pTab_Users_List.Location = new Point(5, 30); |
| 95 | + this.m_pTab_Users_List.View = View.Details; |
| 96 | + this.m_pTab_Users_List.HideSelection = false; |
| 97 | + this.m_pTab_Users_List.FullRowSelect = true; |
| 98 | + this.m_pTab_Users_List.SelectedIndexChanged += new EventHandler(this.m_pTab_Users_List_SelectedIndexChanged); |
| 99 | + this.m_pTab_Users_List.Columns.Add("User Name", 250, HorizontalAlignment.Center); |
| 100 | + this.m_pTab_Users_Add = new Button(); |
| 101 | + this.m_pTab_Users_Add.Size = new Size(70, 20); |
| 102 | + this.m_pTab_Users_Add.Location = new Point(310, 30); |
| 103 | + this.m_pTab_Users_Add.Text = "Add"; |
| 104 | + this.m_pTab_Users_Add.Click += new EventHandler(this.m_pTab_Users_Add_Click); |
| 105 | + this.m_pTab_Users_Edit = new Button(); |
| 106 | + this.m_pTab_Users_Edit.Size = new Size(70, 20); |
| 107 | + this.m_pTab_Users_Edit.Location = new Point(310, 55); |
| 108 | + this.m_pTab_Users_Edit.Text = "Edit"; |
| 109 | + this.m_pTab_Users_Edit.Click += new EventHandler(this.m_pTab_Users_Edit_Click); |
| 110 | + this.m_pTab_Users_Delete = new Button(); |
| 111 | + this.m_pTab_Users_Delete.Size = new Size(70, 20); |
| 112 | + this.m_pTab_Users_Delete.Location = new Point(310, 80); |
| 113 | + this.m_pTab_Users_Delete.Text = "Delete"; |
| 114 | + this.m_pTab_Users_Delete.Click += new EventHandler(this.m_pTab_Users_Delete_Click); |
| 115 | + this.m_pTab.TabPages[1].Controls.Add(this.m_pTab_Users_List); |
| 116 | + this.m_pTab.TabPages[1].Controls.Add(this.m_pTab_Users_Add); |
| 117 | + this.m_pTab.TabPages[1].Controls.Add(this.m_pTab_Users_Edit); |
| 118 | + this.m_pTab.TabPages[1].Controls.Add(this.m_pTab_Users_Delete); |
| 119 | + } |
| 120 | + |
| 121 | + private void m_pTab_IPAccess_List_SelectedIndexChanged(object sender, EventArgs e) |
| 122 | + { |
| 123 | + if (this.m_pTab_IPAccess_List.SelectedItems.Count == 0) |
| 124 | + { |
| 125 | + this.m_pTab_IPAccess_Edit.Enabled = false; |
| 126 | + this.m_pTab_IPAccess_Delete.Enabled = false; |
| 127 | + return; |
| 128 | + } |
| 129 | + this.m_pTab_IPAccess_Edit.Enabled = true; |
| 130 | + this.m_pTab_IPAccess_Delete.Enabled = true; |
| 131 | + } |
| 132 | + |
| 133 | + private void m_pTab_IPAccess_Add_Click(object sender, EventArgs e) |
| 134 | + { |
| 135 | + IPAccessForm iPAccessForm = new IPAccessForm(this.m_pDsSettings); |
| 136 | + if (iPAccessForm.ShowDialog(this) == DialogResult.OK) |
| 137 | + { |
| 138 | + this.m_pTab_IPAccess_List.Items.Clear(); |
| 139 | + foreach (DataRow dataRow in this.m_pDsSettings.Tables["IP_Access"].Rows) |
| 140 | + { |
| 141 | + ListViewItem listViewItem = new ListViewItem(dataRow["StartIP"].ToString()); |
| 142 | + listViewItem.SubItems.Add(dataRow["EndIP"].ToString()); |
| 143 | + listViewItem.Tag = dataRow; |
| 144 | + this.m_pTab_IPAccess_List.Items.Add(listViewItem); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private void m_pTab_IPAccess_Edit_Click(object sender, EventArgs e) |
| 150 | + { |
| 151 | + if (this.m_pTab_IPAccess_List.SelectedItems[0].Text.ToLower() == "127.0.0.1" && this.m_pTab_IPAccess_List.SelectedItems[0].SubItems[0].Text == "127.0.0.1") |
| 152 | + { |
| 153 | + MessageBox.Show("IP range 127.0.0.1 - 127.0.0.1 is permanent system entry and cannot be modified !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Hand); |
| 154 | + return; |
| 155 | + } |
| 156 | + IPAccessForm iPAccessForm = new IPAccessForm(this.m_pDsSettings, (DataRow)this.m_pTab_IPAccess_List.SelectedItems[0].Tag); |
| 157 | + if (iPAccessForm.ShowDialog(this) == DialogResult.OK) |
| 158 | + { |
| 159 | + this.m_pTab_IPAccess_List.Items.Clear(); |
| 160 | + foreach (DataRow dataRow in this.m_pDsSettings.Tables["IP_Access"].Rows) |
| 161 | + { |
| 162 | + ListViewItem listViewItem = new ListViewItem(dataRow["StartIP"].ToString()); |
| 163 | + listViewItem.SubItems.Add(dataRow["EndIP"].ToString()); |
| 164 | + listViewItem.Tag = dataRow; |
| 165 | + this.m_pTab_IPAccess_List.Items.Add(listViewItem); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private void m_pTab_IPAccess_Delete_Click(object sender, EventArgs e) |
| 171 | + { |
| 172 | + if (this.m_pTab_IPAccess_List.SelectedItems[0].Text.ToLower() == "127.0.0.1") |
| 173 | + { |
| 174 | + MessageBox.Show("IP range 127.0.0.1 - 127.0.0.1 is permanent system entry and cannot be deleted !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Hand); |
| 175 | + return; |
| 176 | + } |
| 177 | + if (MessageBox.Show("Are you sure you want to delete entry '" + this.m_pTab_IPAccess_List.SelectedItems[0].Text + "' !", "Confirm:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) |
| 178 | + { |
| 179 | + ((DataRow)this.m_pTab_IPAccess_List.SelectedItems[0].Tag).Delete(); |
| 180 | + this.m_pTab_IPAccess_List.SelectedItems[0].Remove(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private void m_pTab_Users_List_SelectedIndexChanged(object sender, EventArgs e) |
| 185 | + { |
| 186 | + if (this.m_pTab_Users_List.SelectedItems.Count == 0) |
| 187 | + { |
| 188 | + this.m_pTab_Users_Edit.Enabled = false; |
| 189 | + this.m_pTab_Users_Delete.Enabled = false; |
| 190 | + return; |
| 191 | + } |
| 192 | + this.m_pTab_Users_Edit.Enabled = true; |
| 193 | + this.m_pTab_Users_Delete.Enabled = true; |
| 194 | + } |
| 195 | + |
| 196 | + private void m_pTab_Users_Add_Click(object sender, EventArgs e) |
| 197 | + { |
| 198 | + UserForm userForm = new UserForm(this.m_pDsSettings); |
| 199 | + if (userForm.ShowDialog(this) == DialogResult.OK) |
| 200 | + { |
| 201 | + this.m_pTab_Users_List.Items.Clear(); |
| 202 | + foreach (DataRow dataRow in this.m_pDsSettings.Tables["Users"].Rows) |
| 203 | + { |
| 204 | + ListViewItem listViewItem = new ListViewItem(dataRow["UserName"].ToString()); |
| 205 | + listViewItem.Tag = dataRow; |
| 206 | + this.m_pTab_Users_List.Items.Add(listViewItem); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private void m_pTab_Users_Edit_Click(object sender, EventArgs e) |
| 212 | + { |
| 213 | + UserForm userForm = new UserForm(this.m_pDsSettings, (DataRow)this.m_pTab_Users_List.SelectedItems[0].Tag); |
| 214 | + userForm.ShowDialog(this); |
| 215 | + } |
| 216 | + |
| 217 | + private void m_pTab_Users_Delete_Click(object sender, EventArgs e) |
| 218 | + { |
| 219 | + if (this.m_pTab_Users_List.SelectedItems[0].Text.ToLower() == "administrator") |
| 220 | + { |
| 221 | + MessageBox.Show("User Administrator is permanent user and cannot be deleted !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Hand); |
| 222 | + return; |
| 223 | + } |
| 224 | + if (MessageBox.Show("Are you sure you want to delete user '" + this.m_pTab_Users_List.SelectedItems[0].Text + "' !", "Confirm:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) |
| 225 | + { |
| 226 | + ((DataRow)this.m_pTab_Users_List.SelectedItems[0].Tag).Delete(); |
| 227 | + this.m_pTab_Users_List.SelectedItems[0].Remove(); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + private void m_pCancel_Click(object sender, EventArgs e) |
| 232 | + { |
| 233 | + base.DialogResult = DialogResult.Cancel; |
| 234 | + base.Close(); |
| 235 | + } |
| 236 | + |
| 237 | + private void m_pOk_Click(object sender, EventArgs e) |
| 238 | + { |
| 239 | + if (this.m_pDsSettings.HasChanges()) |
| 240 | + { |
| 241 | + var path = Path.Combine(Application.StartupPath, "Settings"); |
| 242 | + if (!Directory.Exists(path)) |
| 243 | + { |
| 244 | + Directory.CreateDirectory(path); |
| 245 | + } |
| 246 | + path = Path.Combine(path, "AdminAccess.xml"); |
| 247 | + this.m_pDsSettings.WriteXml(path); |
| 248 | + } |
| 249 | + base.DialogResult = DialogResult.OK; |
| 250 | + base.Close(); |
| 251 | + } |
| 252 | + |
| 253 | + private void LoadSettings() |
| 254 | + { |
| 255 | + this.m_pDsSettings = new DataSet(); |
| 256 | + this.m_pDsSettings.Tables.Add("IP_Access"); |
| 257 | + this.m_pDsSettings.Tables["IP_Access"].Columns.Add("StartIP"); |
| 258 | + this.m_pDsSettings.Tables["IP_Access"].Columns.Add("EndIP"); |
| 259 | + this.m_pDsSettings.Tables.Add("Users"); |
| 260 | + this.m_pDsSettings.Tables["Users"].Columns.Add("UserName"); |
| 261 | + this.m_pDsSettings.Tables["Users"].Columns.Add("Password"); |
| 262 | + if (File.Exists(Application.StartupPath + "/Settings/AdminAccess.xml")) |
| 263 | + { |
| 264 | + this.m_pDsSettings.ReadXml(Application.StartupPath + "/Settings/AdminAccess.xml"); |
| 265 | + } |
| 266 | + else |
| 267 | + { |
| 268 | + DataRow dataRow = this.m_pDsSettings.Tables["IP_Access"].NewRow(); |
| 269 | + dataRow["StartIP"] = "127.0.0.1"; |
| 270 | + dataRow["EndIP"] = "127.0.0.1"; |
| 271 | + this.m_pDsSettings.Tables["IP_Access"].Rows.Add(dataRow); |
| 272 | + dataRow = this.m_pDsSettings.Tables["Users"].NewRow(); |
| 273 | + dataRow["UserName"] = "Administrator"; |
| 274 | + dataRow["Password"] = ""; |
| 275 | + this.m_pDsSettings.Tables["Users"].Rows.Add(dataRow); |
| 276 | + } |
| 277 | + foreach (DataRow dataRow2 in this.m_pDsSettings.Tables["IP_Access"].Rows) |
| 278 | + { |
| 279 | + ListViewItem listViewItem = new ListViewItem(dataRow2["StartIP"].ToString()); |
| 280 | + listViewItem.SubItems.Add(dataRow2["EndIP"].ToString()); |
| 281 | + listViewItem.Tag = dataRow2; |
| 282 | + this.m_pTab_IPAccess_List.Items.Add(listViewItem); |
| 283 | + } |
| 284 | + foreach (DataRow dataRow3 in this.m_pDsSettings.Tables["Users"].Rows) |
| 285 | + { |
| 286 | + ListViewItem listViewItem2 = new ListViewItem(dataRow3["UserName"].ToString()); |
| 287 | + listViewItem2.Tag = dataRow3; |
| 288 | + this.m_pTab_Users_List.Items.Add(listViewItem2); |
| 289 | + } |
| 290 | + this.m_pTab_IPAccess_List_SelectedIndexChanged(this, null); |
| 291 | + this.m_pTab_Users_List_SelectedIndexChanged(this, null); |
| 292 | + } |
| 293 | + } |
| 294 | +} |
0 commit comments