public bool InteractGroup(EspackGroup Group) { Exchange.EC.ServerName = ServerName; #if DEBUG Console.WriteLine(Group.GroupCode); #endif try { Group.ServiceCommands.Add(new ServiceCommand() { Command = Exchange.CreateGroup(Group.GroupCode, "distribution", AD.DefaultPathAliases) }); Group.ServiceCommands.Add(new ServiceCommand() { Command = Exchange.CleanGroup(Group.GroupCode) }); if (Group.GroupMembers.Count() != 0) { foreach (var _member in Group.GroupMembers) { var _memberName = _member.Split('@')[0]; //var _path = AD.DefaultPath; if (!Domains.Contains(_member.Split('@')[1])) { _memberName = _member.ToUpper().Replace("@", "_AT_"); Group.ServiceCommands.Add(new ServiceCommand() { Command = Exchange.CreateContact(_memberName, _member, AD.DefaultPathContacts) }); } Group.ServiceCommands.Add(new ServiceCommand() { Command = Exchange.AddMemberToGroup(_memberName, Group.GroupCode) }); } } return(true); } catch (Exception ex) { return(false); throw ex; } }
private async Task Synchronize() { var prevState = Values.conn.State; if (prevState == ConnectionState.Closed) { await Values.conn.OpenAsync(); SqlCommand cmd = Values.conn.CreateCommand(); string lSql = "set CONTEXT_INFO @C"; cmd.CommandText = lSql; SqlParameter param = new SqlParameter(); param.ParameterName = "@C"; param.DbType = DbType.Binary; param.Size = 128; param.Value = Values.MasterPassword; cmd.Parameters.Add(param); await cmd.ExecuteNonQueryAsync(); } //using (SqlDataAdapter _sDA = new SqlDataAdapter("select * from Users where UserCode = 'guest' and (GETDATE() >= PasswordEXP or PasswordEXP is null)", Values.conn)) //{ // DataTable T = new DataTable(); // _sDA.Fill(T); // var _RS = T.Rows; // if (_RS.Count != 0) // { // //generate new password // var _newPassword = CT.GeneratePassword(8); // //change guest password // using (var _sp = new SqlCommand("pUppUserPassword", Values.conn) { CommandType = CommandType.StoredProcedure }) // { // //prevState = Values.conn.State; // //if (prevState == ConnectionState.Closed) // // await Values.conn.OpenAsync(); // SqlCommandBuilder.DeriveParameters(_sp); // _sp.Parameters["@msg"].Value = ""; // _sp.Parameters["@UserCode"].Value = "guest"; // _sp.Parameters["@Password"].Value = _newPassword; // _sp.Parameters["@PIN"].Value = "0000"; // int _error = 0; // try // { // await _sp.ExecuteNonQueryAsync(); // } // catch (Exception ex) // { // EventLog.WriteEntry(string.Format("Error updating guest password: {0}", ex.Message), EventLogEntryType.Error); // _error = 1; // } // if (_error == 0 && _sp.Parameters["@msg"].Value.ToString() != "OK") // { // EventLog.WriteEntry(string.Format("Error updating guest password: {0}", _sp.Parameters["@msg"].Value.ToString()), EventLogEntryType.Error); // } // if (_error == 0 && _sp.Parameters["@msg"].Value.ToString() == "OK") // { // EventLog.WriteEntry("Guest user password changed correctly."); // } // } // } //} using (var _sDA = new SqlDataAdapter(string.Format("select top 10 UserCode, Name, Surname1,Password,Position, MainCOD3, emailAddress, localDomain, flags, desCOD3,Area, remaining=(Select count(*) from users where dbo.CheckFlag(flags,'CHANGED')=1),ExchangeDatabase from vUsers where dbo.CheckFlag(flags,'CHANGED')=1 order by Surname1 desc"), Values.conn)) { using (DataTable T = new DataTable()) { try { _sDA.Fill(T); } catch (Exception ex) { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("Error accesing database: {0}", ex.Message), EventLogEntryType.Error); cMiscTools.RegisterMessage($"Error accesing database: {ex.Message}", EventLogEntryType.Error); return; } var _RS = T.Rows; if (_RS.Count != 0) { Collection <EspackUser> users = new Collection <EspackUser>(); foreach (DataRow r in _RS) { #if DEBUG Console.WriteLine(r["UserCode"].ToString()); #endif //var r = _RS.Rows[0]; Collection <string> _flags = r["flags"].ToString().Split('|').ToList().Where(fr => Values.FlagsDefs.Keys.Contains(fr)).ToCollection(); //create the user object var _user = new EspackUser() { UserCode = r["UserCode"].ToString(), Name = r["Name"].ToString().ToUpperFirstLetter(), Surname = r["Surname1"].ToString().ToUpperFirstLetter(), Group = "ESPACK_" + r["Position"].ToString(), Password = r["Password"].ToString(), Email = r["emailAddress"].ToString(), Sede = new EspackSede() { COD3 = r["MainCOD3"].ToString(), COD3Description = r["desCOD3"].ToString() }, Flags = _flags.ToCollection(), Position = r["Position"].ToString(), Area = r["Area"].ToString(), Services = r["flags"].ToString().Split('|').Where(s => SyncedServices.Select(ss => ss.ServiceName).Contains(s)).ToCollection(), LocalDomain = r["localDomain"].ToString(), ExchangeDatabase = r["ExchangeDatabase"].ToString(), }; users.Add(_user); } foreach (var s in SyncedServices) { var _userServices = users.Where(u => u.Services.Contains(s.ServiceName)); if (_userServices.Count() != 0) { foreach (var _user in _userServices) { //_flags.Remove(s.ServiceName); //define the alias list var _alias = Values.DomainList.Select(d => string.Format("'smtp:{0}@{1}'", _user.UserCode, d)); _user.Aliases = new Collection <string>(_alias.Concat(new string[] { string.Format("'smtp:{0}@{1}'", _user.UserCode, _user.LocalDomain) }).ToCollection()); //get the command list for the user s.Interact(_user); } //execute the commands for the user list var commandCollection = _userServices.SelectMany(u => u.ServiceCommands).ToCollection(); try { await s.Commit(commandCollection); } catch (Exception ex) { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("Error interacting users {0}.", ex.Message), EventLogEntryType.Error); cMiscTools.RegisterMessage($"Error interacting users {ex.Message}.", EventLogEntryType.Error); } } foreach (var _user in users) { await _user.DoFlags(Values.conn); var errorMessages = _user.ServiceCommands.Select(c => c.Result).Where(o => o != "OK"); var errorMessage = string.Join("· ", errorMessages); if (errorMessages.Count() == 0) { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("User {0} from {1} was modified correctly in service {2}", _user.UserCode, _user.Sede.COD3, s.ServiceName)); cMiscTools.RegisterMessage($"User {_user.UserCode} from {_user.Sede.COD3} was modified correctly in service {s.ServiceName}"); } else { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("User {0} from {1} was NOT modified correctly in service {2}. \nError message was {3}", _user.UserCode, _user.Sede.COD3, s.ServiceName, errorMessage), EventLogEntryType.Error); cMiscTools.RegisterMessage($"User {_user.UserCode} from {_user.Sede.COD3} was NOT modified correctly in service {s.ServiceName}.\nError message was {errorMessage}", EventLogEntryType.Error); } _user.ServiceCommands.Clear(); } } } } } //now lets sync the groups //Values.gDatos.DataBase = "MAIL"; //Values.gDatos.Connect(); using (var _sDA = new SqlDataAdapter("Select local_part,address,flags from mail..aliasCAB where dbo.CheckFlag(flags,'CHANGED')=1 and local_part!='' order by address", Values.conn)) { DataTable T = new DataTable(); _sDA.SelectCommand.CommandTimeout = 300; try { _sDA.Fill(T); } catch (Exception ex) { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("Error accesing database {0}", ex.Message), EventLogEntryType.Error); cMiscTools.RegisterMessage($"Error accesing database {ex.Message}", EventLogEntryType.Error); return; } var _RS = T.Rows; Collection <EspackGroup> groups = new Collection <EspackGroup>(); foreach (DataRow r in _RS) //_RS.ToList().ForEach(async r => { var _flags = r["flags"].ToString().Split('|'); //lets get the group members //using (var _aliases = new SqlDataAdapter(string.Format("select * from MAIL.dbo.fExpandAlias('{0}') where gotoAddress not in (select gotoAddress from MAIL.dbo.fExpandAliasExceptions('{0}'))", r["address"].ToString()), Values.conn)) //using (var _aliases = new SqlDataAdapter(string.Format("EXEC Mail..pExpandAlias '{0}'", r["address"].ToString()), Values.conn)) using (var _aliases = new SqlCommand("MAil..pExpandAlias", Values.conn) { CommandType = CommandType.StoredProcedure }) { _aliases.Parameters.Add(new SqlParameter() { ParameterName = "@address", DbType = DbType.String, Value = r["address"].ToString() }); var tAliases = new DataTable(); try { tAliases.Load(await _aliases.ExecuteReaderAsync()); //_aliases.Fill(tAliases); } catch (Exception ex) { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("Error accesing database {0}", ex.Message), EventLogEntryType.Error); cMiscTools.RegisterMessage($"Error accesing database {ex.Message}", EventLogEntryType.Error); return; } var _alList = tAliases.Rows.OfType <DataRow>().Select(a => string.Format("{0}@{1}", a["local_part_goto"], CleanDomain(a["domain_goto"].ToString()))).ToArray(); //create the group object foreach (var s in SyncedServices) { var _group = new EspackGroup() { GroupCode = r["local_part"].ToString() != "" ? r["local_part"].ToString() : r["address"].ToString().Replace('@', '_').ToUpper(), GroupMail = r["address"].ToString(), GroupMembers = _alList }; if (_flags.Contains(s.ServiceName)) { s.InteractGroup(_group); groups.Add(_group); } //var commandCollection = _group.SelectMany(u => u.ServiceCommands).ToCollection(); try { await s.Commit(_group.ServiceCommands); } catch (Exception ex) { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("Error interacting groups {0}.", ex.Message), EventLogEntryType.Error); cMiscTools.RegisterMessage($"Error interacting groups {ex.Message}.", EventLogEntryType.Error); } foreach (var group in groups) { await group.DoFlags(Values.conn); var errorMessages = group.ServiceCommands.Select(c => c.Result).Where(o => o != "OK"); var errorMessage = string.Join("· ", errorMessages); if (errorMessages.Count() == 0) { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("Group {0} was modified correctly in service {1}", group.GroupCode, s.ServiceName)); cMiscTools.RegisterMessage($"Group {group.GroupCode} was modified correctly in service {s.ServiceName}"); } else { // [dvalles] 20220608: EventLog not working on DEBUG mode. Replaced by a function with does the stuff depending on the DEBUG status //EventLog.WriteEntry(string.Format("Group {0} was NOT modified correctly in service {1}. \nError message was {2}", group.GroupCode, s.ServiceName, errorMessage), EventLogEntryType.Error); cMiscTools.RegisterMessage($"Group {group.GroupCode} was NOT modified correctly in service {s.ServiceName}.\nError message was {errorMessage}", EventLogEntryType.Error); } group.ServiceCommands.Clear(); } } } } } Values.conn.Close(); }
public bool InteractGroup(EspackGroup Group) { return(true); }