internal void RemoveUserFromOrganization(string requesterUserName, string targetUserID) { var requester = RemotelyContext.Users .Include(x => x.Organization) .ThenInclude(x => x.RemotelyUsers) .FirstOrDefault(x => x.UserName == requesterUserName); var target = requester.Organization.RemotelyUsers.FirstOrDefault(x => x.Id == targetUserID); var newOrganization = new Organization(); target.Organization = newOrganization; RemotelyContext.Organizations.Add(newOrganization); RemotelyContext.SaveChanges(); }
public void WriteLog(LogLevel logLevel, string category, EventId eventId, string state, Exception exception, List <string> scopeStack) { // Prevent re-entrancy. if (eventId.Name?.Contains("EntityFrameworkCore") == true) { return; } try { // TODO: Refactor EventLog to resemble these params. Replace WriteEvent with ILogger<T>. EventType eventType = EventType.Debug; switch (logLevel) { case LogLevel.None: case LogLevel.Trace: case LogLevel.Debug: eventType = EventType.Debug; break; case LogLevel.Information: eventType = EventType.Debug; break; case LogLevel.Warning: eventType = EventType.Warning; break; case LogLevel.Error: case LogLevel.Critical: eventType = EventType.Error; break; default: break; } RemotelyContext.EventLogs.Add(new EventLog() { StackTrace = exception?.StackTrace, EventType = eventType, Message = $"[{logLevel}] [{string.Join(" - ", scopeStack)} - {category}] | Message: {state} | Exception: {exception?.Message}", TimeStamp = DateTimeOffset.Now }); RemotelyContext.SaveChanges(); } catch { } }
public void AddOrUpdateCommandResult(CommandResult commandResult) { var existingContext = RemotelyContext.CommandResults.Find(commandResult.ID); if (existingContext != null) { var entry = RemotelyContext.Entry(existingContext); entry.CurrentValues.SetValues(commandResult); entry.State = EntityState.Modified; } else { RemotelyContext.CommandResults.Add(commandResult); } RemotelyContext.SaveChanges(); }
public void WriteEvent(string message, EventType eventType, string organizationID) { try { RemotelyContext.EventLogs.Add(new EventLog() { EventType = eventType, Message = message, TimeStamp = DateTimeOffset.Now, OrganizationID = organizationID }); RemotelyContext.SaveChanges(); } catch { } }
public void DeleteInvite(string orgID, string inviteID) { var invite = RemotelyContext.InviteLinks.FirstOrDefault(x => x.OrganizationID == orgID && x.ID == inviteID); var user = RemotelyContext.Users.FirstOrDefault(x => x.UserName == invite.InvitedUser); if (user != null && string.IsNullOrWhiteSpace(user.PasswordHash)) { RemotelyContext.Remove(user); } RemotelyContext.Remove(invite); RemotelyContext.SaveChanges(); }
public void UpdateDevice(string deviceID, string tag, string alias, string deviceGroupID, string notes) { var device = RemotelyContext.Devices.Find(deviceID); if (device == null) { return; } device.Tags = tag; device.DeviceGroupID = deviceGroupID; device.Alias = alias; device.Notes = notes; RemotelyContext.SaveChanges(); }
public async Task<ApiToken> CreateApiToken(string userName, string tokenName, string secretHash) { var user = RemotelyContext.Users.FirstOrDefault(x => x.UserName == userName); var newToken = new ApiToken() { Name = tokenName, OrganizationID = user.OrganizationID, Token = Guid.NewGuid().ToString(), Secret = secretHash }; RemotelyContext.ApiTokens.Add(newToken); await RemotelyContext.SaveChangesAsync(); return newToken; }
public bool ValidateApiToken(string apiToken, string apiSecret, string requestPath, string remoteIP) { var hasher = new PasswordHasher <RemotelyUser>(); var token = RemotelyContext.ApiTokens.FirstOrDefault(x => x.Token == apiToken); var isValid = token != null && hasher.VerifyHashedPassword(null, token.Secret, apiSecret) == PasswordVerificationResult.Success; if (token != null) { token.LastUsed = DateTimeOffset.Now; RemotelyContext.SaveChanges(); } WriteEvent($"API token used. Token: {apiToken}. Path: {requestPath}. Validated: {isValid}. Remote IP: {remoteIP}", EventType.Info, token?.OrganizationID); return(isValid); }
public async Task RemoveUserFromOrganization(string orgID, string targetUserID) { var target = RemotelyContext.Users .Include(x => x.DeviceGroups) .ThenInclude(x => x.Devices) .Include(x => x.Organization) .Include(x => x.Alerts) .FirstOrDefault(x => x.Id == targetUserID && x.OrganizationID == orgID); if (target is null) { return; } if (target.DeviceGroups?.Any() == true) { foreach (var deviceGroup in target.DeviceGroups.ToList()) { deviceGroup.Users.Remove(target); } } foreach (var alert in target.Alerts) { RemotelyContext.Alerts.Remove(alert); } target.OrganizationID = null; target.Organization = null; RemotelyContext .Organizations .Include(x => x.RemotelyUsers) .FirstOrDefault(x => x.ID == orgID) .RemotelyUsers.Remove(target); RemotelyContext.Users.Remove(target); await UserManager.DeleteAsync(target); await RemotelyContext.SaveChangesAsync(); }
public void WriteEvent(Exception ex, string organizationID) { try { RemotelyContext.EventLogs.Add(new EventLog() { EventType = EventType.Error, Message = ex.Message, Source = ex.Source, StackTrace = ex.StackTrace, TimeStamp = DateTimeOffset.Now, OrganizationID = organizationID }); RemotelyContext.SaveChanges(); } catch { } }
public async Task<Device> UpdateDevice(DeviceSetupOptions deviceOptions, string organizationId) { var device = RemotelyContext.Devices.Find(deviceOptions.DeviceID); if (device == null || device.OrganizationID != organizationId) { return null; } var group = await RemotelyContext.DeviceGroups.FirstOrDefaultAsync(x => x.Name.ToLower() == deviceOptions.DeviceGroupName.ToLower() && x.OrganizationID == device.OrganizationID); device.DeviceGroup = group; device.Alias = deviceOptions.DeviceAlias; await RemotelyContext.SaveChangesAsync(); return device; }
internal void RemovePermissionFromUser(string requesterUserName, string targetUserID, string permissionID) { var requester = RemotelyContext.Users .Include(x => x.Organization) .ThenInclude(x => x.RemotelyUsers) .ThenInclude(x => x.UserPermissionLinks) .FirstOrDefault(x => x.UserName == requesterUserName); var target = requester.Organization.RemotelyUsers.FirstOrDefault(x => x.Id == targetUserID); foreach (var permission in target.UserPermissionLinks.ToList().Where(x => x.PermissionGroupID == permissionID)) { target.UserPermissionLinks.Remove(permission); } RemotelyContext.Entry(target).State = EntityState.Modified; RemotelyContext.SaveChanges(); }
public bool AddUserToDeviceGroup(string orgID, string groupID, string userName, out string resultMessage) { resultMessage = string.Empty; var deviceGroup = RemotelyContext.DeviceGroups .Include(x => x.Users) .FirstOrDefault(x => x.ID == groupID && x.OrganizationID == orgID); if (deviceGroup == null) { resultMessage = "Device group not found."; return(false); } userName = userName.Trim().ToLower(); var user = RemotelyContext.Users .Include(x => x.DeviceGroups) .FirstOrDefault(x => x.UserName.ToLower() == userName && x.OrganizationID == orgID); if (user == null) { resultMessage = "User not found."; return(false); } deviceGroup.Devices ??= new List <Device>(); user.DeviceGroups ??= new List <DeviceGroup>(); if (deviceGroup.Users.Any(x => x.Id == user.Id)) { resultMessage = "User already in group."; return(false); } deviceGroup.Users.Add(user); user.DeviceGroups.Add(deviceGroup); RemotelyContext.SaveChanges(); resultMessage = user.Id; return(true); }
public void CleanupEmptyOrganizations() { var emptyOrgs = RemotelyContext.Organizations .Include(x => x.RemotelyUsers) .Include(x => x.CommandContexts) .Include(x => x.InviteLinks) .Include(x => x.Devices) .Include(x => x.SharedFiles) .Include(x => x.PermissionGroups) .Include(x => x.EventLogs) .Where(x => x.RemotelyUsers.Count == 0); foreach (var emptyOrg in emptyOrgs) { RemotelyContext.Remove(emptyOrg); } RemotelyContext.SaveChanges(); }
public async Task<Device> CreateDevice(DeviceSetupOptions options) { try { if (options is null || string.IsNullOrWhiteSpace(options.DeviceID) || string.IsNullOrWhiteSpace(options.OrganizationID) || RemotelyContext.Devices.Any(x => x.ID == options.DeviceID)) { return null; } var device = new Device() { ID = options.DeviceID, OrganizationID = options.OrganizationID }; if (!string.IsNullOrWhiteSpace(options.DeviceAlias)) { device.Alias = options.DeviceAlias; } if (!string.IsNullOrWhiteSpace(options.DeviceGroupName)) { var group = RemotelyContext.DeviceGroups.FirstOrDefault(x => x.Name.ToLower() == options.DeviceGroupName.ToLower() && x.OrganizationID == device.OrganizationID); device.DeviceGroup = group; } RemotelyContext.Devices.Add(device); await RemotelyContext.SaveChangesAsync(); return device; } catch (Exception ex) { WriteEvent(ex, options.OrganizationID); return null; } }
internal void CleanupOldRecords() { if (AppConfig.DataRetentionInDays > 0) { RemotelyContext.EventLogs .Where(x => DateTime.Now - x.TimeStamp > TimeSpan.FromDays(AppConfig.DataRetentionInDays)) .ForEachAsync(x => { RemotelyContext.Remove(x); }).Wait(); RemotelyContext.CommandContexts .Where(x => DateTime.Now - x.TimeStamp > TimeSpan.FromDays(AppConfig.DataRetentionInDays)) .ForEachAsync(x => { RemotelyContext.Remove(x); }).Wait(); } }
public async Task RemoveUserFromOrganization(string orgID, string targetUserID) { var target = RemotelyContext.Users .Include(x => x.PermissionLinks) .ThenInclude(x => x.DeviceGroup) .Include(x => x.Organization) .Include(x => x.Alerts) .FirstOrDefault(x => x.Id == targetUserID && x.OrganizationID == orgID); if (target?.PermissionLinks?.Any() == true) { foreach (var link in target.PermissionLinks.ToList()) { link.DeviceGroup = null; link.User = null; RemotelyContext.PermissionLinks.Remove(link); } } foreach (var alert in target.Alerts) { RemotelyContext.Alerts.Remove(alert); } target.OrganizationID = null; target.Organization = null; RemotelyContext .Organizations .Include(x => x.RemotelyUsers) .FirstOrDefault(x => x.ID == orgID) .RemotelyUsers.Remove(target); RemotelyContext.Users.Remove(target); await UserManager.DeleteAsync(target); await RemotelyContext.SaveChangesAsync(); }
public async Task ClearLogs(string currentUserName) { try { var currentUser = await RemotelyContext.Users.FirstOrDefaultAsync(x => x.UserName == currentUserName); var eventLogs = RemotelyContext.EventLogs.Where(x => x.OrganizationID == currentUser.OrganizationID); if (currentUser.IsServerAdmin) { eventLogs = eventLogs.Concat(RemotelyContext.EventLogs.Where(x => string.IsNullOrWhiteSpace(x.OrganizationID))); } RemotelyContext.EventLogs.RemoveRange(eventLogs); var commandResults = RemotelyContext.CommandResults.Where(x => x.OrganizationID == currentUser.OrganizationID); RemotelyContext.CommandResults.RemoveRange(commandResults); await RemotelyContext.SaveChangesAsync(); } catch { } }
public InviteLink AddInvite(string orgID, Invite invite) { invite.InvitedUser = invite.InvitedUser.ToLower(); var organization = RemotelyContext.Organizations .Include(x => x.InviteLinks) .FirstOrDefault(x => x.ID == orgID); var newInvite = new InviteLink() { DateSent = DateTimeOffset.Now, InvitedUser = invite.InvitedUser, IsAdmin = invite.IsAdmin, Organization = organization, OrganizationID = organization.ID }; organization.InviteLinks.Add(newInvite); RemotelyContext.SaveChanges(); return newInvite; }
public async Task<bool> RemoveUserFromDeviceGroup(string orgID, string groupID, string userID) { var deviceGroup = RemotelyContext.DeviceGroups .Include(x => x.Users) .ThenInclude(x => x.DeviceGroups) .FirstOrDefault(x => x.ID == groupID && x.OrganizationID == orgID); if (deviceGroup?.Users?.Any(x => x.Id == userID) == true) { var user = deviceGroup.Users.FirstOrDefault(x => x.Id == userID); user.DeviceGroups.Remove(deviceGroup); deviceGroup.Users.Remove(user); await RemotelyContext.SaveChangesAsync(); return true; } return false; }
internal Tuple <bool, string> AddPermission(string userName, Permission permission) { var organization = RemotelyContext.Users .Include(x => x.Organization) .ThenInclude(x => x.PermissionGroups) .FirstOrDefault(x => x.UserName == userName) .Organization; if (organization.PermissionGroups.Any(x => x.Name.ToLower() == permission.Name.ToLower())) { return(Tuple.Create(false, "Permission group already exists.")); } var newPermission = new PermissionGroup() { Name = permission.Name, Organization = organization }; organization.PermissionGroups.Add(newPermission); RemotelyContext.SaveChanges(); return(Tuple.Create(true, newPermission.ID)); }
public void SetDeviceSetupOptions(string deviceID, DeviceSetupOptions options) { var device = RemotelyContext.Devices.FirstOrDefault(x => x.ID == deviceID); if (device != null) { if (!string.IsNullOrWhiteSpace(options.DeviceAlias)) { device.Alias = options.DeviceAlias; } if (!string.IsNullOrWhiteSpace(options.DeviceGroup)) { var group = RemotelyContext.DeviceGroups.FirstOrDefault(x => x.Name.ToLower() == options.DeviceGroup.ToLower() && x.OrganizationID == device.OrganizationID); device.DeviceGroup = group; } RemotelyContext.SaveChanges(); } }
internal InviteLink AddInvite(string requesterUserName, Invite invite, string requestOrigin) { invite.InvitedUser = invite.InvitedUser.ToLower(); var requester = RemotelyContext.Users .Include(x => x.Organization) .ThenInclude(x => x.InviteLinks) .Include(x => x.Organization) .ThenInclude(x => x.RemotelyUsers) .FirstOrDefault(x => x.UserName == requesterUserName); var newInvite = new InviteLink() { DateSent = DateTime.Now, InvitedUser = invite.InvitedUser, IsAdmin = invite.IsAdmin, Organization = requester.Organization }; requester.Organization.InviteLinks.Add(newInvite); RemotelyContext.SaveChanges(); return(newInvite); }
public async Task<bool> TempPasswordSignIn(string email, string password) { if (string.IsNullOrWhiteSpace(password)) { return false; } var user = GetUserByName(email); if (user is null) { return false; } if (user.TempPassword == password) { user.TempPassword = string.Empty; await RemotelyContext.SaveChangesAsync(); return true; } return false; }
public async Task<string> AddSharedFile(IFormFile file, string organizationID) { var expirationDate = DateTimeOffset.Now.AddDays(-AppConfig.DataRetentionInDays); var expiredFiles = RemotelyContext.SharedFiles.Where(x => x.Timestamp < expirationDate); RemotelyContext.RemoveRange(expiredFiles); byte[] fileContents; using (var stream = file.OpenReadStream()) { using var ms = new MemoryStream(); await stream.CopyToAsync(ms); fileContents = ms.ToArray(); } var newEntity = RemotelyContext.Add(new SharedFile() { FileContents = fileContents, FileName = file.FileName, ContentType = file.ContentType, OrganizationID = organizationID }); await RemotelyContext.SaveChangesAsync(); return newEntity.Entity.ID; }
public async Task UpdateServerAdmins(List <string> serverAdmins, string callerUserName) { var currentAdmins = RemotelyContext.Users.Where(x => x.IsServerAdmin).ToList(); var removeAdmins = currentAdmins.Where(currentAdmin => !serverAdmins.Contains(currentAdmin.UserName.Trim().ToLower()) && currentAdmin.UserName.Trim().ToLower() != callerUserName.Trim().ToLower()); foreach (var removeAdmin in removeAdmins) { removeAdmin.IsServerAdmin = false; } var newAdmins = RemotelyContext.Users.Where(user => serverAdmins.Contains(user.UserName.Trim().ToLower()) && !user.IsServerAdmin); foreach (var newAdmin in newAdmins) { newAdmin.IsServerAdmin = true; } await RemotelyContext.SaveChangesAsync(); }
public void CleanupOldRecords() { if (AppConfig.DataRetentionInDays > 0) { var expirationDate = DateTimeOffset.Now - TimeSpan.FromDays(AppConfig.DataRetentionInDays); var eventLogs = RemotelyContext.EventLogs .Where(x => x.TimeStamp < expirationDate); RemotelyContext.RemoveRange(eventLogs); var commandResults = RemotelyContext.CommandResults .Where(x => x.TimeStamp < expirationDate); RemotelyContext.RemoveRange(commandResults); var sharedFiles = RemotelyContext.SharedFiles .Where(x => x.Timestamp < expirationDate); RemotelyContext.RemoveRange(sharedFiles); RemotelyContext.SaveChanges(); } }
public void DeleteDeviceGroup(string orgID, string deviceGroupID) { var deviceGroup = RemotelyContext.DeviceGroups .Include(x => x.Devices) .Include(x => x.PermissionLinks) .ThenInclude(x => x.User) .FirstOrDefault(x => x.ID == deviceGroupID && x.OrganizationID == orgID); deviceGroup.Devices?.ForEach(x => { x.DeviceGroup = null; }); deviceGroup.PermissionLinks?.ToList()?.ForEach(x => { x.User = null; x.DeviceGroup = null; RemotelyContext.PermissionLinks.Remove(x); }); RemotelyContext.DeviceGroups.Remove(deviceGroup); RemotelyContext.SaveChanges(); }
public async Task <bool> RemoveUserFromDeviceGroup(string orgID, string groupID, string userID) { var deviceGroup = RemotelyContext.DeviceGroups .Include(x => x.PermissionLinks) .ThenInclude(x => x.User) .FirstOrDefault(x => x.ID == groupID && x.OrganizationID == orgID); if (deviceGroup?.PermissionLinks?.Any(x => x.UserID == userID) == true) { var link = deviceGroup.PermissionLinks.FirstOrDefault(x => x.UserID == userID); link.User = null; link.DeviceGroup = null; RemotelyContext.PermissionLinks.Remove(link); await RemotelyContext.SaveChangesAsync(); return(true); } return(false); }
public void DetachEntity(object entity) { RemotelyContext.Entry(entity).State = EntityState.Detached; }