Restructured projects and created Services for permissions and users
This commit is contained in:
@@ -6,11 +6,7 @@ namespace HopFrame.Database;
|
||||
/// <summary>
|
||||
/// This class includes the basic database structure in order for HopFrame to work
|
||||
/// </summary>
|
||||
public class HopDbContextBase : DbContext {
|
||||
|
||||
public HopDbContextBase() {}
|
||||
|
||||
public HopDbContextBase(DbContextOptions options) : base(options) {}
|
||||
public abstract class HopDbContextBase : DbContext {
|
||||
|
||||
public virtual DbSet<UserEntry> Users { get; set; }
|
||||
public virtual DbSet<PermissionEntry> Permissions { get; set; }
|
||||
@@ -25,4 +21,12 @@ public class HopDbContextBase : DbContext {
|
||||
modelBuilder.Entity<TokenEntry>();
|
||||
modelBuilder.Entity<GroupEntry>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets executed when a user is deleted through the IUserService from the
|
||||
/// HopFrame.Security package. You can override this method to also delete
|
||||
/// related user specific entries in the database
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
public virtual void OnUserDelete(UserEntry user) {}
|
||||
}
|
||||
@@ -20,10 +20,37 @@ public static class ModelExtensions {
|
||||
|
||||
user.Permissions = contextBase.Permissions
|
||||
.Where(perm => perm.UserId == entry.Id)
|
||||
.Select(perm => perm.PermissionText)
|
||||
.Select(perm => perm.ToPermissionModel())
|
||||
.ToList();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public static Permission ToPermissionModel(this PermissionEntry entry) {
|
||||
Guid.TryParse(entry.UserId, out var userId);
|
||||
|
||||
return new Permission {
|
||||
Owner = userId,
|
||||
PermissionName = entry.PermissionText,
|
||||
GrantedAt = entry.GrantedAt,
|
||||
Id = entry.RecordId
|
||||
};
|
||||
}
|
||||
|
||||
public static PermissionGroup ToPermissionGroup(this GroupEntry entry, HopDbContextBase contextBase) {
|
||||
var group = new PermissionGroup {
|
||||
Name = entry.Name,
|
||||
IsDefaultGroup = entry.Default,
|
||||
Description = entry.Description,
|
||||
CreatedAt = entry.CreatedAt
|
||||
};
|
||||
|
||||
group.Permissions = contextBase.Permissions
|
||||
.Where(perm => perm.UserId == group.Name)
|
||||
.Select(perm => perm.ToPermissionModel())
|
||||
.ToList();
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
}
|
||||
10
HopFrame.Database/Models/Permission.cs
Normal file
10
HopFrame.Database/Models/Permission.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace HopFrame.Database.Models;
|
||||
|
||||
public sealed class Permission {
|
||||
public long Id { get; init; }
|
||||
public string PermissionName { get; set; }
|
||||
public Guid Owner { get; set; }
|
||||
public DateTime GrantedAt { get; set; }
|
||||
}
|
||||
|
||||
public interface IPermissionOwner {}
|
||||
9
HopFrame.Database/Models/PermissionGroup.cs
Normal file
9
HopFrame.Database/Models/PermissionGroup.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace HopFrame.Database.Models;
|
||||
|
||||
public sealed class PermissionGroup : IPermissionOwner {
|
||||
public string Name { get; init; }
|
||||
public bool IsDefaultGroup { get; set; }
|
||||
public string Description { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public IList<Permission> Permissions { get; set; }
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
namespace HopFrame.Database.Models;
|
||||
|
||||
public class User {
|
||||
public Guid Id { get; set; }
|
||||
public sealed class User : IPermissionOwner {
|
||||
public Guid Id { get; init; }
|
||||
public string Username { get; set; }
|
||||
public string Email { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public IList<string> Permissions { get; set; }
|
||||
public IList<Permission> Permissions { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user