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 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 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 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; }
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 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 void UpdateUserOptions(string userName, RemotelyUserOptions options) { RemotelyContext.Users.FirstOrDefault(x => x.UserName == userName).UserOptions = options; RemotelyContext.SaveChanges(); }
internal void UpdateTags(string deviceID, string tag) { RemotelyContext.Devices.Find(deviceID).Tags = tag; RemotelyContext.SaveChanges(); }
public void WriteEvent(EventLog eventLog) { RemotelyContext.EventLogs.Add(eventLog); RemotelyContext.SaveChanges(); }
public void SetAllDevicesNotOnline() { RemotelyContext.Devices.ForEachAsync(x => { x.IsOnline = false; }).Wait(); RemotelyContext.SaveChanges(); }