public IEnumerable <RequestLogEntry> GetRecentLogEntriesForLink(Guid linkId, int amount) { using (var context = new RelayContext()) { return (context.RequestLogEntries.Where(r => r.LinkId == linkId) .OrderByDescending(e => e.OnPremiseConnectorOutDate) .Take(amount) .ToList() .Select(d => new RequestLogEntry { OnPremiseConnectorInDate = d.OnPremiseConnectorInDate, OnPremiseConnectorOutDate = d.OnPremiseConnectorOutDate, ContentBytesIn = d.ContentBytesIn, ContentBytesOut = d.ContentBytesOut, HttpStatusCode = d.HttpStatusCode, OnPremiseTargetInDate = d.OnPremiseTargetInDate, OnPremiseTargetOutDate = d.OnPremiseTargetOutDate, OnPremiseTargetKey = d.OnPremiseTargetKey, LocalUrl = d.LocalUrl, OriginId = d.OriginId, LinkId = d.LinkId, })); } }
public bool IsUserNameAvailable(string userName) { using (var context = new RelayContext()) { return(!context.Links.Any(p => p.UserName == userName)); } }
public PageResult <Link> GetLinks(PageRequest paging) { using (var context = new RelayContext()) { var query = context.Links.AsQueryable(); if (!string.IsNullOrWhiteSpace(paging.SearchText)) { var searchText = paging.SearchText.ToLower(); query = query.Where(w => w.UserName.ToLower().Contains(searchText) || w.SymbolicName.ToLower().Contains(searchText)); } // Default sorting must be provided if (string.IsNullOrWhiteSpace(paging.SortField)) { paging.SortField = "SymbolicName"; paging.SortDirection = SortDirection.Asc; } var count = query.Count(); query = query.OrderByPropertyName(paging.SortField, paging.SortDirection); query = query.ApplyPaging(paging); var queryResult = query.ToList().Select(GetLinkFromDbLink).ToList(); var result = new PageResult <Link> { Items = queryResult, Count = count }; return(result); } }
public Guid Create(string userName, string password) { if ((String.IsNullOrWhiteSpace(userName)) || (String.IsNullOrWhiteSpace(password))) { return(Guid.Empty); } var passwordInformation = _passwordHash.CreatePasswordInformation(Encoding.UTF8.GetBytes(password)); var dbUser = new DbUser { Id = Guid.NewGuid(), Iterations = passwordInformation.Iterations, Password = passwordInformation.Hash, Salt = passwordInformation.Salt, UserName = userName, CreationDate = DateTime.Now, }; using (var context = new RelayContext()) { context.Users.Add(dbUser); context.SaveChanges(); return(dbUser.Id); } }
public bool Any() { using (var context = new RelayContext()) { return(context.Users.Any()); } }
public CreateLinkResult CreateLink(string symbolicName, string userName) { using (var context = new RelayContext()) { var password = _passwordHash.GeneratePassword(_configuration.LinkPasswordLength); var passwordInformation = _passwordHash.CreatePasswordInformation(password); var link = new DbLink { Id = Guid.NewGuid(), Password = passwordInformation.Hash, Salt = passwordInformation.Salt, Iterations = passwordInformation.Iterations, SymbolicName = symbolicName, UserName = userName, CreationDate = DateTime.UtcNow }; context.Links.Add(link); context.SaveChanges(); var result = new CreateLinkResult { Id = link.Id, Password = Convert.ToBase64String(password) }; return(result); } }
public void LogRequest(RequestLogEntry requestLogEntry) { using (var context = new RelayContext()) { var link = new DbLink { Id = requestLogEntry.LinkId }; context.Links.Attach(link); context.RequestLogEntries.Add(new DbRequestLogEntry { Id = Guid.NewGuid(), ContentBytesIn = requestLogEntry.ContentBytesIn, ContentBytesOut = requestLogEntry.ContentBytesOut, OnPremiseConnectorInDate = requestLogEntry.OnPremiseConnectorInDate, OnPremiseConnectorOutDate = requestLogEntry.OnPremiseConnectorOutDate, HttpStatusCode = requestLogEntry.HttpStatusCode, OnPremiseTargetInDate = requestLogEntry.OnPremiseTargetInDate, OnPremiseTargetKey = requestLogEntry.OnPremiseTargetKey, OnPremiseTargetOutDate = requestLogEntry.OnPremiseTargetOutDate, LocalUrl = requestLogEntry.LocalUrl ?? "/", OriginId = requestLogEntry.OriginId, Link = link, LinkId = requestLogEntry.LinkId, }); context.SaveChanges(); } }
public IEnumerable <Link> GetLinks() { using (var context = new RelayContext()) { return(context.Links.ToList().Select(GetLinkFromDbLink).ToList()); } }
public bool Update(Guid id, string password) { if ((id == Guid.Empty) || (String.IsNullOrWhiteSpace(password))) { return(false); } using (var context = new RelayContext()) { var dbUser = context.Users.SingleOrDefault(u => u.Id == id); if (dbUser == null) { return(false); } var passwordInformation = _passwordHash.CreatePasswordInformation(Encoding.UTF8.GetBytes(password)); dbUser.Iterations = passwordInformation.Iterations; dbUser.Salt = passwordInformation.Salt; dbUser.Password = passwordInformation.Hash; context.Entry(dbUser).State = EntityState.Modified; return(context.SaveChanges() == 1); } }
public async Task RenewActiveConnectionAsync(string connectionId) { _logger?.Verbose("Renewing last activity. connection-id={ConnectionId}", connectionId); try { using (var context = new RelayContext()) { var activeConnection = await context.ActiveConnections.FirstOrDefaultAsync(ac => ac.ConnectionId == connectionId).ConfigureAwait(false); if (activeConnection != null) { activeConnection.LastActivity = DateTime.UtcNow; context.Entry(activeConnection).State = EntityState.Modified; await context.SaveChangesAsync().ConfigureAwait(false); } } } catch (Exception ex) { _logger?.Error(ex, "Error during renewing an active connection. connection-id={ConnectionId}", connectionId); } }
public PageResult <LinkDetails> GetLinkDetails(PageRequest paging) { using (var context = new RelayContext()) { var linksQuery = context.Links.AsQueryable(); if (!String.IsNullOrWhiteSpace(paging.SearchText)) { var searchText = paging.SearchText.ToLower(); linksQuery = linksQuery.Where(w => w.UserName.Contains(searchText) || w.SymbolicName.Contains(searchText)); } // Default sorting must be provided if (String.IsNullOrWhiteSpace(paging.SortField)) { paging.SortField = "SymbolicName"; paging.SortDirection = SortDirection.Asc; } var numberOfLinks = linksQuery.Count(); linksQuery = linksQuery.OrderByPropertyName(paging.SortField, paging.SortDirection); linksQuery = linksQuery.ApplyPaging(paging); return(new PageResult <LinkDetails>() { Items = GetLinkDetailsFromDbLink(linksQuery).ToList(), Count = numberOfLinks, }); } }
public IEnumerable <LinkDetails> GetLinkDetails() { using (var context = new RelayContext()) { return(GetLinkDetailsFromDbLink(context.Links).ToList()); } }
public void DeleteLink(Guid linkId) { _logger?.Verbose("Removing link. link-id={LinkId}", linkId); try { using (var context = new RelayContext()) { var itemToDelete = new DbLink { Id = linkId, }; context.Links.Attach(itemToDelete); context.Links.Remove(itemToDelete); context.SaveChanges(); } } catch (DbUpdateConcurrencyException) { } catch (Exception ex) { _logger?.Error(ex, "Error while removing a link. link-id={LinkId}", linkId); } }
public bool UpdateLink(LinkDetails link) { using (var context = new RelayContext()) { var linkEntity = context.Links.SingleOrDefault(p => p.Id == link.Id); if (linkEntity == null) { return(false); } linkEntity.CreationDate = link.CreationDate; linkEntity.AllowLocalClientRequestsOnly = link.AllowLocalClientRequestsOnly; linkEntity.ForwardOnPremiseTargetErrorResponse = link.ForwardOnPremiseTargetErrorResponse; linkEntity.IsDisabled = link.IsDisabled; linkEntity.MaximumLinks = link.MaximumLinks; linkEntity.SymbolicName = link.SymbolicName; linkEntity.UserName = link.UserName; linkEntity.TokenRefreshWindow = link.TokenRefreshWindow; linkEntity.HeartbeatInterval = link.HeartbeatInterval; linkEntity.ReconnectMinWaitTime = link.ReconnectMinWaitTime; linkEntity.ReconnectMaxWaitTime = link.ReconnectMaxWaitTime; linkEntity.AbsoluteConnectionLifetime = link.AbsoluteConnectionLifetime; linkEntity.SlidingConnectionLifetime = link.SlidingConnectionLifetime; context.Entry(linkEntity).State = EntityState.Modified; return(context.SaveChanges() == 1); } }
public bool IsUserNameAvailable(string userName) { using (var context = new RelayContext()) { var user = context.Users.SingleOrDefault(u => u.UserName == userName); return(user == null); } }
private void UnlockUser(RelayContext ctx, DbUser user) { user.LastFailedLoginAttempt = null; user.FailedLoginAttempts = null; ctx.Entry(user).State = EntityState.Modified; ctx.SaveChanges(); _logger?.Information("Unlocking user account {UserName}", user.UserName); }
public Link GetLink(string linkName) { using (var context = new RelayContext()) { var linkQuery = context.Links .Where(p => p.UserName == linkName); return(GetLinkFromDbLink(linkQuery).FirstOrDefault()); } }
public LinkDetails GetLinkDetails(Guid linkId) { using (var context = new RelayContext()) { var linkQuery = context.Links .Where(l => l.Id == linkId); return(GetLinkDetailsFromDbLink(linkQuery).FirstOrDefault()); } }
public Guid?GetCurrentTraceConfigurationId(Guid linkId) { using (var context = new RelayContext()) { var traceConfiguration = context.TraceConfigurations.SingleOrDefault(t => t.StartDate <DateTime.UtcNow && t.EndDate> DateTime.UtcNow && t.LinkId == linkId); return(traceConfiguration?.Id); } }
public IEnumerable <ContentBytesChartDataItem> GetContentBytesChartDataItemsForLink(Guid id, TimeFrame timeFrame) { using (var context = new RelayContext()) { var query = CreateBasicChartDataQuery(context, id, timeFrame); var result = AggregateContentBytesChartData(query, timeFrame); return(result); } }
public IEnumerable <User> List() { using (var context = new RelayContext()) { return(context.Users.Select(u => new User { Id = u.Id, UserName = u.UserName, CreationDate = u.CreationDate }).ToList()); } }
private void RecordFailedLoginAttempt(RelayContext ctx, DbUser user) { user.LastFailedLoginAttempt = DateTime.UtcNow; user.FailedLoginAttempts = user.FailedLoginAttempts.GetValueOrDefault() + 1; ctx.Entry(user).State = EntityState.Modified; ctx.SaveChanges(); _logger?.Information("User {UserName} failed logging in for {FailedLoginAttempts} attempts", user.UserName, user.FailedLoginAttempts ); }
public Link GetLink(string linkName) { using (var context = new RelayContext()) { var link = context.Links.SingleOrDefault(p => p.UserName == linkName); if (link == null) { return(null); } return(GetLinkFromDbLink(link)); } }
public Link GetLink(Guid linkId) { using (var context = new RelayContext()) { var link = context.Links.SingleOrDefault(p => p.Id == linkId); if (link == null) { return(null); } return(GetLinkFromDbLink(link)); } }
public bool Delete(Guid id) { using (var context = new RelayContext()) { var dbUser = new DbUser { Id = id, }; context.Users.Attach(dbUser); context.Users.Remove(dbUser); return(context.SaveChanges() == 1); } }
public void DeleteLink(Guid linkId) { using (var context = new RelayContext()) { var itemToDelete = new DbLink { Id = linkId }; context.Links.Attach(itemToDelete); context.Links.Remove(itemToDelete); context.SaveChanges(); } }
public TraceConfiguration GetTraceConfiguration(Guid traceConfigurationId) { using (var context = new RelayContext()) { return(context.TraceConfigurations .Where(t => t.Id == traceConfigurationId) .Select(t => new TraceConfiguration { CreationDate = t.CreationDate, EndDate = t.EndDate, Id = t.Id, LinkId = t.LinkId, StartDate = t.StartDate }).SingleOrDefault()); } }
public async Task <bool> HasActiveConnectionAsync(Guid linkId) { _logger?.Verbose("Checking for active connections. link-id={LinkId}", linkId); try { using (var context = new RelayContext()) { return(await context.ActiveConnections.AnyAsync(ac => ac.LinkId == linkId)); } } catch (Exception ex) { _logger?.Error(ex, "Error during checking for active connections. link-id={LinkId}", linkId); return(false); } }
public IEnumerable <TraceConfiguration> GetTraceConfigurations(Guid linkId) { using (var context = new RelayContext()) { return(context.TraceConfigurations.Where(t => t.LinkId == linkId) .OrderByDescending(t => t.CreationDate) .Select(d => new TraceConfiguration { CreationDate = d.CreationDate, EndDate = d.EndDate, Id = d.Id, LinkId = d.LinkId, StartDate = d.StartDate }) .ToList()); } }
private IQueryable <DbRequestLogEntry> CreateBasicChartDataQuery(RelayContext context, Guid id, TimeFrame timeFrame) { var startDate = new DateTime(timeFrame.Start.Year, timeFrame.Start.Month, timeFrame.Start.Day); var endDate = new DateTime(timeFrame.End.Year, timeFrame.End.Month, timeFrame.End.Day, 23, 59, 59, 999); var query = context.RequestLogEntries .Where(log => log.OnPremiseConnectorInDate >= startDate) .Where(log => log.OnPremiseConnectorOutDate <= endDate); if (id != Guid.Empty) { query = query.Where(log => log.LinkId == id); } query = query.OrderBy(log => log.OnPremiseConnectorInDate); return(query); }