Esempio n. 1
0
        internal void RemovePermissionFromDevices(string userID, string[] deviceIDs, string groupName)
        {
            var user = RemotelyContext.Users
                       .Include(x => x.Organization)
                       .ThenInclude(x => x.Devices)
                       .FirstOrDefault(x => x.Id == userID);

            var group = user.Organization.PermissionGroups.FirstOrDefault(x => x.Name.ToLower() == groupName.ToLower());

            foreach (var deviceID in deviceIDs)
            {
                if (user.Organization.Devices.Any(x => x.ID == deviceID))
                {
                    var device = RemotelyContext.Devices
                                 .Include(x => x.DevicePermissionLinks)
                                 .FirstOrDefault(x => x.ID == deviceID);
                    foreach (var permission in device.DevicePermissionLinks.ToList().Where(x => x.PermissionGroupID == group.ID))
                    {
                        device.DevicePermissionLinks.Remove(permission);
                    }
                    RemotelyContext.Entry(device).State = EntityState.Modified;
                }
            }
            RemotelyContext.SaveChanges();
        }
Esempio n. 2
0
        public bool AddOrUpdateDevice(Device device)
        {
            var existingDevice = RemotelyContext.Devices.Find(device.ID);

            if (existingDevice != null)
            {
                device.ServerVerificationToken = existingDevice.ServerVerificationToken;
                RemotelyContext.Entry(existingDevice).CurrentValues.SetValues(device);
            }
            else
            {
                if (!RemotelyContext.Organizations.Any(x => x.ID == device.OrganizationID))
                {
                    WriteEvent(new EventLog()
                    {
                        EventType = EventTypes.Info,
                        Message   = $"Unable to add device {device.DeviceName} because organization {device.OrganizationID} does not exist.",
                        Source    = "DataService.AddOrUpdateDevice"
                    });
                    return(false);
                }
                RemotelyContext.Devices.Add(device);
            }
            RemotelyContext.SaveChanges();
            return(true);
        }
Esempio n. 3
0
        internal void AddPermissionToDevices(string userID, string[] deviceIDs, string groupName)
        {
            var user = RemotelyContext.Users
                       .Include(x => x.Organization)
                       .Include(x => x.Organization)
                       .ThenInclude(x => x.Devices)
                       .FirstOrDefault(x => x.Id == userID);

            var group = user.Organization.PermissionGroups.FirstOrDefault(x => x.Name.ToLower() == groupName.ToLower());

            foreach (var deviceID in deviceIDs)
            {
                if (user.Organization.Devices.Any(x => x.ID == deviceID))
                {
                    var device = RemotelyContext.Devices
                                 .Include(x => x.DevicePermissionLinks)
                                 .FirstOrDefault(x => x.ID == deviceID);
                    if (!device.DevicePermissionLinks.Any(x => x.PermissionGroupID == group.ID))
                    {
                        device.DevicePermissionLinks.Add(new DevicePermissionLink()
                        {
                            Device            = device,
                            DeviceID          = device.ID,
                            PermissionGroup   = group,
                            PermissionGroupID = group.ID
                        });
                        RemotelyContext.Entry(device).State = EntityState.Modified;
                    }
                }
            }
            RemotelyContext.SaveChanges();
        }
Esempio n. 4
0
 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();
 }
Esempio n. 5
0
        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();
        }
Esempio n. 6
0
 public void DetachEntity(object entity)
 {
     RemotelyContext.Entry(entity).State = EntityState.Detached;
 }