/// <summary> /// Přetvoří data do uživatelů (List<List<string>> do List<AdUser>>) /// </summary> private void convertToAdUser() { int listNumber = loadedListList.Count(); if (listNumber > 6) { int listLenght = loadedListList[0].Count; for (int i = 0; i < listLenght; i++) { if (loadedListList[0][i] != "") { AdUser adUserNew = new AdUser(); adUserNew.nameAcco = loadedListList[0][i]; adUserNew.nameFull = loadedListList[1][i]; adUserNew.csvGsm = loadedListList[2][i]; adUserNew.csvData = loadedListList[3][i]; adUserNew.csvGsmo = loadedListList[4][i]; adUserNew.csvFix = loadedListList[5][i]; adUserNew.csvFixo = loadedListList[6][i]; loadedUserList.Add(adUserNew); } } } }
/// <summary> /// compare local "UserListAd" with sended list "userListCSV" and create "userListFinal" /// </summary> public void makeFinalList() { foreach (AdUser user in userListAd) { //search equalent in csv list AdUser userInCsv = userListCSV.Find(x => x.nameAcco.Equals(user.nameAcco)); AdUser finalUser = null; bool ignoreThisUser = isUserInIgnored(user); //user is in ignore list if (!ignoreThisUser) { //user was found in CSV List if (userInCsv != null) { finalUser = redistributionBasedOnAd(user, userInCsv); } else { //user isnt in CSV list //clear all number atributes and add static number finalUser = redistributionNoInCsv(user); } if (finalUser != null) { userListFinal.Add(finalUser); } } //ignored user } }
/// <summary> /// return 2 List with data to change. /// </summary> /// <param name="user">user</param> /// <returns>[0] == List<string> attributes, [1] == List<string> attribData</returns> private List <List <string> > markDiferencesDefault(AdUser user) { List <List <string> > finalList = new List <List <string> >(); List <string> attributes = new List <string>(); List <string> attribData = new List <string>(); //clear all number atributes and add static number if (!user.tTel.Equals("")) { editLog($" {user.nameAcco} (tTel): z [{user.tTel}] , na [] "); attributes.Add("TelephoneNumber"); attribData.Add(""); } //edited to corespond politic if (!user.tIpPhone.Equals(user.tTelOthers)) { editLog($" {user.nameAcco} (tIpPhone): z [{user.tIpPhone}] , na [{user.tTelOthers}] "); attributes.Add("ipPhone"); attribData.Add(user.tTelOthers); } if (!user.tIpPhoneOthers.Equals("")) { editLog($" {user.nameAcco} (tIpPhoneOthers): z [{user.tIpPhoneOthers}] , na [] "); attributes.Add("otherIpPhone"); attribData.Add(""); } if (!user.tMob.Equals("")) { editLog($" {user.nameAcco} (tMob): z [{user.tMob}] , na [] "); attributes.Add("Mobile"); attribData.Add(""); } if (!user.tMobOthers.Equals("")) { editLog($" {user.nameAcco} (tMobOthers): z [{user.tMobOthers}] , na [] "); attributes.Add("otherMobile"); attribData.Add(""); } if (!user.tHomePhone.Equals("")) { editLog($" {user.nameAcco} (tHomePhone): z [{user.tHomePhone}] , na [] "); attributes.Add("homePhone"); attribData.Add(""); } if (!user.tHomePhoneOthers.Equals("")) { editLog($" {user.nameAcco} (tHomePhoneOthers): z [{user.tHomePhoneOthers}] , na [] "); attributes.Add("otherHomePhone"); attribData.Add(""); } finalList.Add(attributes); finalList.Add(attribData); return(finalList); }
/// <summary> /// porovná nameAcco vůči předanému objektu /// </summary> /// <param name="user">porovná nameAcco</param> /// <returns></returns> public bool equalAccountName(AdUser user) { if (this.nameAcco.Equals(user.nameAcco)) { return(true); } else { return(false); } }
/// <summary> /// check if user is in ignored list /// </summary> /// <param name="user">user to check</param> /// <returns>is in ignored</returns> private bool isUserInIgnored(AdUser user) { List <AdUser> adUserIngnoredList = new List <AdUser>() { new AdUser { nameAcco = "testUser10" }, new AdUser { nameAcco = "testUser9" }, new AdUser { nameAcco = "testUser8" }, new AdUser { nameAcco = "testUser7" }, new AdUser { nameAcco = "testUser6" }, new AdUser { nameAcco = "testUser5" }, new AdUser { nameAcco = "testUser4" }, new AdUser { nameAcco = "testUser3" }, new AdUser { nameAcco = "testUser2" }, new AdUser { nameAcco = "testUser1" }, new AdUser { nameAcco = "ftester" } }; AdUser userInList = adUserIngnoredList.Find(x => x.nameAcco.Equals(user.nameAcco)); if (userInList != null) { return(true); } else { return(false); } }
/// <summary> /// number redistribution based on information from AD /// can write tIpPhone and change "," in numbers to ";" /// </summary> /// <param name="adUser">CSV user</param> /// <param name="csvUser">AD user</param> /// <returns>user with redistributed numbers</returns> private AdUser redistributionBasedOnAd(AdUser adUser, AdUser csvUser) { AdUser localUser = csvUser; if ((localUser.tIpPhone.Equals("")) && (!adUser.tTelOthers.Equals(""))) { //Console.WriteLine(); localUser.tIpPhone = adUser.tTelOthers; localUser.telephoneLog += $"(4) číslo přesunuto z (static) otherTelephone do ipPhone. [{adUser.tTelOthers}];"; } localUser.tTelOthers = localUser.tTelOthers.Replace(",", ";"); localUser.tMobOthers = localUser.tMobOthers.Replace(",", ";"); localUser.tIpPhoneOthers = localUser.tIpPhoneOthers.Replace(",", ";"); localUser.tHomePhoneOthers = localUser.tHomePhoneOthers.Replace(",", ";"); return(localUser); }
/// <summary> /// redistribution for user existing only in AD /// </summary> /// <param name="user">user</param> /// <returns>user with no telephone and check otherTelephone</returns> private AdUser redistributionNoInCsv(AdUser user) { if (!user.tIpPhone.Equals(user.tTelOthers)) { user.tIpPhone = user.tTelOthers; user.telephoneLog += $"(4) číslo přesunuto z (static) otherTelephone do ipPhone. [{user.tTelOthers}];"; } else { user.tIpPhone = ""; } user.tIpPhoneOthers = ""; user.tMob = ""; user.tMobOthers = ""; user.tHomePhone = ""; user.tHomePhoneOthers = ""; user.tTel = ""; return(user); }
/// <summary> /// load all user from AD to local list /// </summary> public void loadUserListAd() { userListAd = new List <AdUser>(); using (var context = new PrincipalContext(ContextType.Domain, "sitel.cz")) { using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) { foreach (var result in searcher.FindAll()) { AdUser localAdUser = new AdUser(); DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; if (de.Properties["samAccountName"].Value.ToString() != null) { localAdUser.nameGiven = "" + de.Properties["givenName"].Value; localAdUser.nameSurn = "" + de.Properties["sn"].Value; localAdUser.nameAcco = "" + de.Properties["samAccountName"].Value; localAdUser.nameFull = "" + de.Properties["userPrincipalName"].Value; localAdUser.tHomePhone = "" + de.Properties["homePhone"].Value; localAdUser.tHomePhoneOthers = "" + de.Properties["otherHomePhone"].Value; localAdUser.tIpPhone = "" + de.Properties["ipPhone"].Value; localAdUser.tIpPhoneOthers = "" + de.Properties["otherIpPhone"].Value; localAdUser.tMob = "" + de.Properties["Mobile"].Value; localAdUser.tMobOthers = "" + de.Properties["otherMobile"].Value; localAdUser.tTel = "" + de.Properties["TelephoneNumber"].Value; localAdUser.tTelOthers = "" + de.Properties["otherTelephone"].Value; } else { Exception exc = new Exception("Error in load users from AD. Foud user with no account name. Loading Closed."); throw exc; } userListAd.Add(localAdUser); } } } }
/// <summary> /// return final diference between userListFinal and userListAd /// </summary> /// <returns>list of needed changes in AD</returns> public List <telephoneUser> getFinalDiferences() { List <telephoneUser> finalList = new List <telephoneUser>(); foreach (AdUser user in userListAd) { /*if (user.nameAcco.Equals("jvaldauf")) * { * Console.WriteLine(); * }*/ AdUser userfound = userListFinal.Find(x => x.nameAcco.Equals(user.nameAcco)); telephoneUser diferenceUser = new telephoneUser(user.nameAcco); //user was found in List if (userfound != null) { //check diference between numbers if (!user.tTel.Equals(userfound.tTel)) { //editLog($" {user.nameAcco} (tTel): z [{user.tTel}] , na [{userfound.tTel}] "); diferenceUser.attributes.Add("TelephoneNumber"); diferenceUser.attribData.Add(userfound.tTel); editLog($" {user.nameAcco} ({"TelephoneNumber"}) - z [{user.tTel}], na [{userfound.tTel}]"); } /* static field for impot from AD * if (!user.tTelOthers.Equals(userfound.tTelOthers)) * { * editLog($" {user.nameAcco} (tTelOthers): z [{user.tTelOthers}] , na [{userfound.tTelOthers}] "); * }*/ if (!user.tIpPhone.Equals(userfound.tIpPhone)) { //editLog($" {user.nameAcco} (tIpPhone): z [{user.tIpPhone}] , na [{userfound.tIpPhone}] "); diferenceUser.attributes.Add("ipPhone"); diferenceUser.attribData.Add(userfound.tIpPhone); editLog($" {user.nameAcco} ({"ipPhone"}) - z [{user.tIpPhone}], na [{userfound.tIpPhone}]"); } if (!user.tIpPhoneOthers.Equals(userfound.tIpPhoneOthers)) { //editLog($" {user.nameAcco} (tIpPhoneOthers): z [{user.tIpPhoneOthers}] , na [{userfound.tIpPhoneOthers}] "); diferenceUser.attributes.Add("otherIpPhone"); diferenceUser.attribData.Add(userfound.tIpPhoneOthers); editLog($" {user.nameAcco} ({"otherIpPhone"}) - z [{user.tIpPhoneOthers}], na [{userfound.tIpPhoneOthers}]"); } if (!user.tMob.Equals(userfound.tMob)) { //editLog($" {user.nameAcco} (tMob): z [{user.tMob}] , na [{userfound.tMob}] "); diferenceUser.attributes.Add("Mobile"); diferenceUser.attribData.Add(userfound.tMob); editLog($" {user.nameAcco} ({"Mobile"}) - z [{user.tMob}], na [{userfound.tMob}]"); } if (!user.tMobOthers.Equals(userfound.tMobOthers)) { //editLog($" {user.nameAcco} (tMobOthers): z [{user.tMobOthers}] , na [{userfound.tMobOthers}] "); diferenceUser.attributes.Add("otherMobile"); diferenceUser.attribData.Add(userfound.tMobOthers); editLog($" {user.nameAcco} ({"otherMobile"}) - z [{user.tMobOthers}], na [{userfound.tMobOthers}]"); } if (!user.tHomePhone.Equals(userfound.tHomePhone)) { //editLog($" {user.nameAcco} (tHomePhone): z [{user.tHomePhone}] , na [{userfound.tHomePhone}] "); diferenceUser.attributes.Add("homePhone"); diferenceUser.attribData.Add(userfound.tHomePhone); editLog($" {user.nameAcco} ({"homePhone"}) - z [{user.tHomePhone}], na [{userfound.tHomePhone}]"); } if (!user.tHomePhoneOthers.Equals(userfound.tHomePhoneOthers)) { //editLog($" {user.nameAcco} (tHomePhoneOthers): z [{user.tHomePhoneOthers}] , na [{userfound.tHomePhoneOthers}] "); diferenceUser.attributes.Add("otherHomePhone"); diferenceUser.attribData.Add(userfound.tHomePhoneOthers); editLog($" {user.nameAcco} ({"otherHomePhone"}) - z [{user.tHomePhoneOthers}], na [{userfound.tHomePhoneOthers}]"); } //add changed user to list if (diferenceUser.attributes.Count() > 0) { finalList.Add(diferenceUser); } } } return(finalList); }
/// <summary> /// compare local "UserListAd" with sended list "listToCompare" and write diferences (local == From, sended == For) /// </summary> /// <param name="listToCompare">list to compare</param> /// <param name="showOnly">no changes in AD</param> public void writeDiferencesToAd(List <AdUser> listToCompare, bool showOnly) { List <string> adUserFindedByCsv = new List <string>(); List <string> adUserDefault = new List <string>(); List <string> adUserIgnored = new List <string>(); foreach (AdUser user in userListAd) { List <string> attributes = new List <string>(); List <string> attribData = new List <string>(); //search equalent in csv list AdUser userInCsv = listToCompare.Find(x => x.nameAcco.Equals(user.nameAcco)); bool ignoreThisUser = isUserInIgnored(user); //user isnt in ignore list if (!ignoreThisUser) { //user was found in CSV List if (userInCsv != null) { //check difference between current attributes and edited attribute, mark changes for edit List <List <string> > list = markDiferencesAgainCsv(user, userInCsv); attributes = list[0]; attribData = list[1]; adUserFindedByCsv.Add(user.nameAcco); } else { //user isnt in CSV list //clear all number atributes and add static number List <List <string> > list = markDiferencesDefault(user); attributes = list[0]; attribData = list[1]; adUserDefault.Add(user.nameAcco); } //write changes to AD if (attributes.Count() > 0) { string nameAcco = user.nameAcco; if (!showOnly) { try { editUserInAd(nameAcco, attributes.ToArray(), attribData.ToArray()); editLog($" --(Uživatel upraven {nameAcco})------------------------"); } catch (Exception ex) { errorLog($"Exception AdConnection:\n\n {ex.ToString()}"); } } } } //ignored user else { adUserIgnored.Add(user.nameAcco); } }// Console.WriteLine($"Uživatelů zkontrolováno vuci CSV: [{adUserFindedByCsv.Count()}]"); Console.WriteLine($"Uživatelů zkontrolováno default : [{adUserDefault.Count()}]"); Console.WriteLine($"Uživatelů přeskočeno : [{adUserIgnored.Count()}]"); }
/// <summary> /// return 2 List with data to change. /// </summary> /// <param name="user">user list</param> /// <param name="userInCsv">CSV user list</param> /// <returns>[0] == List<string> attributes, [1] == List<string> attribData</returns> private List <List <string> > markDiferencesAgainCsv(AdUser user, AdUser userInCsv) { List <List <string> > finalList = new List <List <string> >(); List <string> attributes = new List <string>(); List <string> attribData = new List <string>(); //redistribute telephones based on AD atributes userInCsv = lastRedistribution(user, userInCsv); //check diference between numbers if (!user.tTel.Equals(userInCsv.tTel)) { editLog($" {user.nameAcco} (tTel): z [{user.tTel}] , na [{userInCsv.tTel}] "); attributes.Add("TelephoneNumber"); attribData.Add(userInCsv.tTel); } //static field for impot from AD //if (!user.tTelOthers.Equals(userInCsv.tTelOthers)) //{ // editLog($" {user.nameAcco} (tTelOthers): z [{user.tTelOthers}] , na [{userInCsv.tTelOthers}] "); //} if (!user.tIpPhone.Equals(userInCsv.tIpPhone)) { editLog($" {user.nameAcco} (tIpPhone): z [{user.tIpPhone}] , na [{userInCsv.tIpPhone}] "); attributes.Add("ipPhone"); attribData.Add(userInCsv.tIpPhone); } if (!user.tIpPhoneOthers.Equals(userInCsv.tIpPhoneOthers)) { editLog($" {user.nameAcco} (tIpPhoneOthers): z [{user.tIpPhoneOthers}] , na [{userInCsv.tIpPhoneOthers}] "); attributes.Add("otherIpPhone"); attribData.Add(userInCsv.tIpPhoneOthers); } if (!user.tMob.Equals(userInCsv.tMob)) { editLog($" {user.nameAcco} (tMob): z [{user.tMob}] , na [{userInCsv.tMob}] "); attributes.Add("Mobile"); attribData.Add(userInCsv.tMob); } if (!user.tMobOthers.Equals(userInCsv.tMobOthers)) { editLog($" {user.nameAcco} (tMobOthers): z [{user.tMobOthers}] , na [{userInCsv.tMobOthers}] "); attributes.Add("otherMobile"); attribData.Add(userInCsv.tMobOthers); } if (!user.tHomePhone.Equals(userInCsv.tHomePhone)) { editLog($" {user.nameAcco} (tHomePhone): z [{user.tHomePhone}] , na [{userInCsv.tHomePhone}] "); attributes.Add("homePhone"); attribData.Add(userInCsv.tHomePhone); } if (!user.tHomePhoneOthers.Equals(userInCsv.tHomePhoneOthers)) { editLog($" {user.nameAcco} (tHomePhoneOthers): z [{user.tHomePhoneOthers}] , na [{userInCsv.tHomePhoneOthers}] "); attributes.Add("otherHomePhone"); attribData.Add(userInCsv.tHomePhoneOthers); } finalList.Add(attributes); finalList.Add(attribData); return(finalList); }