public async Task SaveStatusOnlyAsync(InstanceInfo[] newInfo) { MsSqlMonitorEntities context = new MsSqlMonitorEntities(); MapBaseInstanceDataToContext(newInfo, context); await context.SaveChangesAsync().ConfigureAwait(false); }
public async Task GrantInstanceAccess(int instanceId, int userId) { using (MsSqlMonitorEntities context = new MsSqlMonitorEntities()) { Assign assign = await context.Assigns.FirstOrDefaultAsync(a => a.InstanceId == instanceId && a.UserId == userId); if (assign != null) { throw new Exception($"User '{userId}' already has assign to instance '{instanceId}'"); } Instance instance = await context.Instances.FirstOrDefaultAsync(inst => inst.Id == instanceId); if (instance == null) { throw new EntityNotExistException($"There is no instance with id '{instanceId}' in the database"); } User user = await context.Users.FirstOrDefaultAsync(u => u.Id == userId); if (user == null) { throw new EntityNotExistException($"There is no user with id '{userId}' in the database"); } context.Assigns.Add(new Assign() { InstanceId = instanceId, UserId = userId }); await context.SaveChangesAsync(); } }
public async void RemoveInstanceForeverAsync(CollectionResult result, ISLogger logger) { logger.Debug("Removing forever instance id=" + result.InstanceID); try { MsSqlMonitorEntities context = new MsSqlMonitorEntities(); Instance instance = context.Instances.Find(result.InstanceID); instance.Assigns.ToList().ForEach(i => context.Assigns.Remove(i)); instance.Roles.ToList().ForEach(r => { r.RefuseAllLogins(); context.Entry(r).State = EntityState.Deleted; }); // Delete all users before update. instance.Logins.ToList().ForEach(l => context.Entry(l).State = EntityState.Deleted); // Delete all roles from databases instance.Databases.ToList().ForEach(db => db.Roles.ToList().ForEach(role => { role.RefuseAllUsers(); context.Entry(role).State = EntityState.Deleted; })); instance.Databases.ToList().ForEach(db => db.Users.ToList().ForEach(user => { user.RefuseAllRoles(); context.Entry(user).State = EntityState.Deleted; })); // Delete all databases instance.Databases.ToList().ForEach(db => context.Entry(db).State = EntityState.Deleted); context.Entry(instance).State = EntityState.Deleted; await context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception e) { logger.Error(e.StackTrace); } }
public async Task SaveInstancesAsync(InstanceInfo[] newInfo) { using (MsSqlMonitorEntities context = new MsSqlMonitorEntities()) { MapWholeInstanceDataToContext(context, newInfo); await context.SaveChangesAsync().ConfigureAwait(false); } }
public async Task ShowInstance(int instanceId, int userId) { using (MsSqlMonitorEntities context = new MsSqlMonitorEntities()) { Assign assign = await context.Assigns.FirstOrDefaultAsync(a => a.InstanceId == instanceId && a.UserId == userId); if (assign == null) { throw new EntityNotExistException($"Assignment of user '{userId}' to instance '{instanceId}' doesn't exist"); } assign.IsHidden = false; await context.SaveChangesAsync(); } }
public async Task RevokeInstanceAccess(int instanceId, int userId) { using (MsSqlMonitorEntities context = new MsSqlMonitorEntities()) { Assign assign = await context.Assigns.FirstOrDefaultAsync(a => a.InstanceId == instanceId && a.UserId == userId); if (assign == null) { throw new EntityNotExistException($"Assignment of user '{userId}' to instance '{instanceId}' doesn't exist"); } context.Entry(assign).State = EntityState.Deleted; await context.SaveChangesAsync(); } }
public async Task <InstanceViewModel> RecoverInstance(int instanceId) { using (var context = new MsSqlMonitorEntities()) { Instance instToRecover = await context.Instances.FirstOrDefaultAsync(inst => inst.Id == instanceId); if (instToRecover == null) { throw new EntityNotExistException($"Instance with id '{instanceId}' that you are trying to recover doesn't exist"); } instToRecover.IsDeleted = false; await context.SaveChangesAsync(); return(instToRecover.ToViewModel()); } }
public async Task <InstanceViewModel> DeleteInstance(int instanceId) { using (var context = new MsSqlMonitorEntities()) { Instance instToDelete = await context.Instances.FirstOrDefaultAsync(inst => inst.Id == instanceId); if (instToDelete == null) { throw new EntityNotExistException($"Instance with id '{instanceId}' that you try to delete doesn't exist"); } instToDelete.IsDeleted = true; instToDelete.IsDeletedTime = DateTime.Now; await context.SaveChangesAsync(); return(instToDelete.ToViewModel()); } }
public async Task SaveStatusOnlyAsync(InstanceInfo[] newInfo, ISLogger logger) { try { MsSqlMonitorEntities context = new MsSqlMonitorEntities(); MapBaseInstanceDataToContext(newInfo, context); await context.SaveChangesAsync().ConfigureAwait(false); } catch (AggregateException e) { foreach (Exception ex in e.InnerExceptions) { logger.Error("LocalDb.SaveSatusOnly " + ex.Message); } logger.Error(e.StackTrace); } }
public async Task <InstanceViewModel> UpdateInstance(InstanceViewModel source) { if (!source.Id.HasValue) { throw new InvalidModelException("Instance id is required."); } using (var context = new MsSqlMonitorEntities()) { Instance instToUpdate = await context.Instances.FirstOrDefaultAsync(inst => inst.Id == source.Id); if (instToUpdate == null) { throw new EntityNotExistException($"Instance with id '{source.Id}' that you try to update is not exist"); } if (!String.IsNullOrEmpty(source.ServerName)) { instToUpdate.ServerName = source.ServerName; } if (!String.IsNullOrEmpty(source.InstanceName)) { instToUpdate.InstanceName = source.InstanceName; } if (!String.IsNullOrEmpty(source.Login)) { instToUpdate.Login = source.Login; } if (!String.IsNullOrEmpty(source.Password)) { string encryptionKey; instToUpdate.Password = encryptionManager.Encrypt(out encryptionKey, source.Password); instToUpdate.EncryptionKey = encryptionKey; } if (source.IsWindowsAuthentication.HasValue) { instToUpdate.Authentication = source.IsWindowsAuthentication.Value ? AuthenticationType.Windows : AuthenticationType.Sql; } await context.SaveChangesAsync(); return(instToUpdate.ToViewModel()); } }
public async Task <InstanceViewModel> AddInstance(InstanceViewModel newInstance) { if (String.IsNullOrEmpty(newInstance.InstanceName) || String.IsNullOrEmpty(newInstance.ServerName) || String.IsNullOrEmpty(newInstance.Login) || String.IsNullOrEmpty(newInstance.Password) || !newInstance.IsWindowsAuthentication.HasValue) { throw new InvalidModelException("Instance name, Server name, Login, Password, and authentication type are required."); } string encryptionKey; var encryptedPassword = encryptionManager.Encrypt(out encryptionKey, newInstance.Password); using (var context = new MsSqlMonitorEntities()) { if (null != await context.Instances.FirstOrDefaultAsync(inst => inst.ServerName.ToUpper() == newInstance.ServerName.ToUpper() && inst.InstanceName.ToUpper() == newInstance.InstanceName.ToUpper())) { throw new ObjectAlreadyExistException("Instance already exist in the system."); } Instance result = new Instance() { ServerName = newInstance.ServerName, InstanceName = newInstance.InstanceName, Password = encryptedPassword, EncryptionKey = encryptionKey, Login = newInstance.Login, Authentication = newInstance.IsWindowsAuthentication.Value ? AuthenticationType.Windows : AuthenticationType.Sql }; context.Instances.Add(result); await context.SaveChangesAsync(); return(result.ToViewModel()); } }
public async Task <int> SaveAsync() { return(await context.SaveChangesAsync()); }