Skip to content

Commit

Permalink
Refactor entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Dec 4, 2020
1 parent 98b1d87 commit d614507
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@
If no users are added, everyone will have access.
</div>
<select multiple class="form-control device-group-user-list mb-1">
@if (Model.DeviceGroups[i]?.PermissionLinks?.Any() == true)
@if (Model.DeviceGroups[i]?.Users?.Any() == true)
{
foreach (var link in Model.DeviceGroups[i].PermissionLinks)
foreach (var user in Model.DeviceGroups[i].Users)
{
<option value="@link.UserID">@link.User.UserName</option>
<option value="@user.Id">@user.UserName</option>
}
}
</select>
Expand Down
3 changes: 2 additions & 1 deletion Server/Areas/Identity/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
Email = Input.Email,
IsServerAdmin = organizationCount == 0,
Organization = new Organization(),
UserOptions = new RemotelyUserOptions()
UserOptions = new RemotelyUserOptions(),
IsAdministrator = true
};
var result = await _userManager.CreateAsync(user, Input.Password);

Expand Down
14 changes: 2 additions & 12 deletions Server/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public ApplicationDbContext(DbContextOptions context)

public DbSet<DeviceGroup> DeviceGroups { get; set; }

public DbSet<UserDevicePermission> PermissionLinks { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{

Expand All @@ -62,8 +60,6 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<DeviceGroup>()
.HasMany(x => x.Devices)
.WithOne(x => x.DeviceGroup);
builder.Entity<DeviceGroup>()
.HasMany(x => x.PermissionLinks);
builder.Entity<Organization>()
.HasMany(x => x.DeviceGroups)
.WithOne(x => x.Organization);
Expand Down Expand Up @@ -109,18 +105,13 @@ protected override void OnModelCreating(ModelBuilder builder)
.Property(x => x.CommandResults)
.Metadata.SetValueComparer(new ValueComparer<ICollection<GenericCommandResult>>(true));

//builder.Entity<GenericCommandResult>()
// .HasNoKey();

//builder.Entity<PSCoreCommandResult>()
// .HasNoKey();

builder.Entity<RemotelyUser>()
.HasOne(x => x.Organization)
.WithMany(x => x.RemotelyUsers);

builder.Entity<RemotelyUser>()
.HasMany(x => x.PermissionLinks);
.HasMany(x => x.DeviceGroups)
.WithMany(x => x.Users);
builder.Entity<RemotelyUser>()
.HasMany(x => x.Alerts)
.WithOne(x => x.User);
Expand All @@ -142,7 +133,6 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<Device>()
.Property(x => x.Drives)
.Metadata.SetValueComparer(new ValueComparer<List<Drive>>(true));

builder.Entity<Device>()
.HasIndex(x => x.DeviceName);
builder.Entity<Device>()
Expand Down
95 changes: 41 additions & 54 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public bool AddUserToDeviceGroup(string orgID, string groupID, string userName,
resultMessage = string.Empty;

var deviceGroup = RemotelyContext.DeviceGroups
.Include(x => x.PermissionLinks)
.Include(x => x.Users)
.FirstOrDefault(x =>
x.ID == groupID &&
x.OrganizationID == orgID);
Expand All @@ -323,7 +323,7 @@ public bool AddUserToDeviceGroup(string orgID, string groupID, string userName,
userName = userName.Trim().ToLower();

var user = RemotelyContext.Users
.Include(x => x.PermissionLinks)
.Include(x => x.DeviceGroups)
.FirstOrDefault(x =>
x.UserName.ToLower() == userName &&
x.OrganizationID == orgID);
Expand All @@ -334,25 +334,17 @@ public bool AddUserToDeviceGroup(string orgID, string groupID, string userName,
return false;
}

deviceGroup.PermissionLinks ??= new List<UserDevicePermission>();
user.PermissionLinks ??= new List<UserDevicePermission>();
deviceGroup.Devices ??= new List<Device>();
user.DeviceGroups ??= new List<DeviceGroup>();

if (deviceGroup.PermissionLinks.Any(x => x.UserID == user.Id))
if (deviceGroup.Users.Any(x => x.Id == user.Id))
{
resultMessage = "User already in group.";
return false;
}

var link = new UserDevicePermission()
{
DeviceGroup = deviceGroup,
DeviceGroupID = deviceGroup.ID,
User = user,
UserID = user.Id
};

deviceGroup.PermissionLinks.Add(link);
user.PermissionLinks.Add(link);
deviceGroup.Users.Add(user);
user.DeviceGroups.Add(deviceGroup);
RemotelyContext.SaveChanges();
resultMessage = user.Id;
return true;
Expand Down Expand Up @@ -506,25 +498,25 @@ 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);
.Include(x => x.Users)
.ThenInclude(x => x.DeviceGroups)
.FirstOrDefault(x =>
x.ID == deviceGroupID &&
x.OrganizationID == orgID);

deviceGroup.Devices?.ForEach(x =>
{
x.DeviceGroup = null;
});

deviceGroup.PermissionLinks?.ToList()?.ForEach(x =>
deviceGroup.Users?.ForEach(x =>
{
x.User = null;
x.DeviceGroup = null;

RemotelyContext.PermissionLinks.Remove(x);
x.DeviceGroups.Remove(deviceGroup);
});

deviceGroup.Devices.Clear();
deviceGroup.Users.Clear();

RemotelyContext.DeviceGroups.Remove(deviceGroup);

RemotelyContext.SaveChanges();
Expand Down Expand Up @@ -575,13 +567,13 @@ public bool DoesUserHaveAccessToDevice(string deviceID, RemotelyUser remotelyUse
{
return RemotelyContext.Devices
.Include(x => x.DeviceGroup)
.ThenInclude(x => x.PermissionLinks)
.ThenInclude(x => x.Users)
.Any(device => device.OrganizationID == remotelyUser.OrganizationID &&
device.ID == deviceID &&
(
remotelyUser.IsAdministrator ||
string.IsNullOrWhiteSpace(device.DeviceGroupID) ||
device.DeviceGroup.PermissionLinks.Any(permission => permission.UserID == remotelyUser.Id
device.DeviceGroup.Users.Any(user => user.Id == remotelyUser.Id
)));
}

Expand All @@ -596,14 +588,14 @@ public string[] FilterDeviceIDsByUserPermission(string[] deviceIDs, RemotelyUser
{
return RemotelyContext.Devices
.Include(x => x.DeviceGroup)
.ThenInclude(x => x.PermissionLinks)
.ThenInclude(x => x.Users)
.Where(device =>
device.OrganizationID == remotelyUser.OrganizationID &&
deviceIDs.Contains(device.ID) &&
(
remotelyUser.IsAdministrator ||
device.DeviceGroup.PermissionLinks.Count == 0 ||
device.DeviceGroup.PermissionLinks.Any(permission => permission.UserID == remotelyUser.Id
device.DeviceGroup.Users.Count == 0 ||
device.DeviceGroup.Users.Any(user => user.Id == remotelyUser.Id
)))
.Select(x => x.ID)
.ToArray();
Expand All @@ -613,7 +605,7 @@ public string[] FilterUsersByDevicePermission(IEnumerable<string> userIDs, strin
{
var device = RemotelyContext.Devices
.Include(x => x.DeviceGroup)
.ThenInclude(x => x.PermissionLinks)
.ThenInclude(x => x.Users)
.FirstOrDefault(x => x.ID == deviceID);

var orgUsers = RemotelyContext.Users
Expand All @@ -628,7 +620,7 @@ public string[] FilterUsersByDevicePermission(IEnumerable<string> userIDs, strin
.ToArray();
}

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

return orgUsers
.Where(user =>
Expand Down Expand Up @@ -757,14 +749,14 @@ public IEnumerable<DeviceGroup> GetDeviceGroups(string username)
var userId = user.Id;

return RemotelyContext.DeviceGroups
.Include(x => x.PermissionLinks)
.ThenInclude(x => x.User)
.Include(x => x.Users)
.ThenInclude(x => x.DeviceGroups)
.Where(x =>
x.OrganizationID == user.OrganizationID &&
(
user.IsAdministrator ||
x.PermissionLinks.Count == 0 ||
x.PermissionLinks.Any(x => x.UserID == userId)
x.Users.Count == 0 ||
x.Users.Any(x => x.Id == userId)
)
)
.OrderBy(x => x.Name) ?? Enumerable.Empty<DeviceGroup>();
Expand All @@ -777,13 +769,13 @@ public IEnumerable<Device> GetDevicesForUser(string userName)

return RemotelyContext.Devices
.Include(x => x.DeviceGroup)
.ThenInclude(x => x.PermissionLinks)
.ThenInclude(x => x.Users)
.Where(x =>
x.OrganizationID == user.OrganizationID &&
(
user.IsAdministrator ||
string.IsNullOrWhiteSpace(x.DeviceGroupID)||
x.DeviceGroup.PermissionLinks.Any(permission => permission.UserID == userID)
x.DeviceGroup.Users.Any(user => user.Id == userID)
));
}

Expand Down Expand Up @@ -923,20 +915,18 @@ public void RemoveDevices(string[] deviceIDs)
public async Task<bool> RemoveUserFromDeviceGroup(string orgID, string groupID, string userID)
{
var deviceGroup = RemotelyContext.DeviceGroups
.Include(x => x.PermissionLinks)
.ThenInclude(x => x.User)
.Include(x => x.Users)
.ThenInclude(x => x.DeviceGroups)
.FirstOrDefault(x =>
x.ID == groupID &&
x.OrganizationID == orgID);

if (deviceGroup?.PermissionLinks?.Any(x => x.UserID == userID) == true)
if (deviceGroup?.Users?.Any(x => x.Id == userID) == true)
{
var link = deviceGroup.PermissionLinks.FirstOrDefault(x => x.UserID == userID);
var user = deviceGroup.Users.FirstOrDefault(x => x.Id == userID);

link.User = null;
link.DeviceGroup = null;

RemotelyContext.PermissionLinks.Remove(link);
user.DeviceGroups.Remove(deviceGroup);
deviceGroup.Users.Remove(user);

await RemotelyContext.SaveChangesAsync();
return true;
Expand All @@ -947,22 +937,19 @@ public async Task<bool> RemoveUserFromDeviceGroup(string orgID, string groupID,
public async Task RemoveUserFromOrganization(string orgID, string targetUserID)
{
var target = RemotelyContext.Users
.Include(x => x.PermissionLinks)
.ThenInclude(x => x.DeviceGroup)
.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?.PermissionLinks?.Any() == true)
if (target?.DeviceGroups?.Any() == true)
{
foreach (var link in target.PermissionLinks.ToList())
foreach (var deviceGroup in target.DeviceGroups.ToList())
{
link.DeviceGroup = null;
link.User = null;

RemotelyContext.PermissionLinks.Remove(link);
deviceGroup.Users.Remove(target);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Shared/Models/DeviceGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public class DeviceGroup

public string OrganizationID { get; set; }

public List<UserDevicePermission> PermissionLinks { get; set; }
public List<RemotelyUser> Users { get; set; }
}
}
4 changes: 2 additions & 2 deletions Shared/Models/RemotelyUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public class RemotelyUser : IdentityUser
[StringLength(100)]
public string DisplayName { get; set; }

public bool IsAdministrator { get; set; } = true;
public bool IsAdministrator { get; set; }
public bool IsServerAdmin { get; set; }

[JsonIgnore]
public Organization Organization { get; set; }
public string OrganizationID { get; set; }

public List<UserDevicePermission> PermissionLinks { get; set; }
public List<DeviceGroup> DeviceGroups { get; set; }

public string TempPassword { get; set; }

Expand Down
22 changes: 0 additions & 22 deletions Shared/Models/UserDevicePermission.cs

This file was deleted.

0 comments on commit d614507

Please sign in to comment.