1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Security.Principal;
using System.Configuration;
namespace WindowsFormsApplication1
{
public partial class Gestion_comptes_administrateur : Form
{
#region Instanciation des attibuts
int touche = 0;//Variable qui contient la valeur d'une touche
List<String> NumBadgeAdmin = new List<string>();//Liste comportant tous les numéros de badge administrateur
//Pour récupérer ce qui est entré par l'utilisateur
private String badge = String.Empty;
private String nom = String.Empty;
//Pour autocomplete des textbox
List<String> Prenom = new List<String>();
List<String> Badge = new List<String>();
List<String> Nom = new List<String>();
AutoCompleteStringCollection LBadge = new AutoCompleteStringCollection();
AutoCompleteStringCollection LNom = new AutoCompleteStringCollection();
List<String> LPrenom = new List<String>();
//Connexion à l'annuaire ActiveDirectory
DirectoryEntry Ldap = new DirectoryEntry("LDAP://" + ConfigurationSettings.AppSettings["LdapServeur"].ToString(), ConfigurationSettings.AppSettings["LdapUser"].ToString(), ConfigurationSettings.AppSettings["LdapMdp"].ToString());
#endregion
public Gestion_comptes_administrateur()
{
InitializeComponent();
Initialisation();
}
#region Evènement selectedindexchanged liste_administrateur
//Lorsque l'on sélectionne une personne dans la listbox
private void Liste_administrateur_SelectedIndexChanged(object sender, EventArgs e)
{
String Numbadge = NumBadgeAdmin[Liste_administrateur.SelectedIndex];
//Nouvel objet pour instancier la recherche
DirectorySearcher recherche = new DirectorySearcher(Ldap);
//On modifie le filtre pour faire une recherche par badge
recherche.Filter = "(badge=" + Numbadge + ")";
SearchResult result = recherche.FindOne();
//On récupère l'objet trouvé lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
Badge_suppression.Text = Numbadge;
Nom_suppression.Text = DirEntry.Properties["nom"].Value.ToString();
Prenom_suppression.Text = DirEntry.Properties["prenom"].Value.ToString();
//Libère la ressource
recherche.Dispose();
}
#endregion
#region Evènement click
//Retourner à la page de démarrage
private void Retour_Click(object sender, EventArgs e)
{
Program.form1.Show();//Affiche la page de démarrage
this.Close();//Ferme la page actuelle
}
//Si on clic sur le boutton ajouter
private void Ajout_Click(object sender, EventArgs e)
{
int i = 0;
for (int j = 0; j < NumBadgeAdmin.Count; j++)
{
if (Badge_ajout.Text == NumBadgeAdmin[j]) i++;
}
if (i == 0)
{
try
{
//Nouvel objet pour instancier la recherche
DirectorySearcher recherche = new DirectorySearcher(Ldap);
//On modifie le filtre pour faire une recherche par badge
recherche.Filter = "(badge=" + Badge_ajout.Text + ")";
SearchResult result = recherche.FindOne();
//On récupère l'objet trouvé lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
//Libère la ressource
recherche.Dispose();
RequêteSQL sql = new RequêteSQL();
sql.AjoutAdmin(Badge_ajout.Text.ToUpper());
MessageBox.Show(DirEntry.Properties["nom"].Value.ToString() + " " + DirEntry.Properties["prenom"].Value.ToString() + " fait maintenant parti des administrateurs", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("Impossible d'ajouter cette personne, n'avez vous pas fait une erreur?", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Initialisation();
}
}
else
{
MessageBox.Show("Cette personne est déjà administrateur", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Error);
Initialisation();
}
}
//Si on clic sur le boutton supprimer
private void Suppression_Click(object sender, EventArgs e)
{
//Impossible de supprimer ses propres droits
if (Badge_suppression.Text == Identification.id) MessageBox.Show("Impossible de supprimer vos propres droits", "Suppression", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
DialogResult suppression = MessageBox.Show("Etes-vous sur de vouloir supprimer les droits à cet administrateur?", "Suppression", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
//Si l'utilisateur confirme la suppression
if (suppression == DialogResult.OK)
{
RequêteSQL sql = new RequêteSQL();
int sup = sql.SuppressionAdmin(Badge_suppression.Text);
if (sup == 1) MessageBox.Show(Prenom_suppression.Text + " " + Nom_suppression.Text + " a été retiré des administrateurs du logiciel", "Suppression", MessageBoxButtons.OK, MessageBoxIcon.Information);
else MessageBox.Show("Cette personne est introuvable dans la base de donnée, veuillez sélectionner une personne dans la liste. Sinon contacté l'administrateur", "Suppression", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//Si l'utilisateur annule la suppression
else if (suppression == DialogResult.Cancel)
{
MessageBox.Show("Supression annulée", "Suppression", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Initialisation();
}
#endregion
#region Evènement selectionchangecommitted prenom_ajout
//Si on choisi un élément dans le combobox prénom
private void Prenom_ajout_SelectionChangeCommitted(object sender, EventArgs e)
{
int x = 0;
//Pour afficher l'item sélectionné
ComboBox senderComboBox = (ComboBox)sender;
if (senderComboBox.SelectedIndex != -1)
{
Prenom_ajout.Text = senderComboBox.Items[senderComboBox.SelectedIndex].ToString();
foreach (String a in Prenom)
{
if (a == Prenom_ajout.Text && Nom[x].StartsWith(this.nom))
{
Nom_ajout.Text = Nom[x];
this.nom = Nom_ajout.Text;
Badge_ajout.Text = Badge[x];
this.badge = Badge_ajout.Text;
}
x++;
}
}
}
#endregion
#region Evènement keypress
//Si on appui sur une touche lorsque le textbox Badge_ajout est sélectionné
private void Badge_ajout_KeyPress(object sender, KeyPressEventArgs e)
{
touche = Convert.ToInt16(e.KeyChar);//Récupère le code ASCII de la touche enfoncée
//Vérifie que la touche enfoncée est un chiffre, une lettre majuscule ou une lettre miniscule
if ((touche >= 48 && touche <= 57) || (touche >= 65 && touche <= 90) || (touche >= 97 && touche <= 122))
{
if (badge.Length < 7)
{
this.badge += e.KeyChar.ToString().ToUpper();
Badge_ajout.Text = this.badge;
}
}
//Si on appuie sur la touche de retour en arrière
else if (touche == 8)
{
if (badge.Length > 0)
{
//Si l'utilisateur a sélectionné 1 ou plusieurs lettres autres que celles surlignées pour l'autocomplete
if (Badge_ajout.SelectionLength > 0 && this.badge == Badge_ajout.Text) this.badge = Badge_ajout.Text.Remove(Badge_ajout.SelectionStart);
else this.badge = Badge_ajout.Text.Remove(badge.Length - 1);
Badge_ajout.Text = this.badge;
}
}
ActiveDirectory(1, 0);//Appel d'une fonction ActiveDirectory
//Pour mettre le curseur à la fin
Badge_ajout.SelectionStart = badge.Length;
//Pour surligner ce qui a été rajouter par l'autocomplete
Badge_ajout.SelectionLength = Badge_ajout.Text.Length - badge.Length;
//Pour marqué l'évènement comme géré
e.Handled = true;
}//L'erreur apparait ici
//Si on appui sur une touche lorsque le textbox Nom_ajout est sélectionné
private void Nom_ajout_KeyPress(object sender, KeyPressEventArgs e)
{
touche = Convert.ToInt16(e.KeyChar);//Récupère le code ASCII de la touche enfoncée
//Vérifie que la touche enfoncée est un chiffre, une lettre majuscule ou une lettre miniscule
if ((touche >= 48 && touche <= 57) || (touche >= 65 && touche <= 90) || (touche >= 97 && touche <= 122))
{
this.nom += e.KeyChar.ToString().ToUpper();
Nom_ajout.Text = this.nom;
}
//Si on appuie sur la touche de retour en arrière
else if (touche == 8)
{
//Si l'utilisateur à rentré au moins 1 lettre
if (this.nom.Length > 0)
{
//Si l'utilisateur a sélectionné 1 ou plusieurs lettres autres que celles surlignées pour l'autocomplete
if (Nom_ajout.SelectionLength > 0 && this.nom == Nom_ajout.Text) this.nom = Nom_ajout.Text.Remove(Nom_ajout.SelectionStart);
else this.nom = Nom_ajout.Text.Remove(nom.Length - 1);
Nom_ajout.Text = this.nom;
}
}
ActiveDirectory(0, 1);//Appel d'une fonction ActiveDirectory
//Pour mettre le curseur à la fin
Nom_ajout.SelectionStart = nom.Length;
//Pour surligner ce qui a été rajouter par l'autocomplete
Nom_ajout.SelectionLength = Nom_ajout.Text.Length - nom.Length;
//Pour marqué l'évènement comme géré
e.Handled = true;
} //L'erreur apparait ici
#endregion
#region Fonction de la classe
//Fonction pour initialiser la fenêtre
private void Initialisation()
{
this.ControlBox = false;//Enlève les bouton en haut à droite pour quitter, réduire et agrandir
StartPosition = FormStartPosition.CenterScreen;//Positionne la form au milieu de l'écran
Titre.Font = new Font("Gestion des comptes utilisateur-administrateur", 16);//Modifie la taille du titre
//Initialise à vide les label et les combobox
Prenom_ajout.Items.Clear();
Badge_suppression.Text = String.Empty;
Prenom_suppression.Text = String.Empty;
Nom_suppression.Text = String.Empty;
Badge_ajout.Text = String.Empty;
Nom_ajout.Text = String.Empty;
Prenom_ajout.Text = String.Empty;
Badge_ajout.MaxLength = 7;//Limite à 7 caractères le numéro du badge
//Initialise l'autocomplete des textbox et du combobox
Prenom_ajout.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
Prenom_ajout.AutoCompleteSource = AutoCompleteSource.ListItems;
Nom_ajout.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
Nom_ajout.AutoCompleteSource = AutoCompleteSource.CustomSource;
Nom_ajout.AutoCompleteCustomSource = LNom;
Badge_ajout.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
Badge_ajout.AutoCompleteSource = AutoCompleteSource.CustomSource;
Badge_ajout.AutoCompleteCustomSource = LBadge;
//Initialisation de la listbox
Liste_administrateur.Items.Clear();
//Initialisation des list
LBadge.Clear();
LNom.Clear();
LPrenom.Clear();
NumBadgeAdmin.Clear();
Liste_administrateur.BeginUpdate();
RequêteSQL sql = new RequêteSQL();
foreach (string c in sql.RecNumBadgeAdmin())
{
//Ajoute le numéro de badge dans une liste de badge administrateur
NumBadgeAdmin.Add(c);
//Nouvel objet pour instancier la recherche
DirectorySearcher recherche = new DirectorySearcher(Ldap);
//On modifie le filtre pour faire une recherche par badge
recherche.Filter = "(sAMAccountName=" + c + ")";
SearchResult result = recherche.FindOne();
//On récupère l'objet trouvé lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
this.Liste_administrateur.Items.Add("Numéro du badge : " + c + " ; Nom : " + DirEntry.Properties["nom"].Value.ToString() + " ; Prénom : " + DirEntry.Properties["prenom"].Value.ToString());
//Libère la ressource
recherche.Dispose();
}
Liste_administrateur.EndUpdate();
}
//Récupère un nom et un prénom et un badge selon ce qui est modifié dans les textbox
private void ActiveDirectory(int badge, int nom)
{
//Si on modifie le badge
if (badge == 1)
{
//S'il y a moins de 5 caractères dans le badge, on ne recherche pas les badges, les noms et prénoms des personnes correspondant
if (this.badge.Length < 5)
{
//Met à vide les autres textbox et combobox
Nom_ajout.Text = String.Empty;
Prenom_ajout.Text = String.Empty;
//Met à vide les listes et les items des combobox
Prenom.Clear();
Nom.Clear();
Badge.Clear();
LBadge.Clear();
LNom.Clear();
LPrenom.Clear();
Prenom_ajout.Items.Clear();
}
//S'il y a au moins 5 caractères dans le badge
else
{
//Met à vide les listes
Nom.Clear();
Prenom.Clear();
Badge.Clear();
ChercheBadge();//Appel de la fonction ChercheBadge
}
}
//Si on modifie le nom
if (nom == 1)
{
//Si on a inséré moins de 3 caractères dans le combobox nom
if (this.nom.Length < 3)
{
//Met à vide les autres textbox et combobox
Badge_ajout.Text = String.Empty;
Prenom_ajout.Text = String.Empty;
//Met à vide les listes et les items des combobox
Prenom.Clear();
Nom.Clear();
Badge.Clear();
LBadge.Clear();
LNom.Clear();
LPrenom.Clear();
Prenom_ajout.Items.Clear();
}
//Si on a inséré au moins 3 caractères dans le combobox nom
else
{
//Met à vide les listes
Nom.Clear();
Prenom.Clear();
Badge.Clear();
ChercheNom();//Appel de la fonction ChercheNom
}
}
//On affiche la première possibilité
if (LPrenom.Count > 0) Prenom_ajout.Text = LPrenom[0];
if (LBadge.Count > 0) Badge_ajout.Text = LBadge[0];
if (LNom.Count > 0) Nom_ajout.Text = LNom[0];
}
//Si on entre un badge
private void ChercheBadge()
{
try
{
//Nouvel objet pour instancier la recherche
DirectorySearcher recherche = new DirectorySearcher(Ldap);
//On modifie le filtre pour faire une recherche par badge
recherche.Filter = "(badge=" + this.badge + ")";
SearchResult result = recherche.FindOne();//S'il trouve un badge identique
//Si on ne trouve aucun résultat on filtre en disant que ce n'est que le début du badge
if (result == null)
{
//Libère la ressource
recherche.Dispose();
recherche.Filter = "(badge=" + this.badge + "*)";
//Si le bage que l'on entre n'est pas connu
if (recherche.FindOne() == null)
{
//Met à vide les autres textbox et combobox
Nom_ajout.Text = String.Empty;
Prenom_ajout.Text = String.Empty;
//Met à vide les listes et les items des combobox
Prenom.Clear();
Nom.Clear();
Badge.Clear();
LBadge.Clear();
LNom.Clear();
LPrenom.Clear();
Prenom_ajout.Items.Clear();
}
else
{
//Boucle pour récupérer tous les utilisateurs dont le badge commence par les caractères entrés
foreach (SearchResult result2 in recherche.FindAll())
{
//On récupère l'objet trouvé lors de la recherche
DirectoryEntry DirEntry = result2.GetDirectoryEntry();
//On n'utilise que les badges ayant 7 caractères
if (DirEntry.Properties["badge"].Value.ToString().Length == 7)
{
//On ajoute dans la liste les valeurs
Prenom.Add(DirEntry.Properties["prenom"].Value.ToString());
Nom.Add(DirEntry.Properties["nom"].Value.ToString());
Badge.Add(DirEntry.Properties["badge"].Value.ToString());
}
}
//Met à vide les item du combobox
Prenom_ajout.Items.Clear();
//Met à vide les collections
LNom.Clear();
LBadge.Clear();
LPrenom.Clear();
//Pour chaque badge qui existe
for (int w = 0; w < Badge.Count; w++)
{
//Si le badge est identique à celui choisi
if (Badge[w].StartsWith(Badge_ajout.Text))
{
int k = 0;
int l = 0;
int m = 0;
//On insère dans les items de la combobox les valeurs possible
foreach (String a in Prenom_ajout.Items) if (a == Prenom[w]) k++;
if (k == 0)
{
LPrenom.Add(Prenom[w]);
Prenom_ajout.Items.Add(Prenom[w]);
}
//On insère dans les listes les valeurs possible
foreach (String a in LNom) if (a == Nom[w]) l++;
if (l == 0) LNom.Add(Nom[w]);
foreach (String a in LBadge) if (a == Badge[w]) m++;
if (m == 0) LBadge.Add(Badge[w]);
}
}
}
//Libère la ressource
recherche.Dispose();
}
//Libère la ressource
else recherche.Dispose();
}
catch (Exception ex)
{
string test = ex.Message;
MessageBox.Show(test, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
//Trie par ordre alphabétique les prénoms et les badges
Prenom_ajout.Sorted = true;
}
}
//Si on entre un nom
private void ChercheNom()
{
try
{
//Nouvel objet pour instancier la recherche
DirectorySearcher recherche = new DirectorySearcher(Ldap);
//On modifie le filtre pour faire une recherche par nom
recherche.Filter = "(nom=" + this.nom + ")";
SearchResult result = recherche.FindOne();//S'il trouve un nom identique
//Si on ne trouve aucun résultat on filtre en disant que ce n'est que le début du nom
if (result == null)
{
//Libère la ressource
recherche.Dispose();
recherche.Filter = "(nom=" + this.nom + "*)";
//Si le bage que l'on entre n'est pas connu
if (recherche.FindOne() == null)
{
//Met à vide les autres textbox et combobox
Badge_ajout.Text = String.Empty;
Prenom_ajout.Text = String.Empty;
//Met à vide les listes et les items des combobox
Prenom.Clear();
Nom.Clear();
Badge.Clear();
LBadge.Clear();
LNom.Clear();
LPrenom.Clear();
Prenom_ajout.Items.Clear();
}
else
{
//Boucle pour récupérer tous les utilisateurs dont le nom commence par les caractères entrés
foreach (SearchResult result2 in recherche.FindAll())
{
//On récupère l'objet trouvé lors de la recherche
DirectoryEntry DirEntry = result2.GetDirectoryEntry();
//Vérification que c'est une personne
if (DirEntry.Properties.Contains("badge") && DirEntry.Properties["prenom"].Value != null && DirEntry.Properties["prenom"].Value.ToString() != "-")
{
//On n'utilise que les badges ayant 7 caractères
if (DirEntry.Properties["sAMAccountName"].Value.ToString().Length == 7)
{
//On ajoute dans la liste les valeurs
Nom.Add(DirEntry.Properties["nom"].Value.ToString());
Prenom.Add(DirEntry.Properties["prenom"].Value.ToString());
Badge.Add(DirEntry.Properties["badge"].Value.ToString());
}
}
}
//Met à vide les items du combobox
Prenom_ajout.Items.Clear();
//Met à vide les collections
LNom.Clear();
LBadge.Clear();
LPrenom.Clear();
//Pour chaque nom qui existe
for (int w = 0; w < Nom.Count; w++)
{
//Si le nom est identique à celui choisi
if (Nom[w].StartsWith(Nom_ajout.Text))
{
int k = 0;
int l = 0;
int m = 0;
//On insère dans les items de la combobox les valeurs possible
foreach (String a in Prenom_ajout.Items) if (a == Prenom[w]) k++;
if (k == 0)
{
LPrenom.Add(Prenom[w]);
Prenom_ajout.Items.Add(Prenom[w]);
}
//On insère dans les listes les valeurs possible
foreach (String a in LNom) if (a == Nom[w]) l++;
if (l == 0) LNom.Add(Nom[w]);
foreach (String a in LBadge) if (a == Badge[w]) m++;
if (m == 0) LBadge.Add(Badge[w]);
}
}
}
//Libère la ressource
recherche.Dispose();
}
//Libère la ressource
else recherche.Dispose();
}
catch (Exception ex)
{
string test = ex.Message;
MessageBox.Show(test, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
//Trie par ordre alphabétique les prénoms et les badges
Prenom_ajout.Sorted = true;
}
}
#endregion
}
} |
Partager