Skip to content

Commit

Permalink
Fix device filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Dec 4, 2020
1 parent b9a5cbd commit 98b1d87
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public bool DoesUserHaveAccessToDevice(string deviceID, RemotelyUser remotelyUse
device.ID == deviceID &&
(
remotelyUser.IsAdministrator ||
device.DeviceGroup.PermissionLinks.Count == 0 ||
string.IsNullOrWhiteSpace(device.DeviceGroupID) ||
device.DeviceGroup.PermissionLinks.Any(permission => permission.UserID == remotelyUser.Id
)));
}
Expand Down Expand Up @@ -616,18 +616,24 @@ public string[] FilterUsersByDevicePermission(IEnumerable<string> userIDs, strin
.ThenInclude(x => x.PermissionLinks)
.FirstOrDefault(x => x.ID == deviceID);

var orgUsers = RemotelyContext.Users
.Where(user =>
user.OrganizationID == device.OrganizationID &&
userIDs.Contains(user.Id));

if (string.IsNullOrWhiteSpace(device.DeviceGroupID))
{
return orgUsers
.Select(x => x.Id)
.ToArray();
}

var allowedUsers = device?.DeviceGroup?.PermissionLinks?.Select(x => x.UserID) ?? Array.Empty<string>();

return RemotelyContext.Users
.Include(x => x.PermissionLinks)
return orgUsers
.Where(user =>
user.OrganizationID == device.OrganizationID &&
userIDs.Contains(user.Id) &&
(
user.IsAdministrator ||
allowedUsers.Any() ||
allowedUsers.Contains(user.Id)
)
user.IsAdministrator ||
allowedUsers.Contains(user.Id)
)
.Select(x => x.Id)
.ToArray();
Expand Down Expand Up @@ -748,6 +754,7 @@ public IEnumerable<DeviceGroup> GetDeviceGroups(string username)
{
return null;
}
var userId = user.Id;

return RemotelyContext.DeviceGroups
.Include(x => x.PermissionLinks)
Expand All @@ -757,7 +764,7 @@ public IEnumerable<DeviceGroup> GetDeviceGroups(string username)
(
user.IsAdministrator ||
x.PermissionLinks.Count == 0 ||
x.PermissionLinks.Any(x => x.UserID == user.Id)
x.PermissionLinks.Any(x => x.UserID == userId)
)
)
.OrderBy(x => x.Name) ?? Enumerable.Empty<DeviceGroup>();
Expand All @@ -775,9 +782,9 @@ public IEnumerable<Device> GetDevicesForUser(string userName)
x.OrganizationID == user.OrganizationID &&
(
user.IsAdministrator ||
x.DeviceGroup.PermissionLinks.Count == 0 ||
x.DeviceGroup.PermissionLinks.Any(permission => permission.UserID == userID
)));
string.IsNullOrWhiteSpace(x.DeviceGroupID)||
x.DeviceGroup.PermissionLinks.Any(permission => permission.UserID == userID)
));
}

public IEnumerable<EventLog> GetEventLogs(string userName, DateTimeOffset from, DateTimeOffset to, EventType? type, string message)
Expand Down

0 comments on commit 98b1d87

Please sign in to comment.