//public async Task<bool> CheckExist(string UserCode) //{ // return await Exchange.CheckUser(UserCode); //} public bool Interact(EspackUser User) { Exchange.EC.ServerName = ServerName; User.ServiceCommands.Add(new ServiceCommand() { Command = Exchange.EnableMailbox(User.UserCode, User.ExchangeDatabase) }); return(true); }
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 async Task<bool> CheckExist(string UserCode) //{ // return await AD.CheckUser(UserCode); //} public bool Interact(EspackUser User) { try { AD.EC.ServerName = ServerName; if (User.Flags.Contains("DIS")) { User.ServiceCommands.Add(new ServiceCommand() { Command = AD.UpdateUser(User.Name, User.Surname, User.UserCode, User.Password, User.Email, User.Sede.COD3, User.Position, User.Area, User.Sede.COD3Description) }); User.ServiceCommands.Add(new ServiceCommand() { Command = AD.DisableUser(User.UserCode) }); return(true); } // insert or update the user User.ServiceCommands.Add(new ServiceCommand() { Command = AD.InteractUser(User.Name, User.Surname, User.UserCode, User.Password, User.Email, User.Sede.COD3, User.Position, User.Area, User.Sede.COD3Description) }); // create the OU if not exists User.ServiceCommands.Add(new ServiceCommand() { Command = AD.CreateOrganizationalUnit(User.Sede.COD3, User.Sede.COD3Description, AD.DefaultPath) }); // move to the OU if not there User.ServiceCommands.Add(new ServiceCommand() { Command = AD.MoveUserToOU(User.UserCode, User.Sede.COD3) }); // remove from non present flag groups foreach (var f in Flags) { if (!User.Flags.Contains(f.Key)) { User.ServiceCommands.Add(new ServiceCommand() { Command = AD.RemoveUserFromGroup(User.UserCode, f.Value, AD.DefaultPath) }); } } // add to flag groups if (User.Flags != null) { foreach (var f in User.Flags) { User.ServiceCommands.Add(new ServiceCommand() { Command = AD.CreateGroup(Flags[f], Flags[f], "Security", AD.DefaultPath) }); User.ServiceCommands.Add(new ServiceCommand() { Command = AD.AddUserToGroup(User.UserCode, Flags[f], GroupPath: AD.DefaultPath) }); } } return(true); } catch (Exception ex) { throw ex; } }