Esempio n. 1
0
 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 { }
 }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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;
        }
Esempio n. 6
0
        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));
        }
Esempio n. 7
0
        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();
            }
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        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();
            }
        }
Esempio n. 10
0
        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();
        }
Esempio n. 11
0
 public void UpdateUserOptions(string userName, RemotelyUserOptions options)
 {
     RemotelyContext.Users.FirstOrDefault(x => x.UserName == userName).UserOptions = options;
     RemotelyContext.SaveChanges();
 }
Esempio n. 12
0
 internal void UpdateTags(string deviceID, string tag)
 {
     RemotelyContext.Devices.Find(deviceID).Tags = tag;
     RemotelyContext.SaveChanges();
 }
Esempio n. 13
0
 public void WriteEvent(EventLog eventLog)
 {
     RemotelyContext.EventLogs.Add(eventLog);
     RemotelyContext.SaveChanges();
 }
Esempio n. 14
0
 public void SetAllDevicesNotOnline()
 {
     RemotelyContext.Devices.ForEachAsync(x => { x.IsOnline = false; }).Wait();
     RemotelyContext.SaveChanges();
 }