[CHANGE] Adding functionality to manager
This commit is contained in:
@@ -11,54 +11,47 @@ public class AuthorityContext(DbContextOptions<AuthorityContext> options) : DbCo
|
||||
public DbSet<AuthorityRole> Roles { get; set; }
|
||||
public DbSet<AuthorityUser> Users { get; set; }
|
||||
|
||||
public DbSet<RoleGroup> RoleGroup { get; set; }
|
||||
public DbSet<RoleUser> RoleUser { get; set; }
|
||||
public DbSet<UserGroup> UserGroup { get; set; }
|
||||
public DbSet<RoleLink> RoleLinks { get; set; }
|
||||
public DbSet<UserGroups> UserGroups { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<AuthorityAttribute>(attributeEntity =>
|
||||
{
|
||||
attributeEntity.ToTable("authority_attributes");
|
||||
attributeEntity.HasKey(a => new { a.BoundId, a.AttributeKey });
|
||||
attributeEntity.HasKey(a => new { a.ForeignKey, a.AttributeKey });
|
||||
});
|
||||
|
||||
modelBuilder.Entity<AuthorityGroup>(groupEntity =>
|
||||
{
|
||||
groupEntity.ToTable("authority_groups");
|
||||
groupEntity.HasKey(x => x.Id);
|
||||
groupEntity.HasMany(g => g.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade);
|
||||
groupEntity.HasMany(g => g.Attributes).WithOne().HasForeignKey(a => a.ForeignKey).OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<AuthorityRole>(roleEntity =>
|
||||
{
|
||||
roleEntity.ToTable("authority_roles");
|
||||
roleEntity.HasKey(x => x.Id);
|
||||
roleEntity.HasMany(r => r.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade);
|
||||
roleEntity.HasMany(r => r.Attributes).WithOne().HasForeignKey(a => a.ForeignKey).OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<AuthorityUser>(userEntity =>
|
||||
{
|
||||
userEntity.ToTable("authority_users");
|
||||
userEntity.HasKey(x => x.Id);
|
||||
userEntity.HasMany(u => u.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade);
|
||||
userEntity.HasMany(u => u.Attributes).WithOne().HasForeignKey(a => a.ForeignKey).OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<RoleGroup>(rgEntity =>
|
||||
modelBuilder.Entity<RoleLink>(rgEntity =>
|
||||
{
|
||||
rgEntity.ToTable("role_group");
|
||||
rgEntity.HasKey(rg => new { rg.RoleId, rg.GroupId });
|
||||
rgEntity.ToTable("role_links");
|
||||
rgEntity.HasKey(rg => new { rg.RoleId, rg.LinkId });
|
||||
});
|
||||
|
||||
modelBuilder.Entity<RoleUser>(ruEntity =>
|
||||
modelBuilder.Entity<UserGroups>(ugEntity =>
|
||||
{
|
||||
ruEntity.ToTable("role_user");
|
||||
ruEntity.HasKey(ru => new { ru.RoleId, ru.UserId });
|
||||
});
|
||||
|
||||
modelBuilder.Entity<UserGroup>(ugEntity =>
|
||||
{
|
||||
ugEntity.ToTable("user_group");
|
||||
ugEntity.ToTable("user_groups");
|
||||
ugEntity.HasKey(ug => new { ug.UserId, ug.GroupId });
|
||||
});
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
namespace DotBased.AspNet.Authority.EFCore.Models;
|
||||
|
||||
public class RoleGroup
|
||||
public class RoleLink
|
||||
{
|
||||
public Guid RoleId { get; set; }
|
||||
public Guid GroupId { get; set; }
|
||||
public Guid LinkId { get; set; }
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
namespace DotBased.AspNet.Authority.EFCore.Models;
|
||||
|
||||
public class UserGroup
|
||||
public class UserGroups
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public Guid GroupId { get; set; }
|
@@ -1,6 +1,6 @@
|
||||
namespace DotBased.AspNet.Authority.EFCore.Models;
|
||||
|
||||
public class RoleUser
|
||||
public class UserRoles
|
||||
{
|
||||
public Guid RoleId { get; set; }
|
||||
public Guid UserId { get; set; }
|
@@ -15,13 +15,13 @@ public class AttributeRepository(IDbContextFactory<AuthorityContext> contextFact
|
||||
var query = context.Attributes.AsQueryable();
|
||||
if (!string.IsNullOrEmpty(search))
|
||||
{
|
||||
query = query.Where(a => $"{a.AttributeKey} {a.BoundId} {a.AttributeValue}".Contains(search, StringComparison.CurrentCultureIgnoreCase));
|
||||
query = query.Where(a => $"{a.AttributeKey} {a.ForeignKey} {a.AttributeValue}".Contains(search, StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
|
||||
var total = await query.CountAsync(cancellationToken);
|
||||
var select = await query.OrderBy(a => a.AttributeKey).Skip(offset).Take(limit).Select(a => new AuthorityAttributeItem()
|
||||
{
|
||||
BoundId = a.BoundId,
|
||||
BoundId = a.ForeignKey,
|
||||
AttributeKey = a.AttributeKey,
|
||||
AttributeValue = a.AttributeValue
|
||||
}).ToListAsync(cancellationToken);
|
||||
@@ -52,7 +52,7 @@ public class AttributeRepository(IDbContextFactory<AuthorityContext> contextFact
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
if (string.IsNullOrWhiteSpace(attribute.AttributeKey) || attribute.BoundId == Guid.Empty)
|
||||
if (string.IsNullOrWhiteSpace(attribute.AttributeKey) || attribute.ForeignKey == Guid.Empty)
|
||||
{
|
||||
return Result<AuthorityAttribute>.Failed("Attribute key and/or bound id is empty");
|
||||
}
|
||||
|
@@ -48,6 +48,21 @@ public class GroupRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<AuthorityGroup>> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var userJoinGroups = context.UserGroups.Where(ug => ug.UserId == user.Id).Select(ug => ug.GroupId);
|
||||
var userGroups = context.Groups.Where(g => userJoinGroups.Contains(g.Id));
|
||||
return ListResult<AuthorityGroup>.Ok(userGroups, userGroups.Count());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionListResult<AuthorityGroup>("Failed to get Groups", e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<AuthorityGroup>> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
|
@@ -1,9 +1,11 @@
|
||||
using DotBased.AspNet.Authority.EFCore.Models;
|
||||
using DotBased.AspNet.Authority.Models.Authority;
|
||||
using DotBased.AspNet.Authority.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
||||
|
||||
|
||||
public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory) : RepositoryBase, IRoleRepository
|
||||
{
|
||||
public async Task<ListResult<AuthorityRoleItem>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||
@@ -95,19 +97,18 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
||||
public async Task<Result> DeleteRolesAsync(List<AuthorityRole> roles, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var currentRole = await context.Roles.FirstOrDefaultAsync(r => r.Id == role.Id, cancellationToken: cancellationToken);
|
||||
if (currentRole == null)
|
||||
{
|
||||
return Result.Failed("Role not found, could not delete!");
|
||||
}
|
||||
context.Roles.Remove(currentRole);
|
||||
var roleIds = roles.Select(r => r.Id).ToList();
|
||||
|
||||
context.Roles.RemoveRange(roles);
|
||||
context.RoleLinks.RemoveRange(context.RoleLinks.Where(rg => roleIds.Contains(rg.RoleId)));
|
||||
|
||||
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||
return saveResult <= 0 ? Result.Failed("Failed to delete role!") : Result.Ok();
|
||||
return saveResult <= 0 ? Result.Failed("Failed to delete roles!") : Result.Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -117,6 +118,89 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
|
||||
public async Task<ListResult<AuthorityRoleItem>> GetUserRolesAsync(AuthorityUser user, int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||
{
|
||||
return ListResult<AuthorityRoleItem>.Failed("Not implemented!");
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var roleIds = await context.RoleLinks.Where(r => r.LinkId == user.Id).Select(i => i.RoleId).ToListAsync(cancellationToken: cancellationToken);
|
||||
var rolesQuery = context.Roles.Where(r => roleIds.Contains(r.Id));
|
||||
if (!string.IsNullOrEmpty(search))
|
||||
{
|
||||
rolesQuery = rolesQuery.Where(r => r.Name.Contains(search));
|
||||
}
|
||||
|
||||
var roles = rolesQuery.Where(r => roleIds.Contains(r.Id)).Skip(offset).Take(limit).Select(r => new AuthorityRoleItem()
|
||||
{
|
||||
Id = r.Id,
|
||||
Name = r.Name
|
||||
});
|
||||
return ListResult<AuthorityRoleItem>.Ok(roles, limit, offset);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionListResult<AuthorityRoleItem>("Failed to get user roles.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> AddRolesLinkAsync(List<AuthorityRole> roles, Guid linkId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
foreach (var role in roles)
|
||||
{
|
||||
context.RoleLinks.Add(new RoleLink() { LinkId = linkId, RoleId = role.Id });
|
||||
}
|
||||
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||
return saveResult <= 0 ? Result.Failed("Failed to ad role link!") : Result.Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleException("Failed to add role link!", e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<AuthorityRole>> GetLinkedRolesAsync(List<Guid> linkIds, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var linkedRoles = context.RoleLinks.Where(r => linkIds.Contains(r.LinkId)).Select(r => r.RoleId);
|
||||
var roleList = await context.Roles.Where(r => linkedRoles.Contains(r.Id)).ToListAsync(cancellationToken);
|
||||
return ListResult<AuthorityRole>.Ok(roleList.DistinctBy(r => r.Id));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionListResult<AuthorityRole>("Failed to get linked roles!", e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRolesLinkAsync(List<AuthorityRole> roles, Guid linkId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var roleIds = roles.Select(r => r.Id).ToList();
|
||||
context.RoleLinks.RemoveRange(context.RoleLinks.Where(rg => rg.LinkId == linkId && roleIds.Contains(rg.RoleId)));
|
||||
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||
return saveResult <= 0 ? Result.Failed("Failed to delete role links!") : Result.Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleException("Failed to delete role link!", e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<Guid>> HasRolesAsync(Guid linkId, List<AuthorityRole> roles, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var hasRoles = await context.RoleLinks.Where(r => r.LinkId == linkId && roles.Any(ar => ar.Id == r.RoleId)).Select(r => r.RoleId).ToListAsync(cancellationToken);
|
||||
return ListResult<Guid>.Ok(hasRoles);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionListResult<Guid>("Failed to determine role for user!", e);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user