Use IUserGroupAuthority functions in the controller

This commit is contained in:
Jordan Dominion
2024-09-15 13:19:17 -04:00
parent bfe12f0399
commit 547fb9de7d
6 changed files with 31 additions and 28 deletions
@@ -24,16 +24,18 @@ namespace Tgstation.Server.Host.Authority
/// Gets the <see cref="UserGroup"/> with a given <paramref name="id"/>.
/// </summary>
/// <param name="id">The <see cref="Api.Models.EntityId.Id"/> of the <see cref="UserGroup"/>.</param>
/// <param name="includeJoins">If related entities should be loaded.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="ValueTask{TResult}"/> resulting in a <see cref="User"/> <see cref="AuthorityResponse{TResult}"/>.</returns>
[TgsAuthorize(AdministrationRights.ReadUsers)]
public ValueTask<AuthorityResponse<UserGroup>> GetId(long id, CancellationToken cancellationToken);
public ValueTask<AuthorityResponse<UserGroup>> GetId(long id, bool includeJoins, CancellationToken cancellationToken);
/// <summary>
/// Gets all registered <see cref="UserGroup"/>s.
/// </summary>
/// <param name="includeJoins">If related entities should be loaded.</param>
/// <returns>A <see cref="IQueryable{T}"/> of <see cref="UserGroup"/>s.</returns>
[TgsAuthorize(AdministrationRights.ReadUsers)]
IQueryable<UserGroup> Queryable();
IQueryable<UserGroup> Queryable(bool includeJoins);
}
}
@@ -68,12 +68,19 @@ namespace Tgstation.Server.Host.Authority
}
/// <inheritdoc />
public async ValueTask<AuthorityResponse<UserGroup>> GetId(long id, CancellationToken cancellationToken)
public async ValueTask<AuthorityResponse<UserGroup>> GetId(long id, bool includeJoins, CancellationToken cancellationToken)
{
if (id != AuthenticationContext.User.GroupId && !((AdministrationRights)AuthenticationContext.GetRight(RightsType.Administration)).HasFlag(AdministrationRights.ReadUsers))
return Forbid<UserGroup>();
var userGroup = await userGroupsDataLoader.LoadAsync(id, cancellationToken);
UserGroup? userGroup;
if (includeJoins)
userGroup = await Queryable(true)
.Where(x => x.Id == id)
.FirstOrDefaultAsync(cancellationToken);
else
userGroup = await userGroupsDataLoader.LoadAsync(id, cancellationToken);
if (userGroup == null)
return NotFound<UserGroup>();
@@ -91,9 +98,18 @@ namespace Tgstation.Server.Host.Authority
}
/// <inheritdoc />
public IQueryable<UserGroup> Queryable()
=> DatabaseContext
public IQueryable<UserGroup> Queryable(bool includeJoins)
{
var queryable = DatabaseContext
.Groups
.AsQueryable();
if (includeJoins)
queryable = queryable
.Include(x => x.Users)
.Include(x => x.PermissionSet);
return queryable;
}
}
}
@@ -167,20 +167,8 @@ namespace Tgstation.Server.Host.Controllers
[TgsRestAuthorize<IUserGroupAuthority>(nameof(IUserGroupAuthority.GetId))]
[ProducesResponseType(typeof(UserGroupResponse), 200)]
[ProducesResponseType(typeof(ErrorMessageResponse), 410)]
public async ValueTask<IActionResult> GetId(long id, CancellationToken cancellationToken)
{
// this functions as userId
var group = await DatabaseContext
.Groups
.AsQueryable()
.Where(x => x.Id == id)
.Include(x => x.Users)
.Include(x => x.PermissionSet)
.FirstOrDefaultAsync(cancellationToken);
if (group == default)
return this.Gone();
return Json(group.ToApi(true));
}
public ValueTask<IActionResult> GetId(long id, CancellationToken cancellationToken)
=> userGroupAuthority.InvokeTransformable<UserGroup, UserGroupResponse>(this, authority => authority.GetId(id, true, cancellationToken));
/// <summary>
/// Lists all <see cref="UserGroup"/>s.
@@ -197,11 +185,8 @@ namespace Tgstation.Server.Host.Controllers
=> Paginated<UserGroup, UserGroupResponse>(
() => ValueTask.FromResult(
new PaginatableResult<UserGroup>(
DatabaseContext
.Groups
.AsQueryable()
.Include(x => x.Users)
.Include(x => x.PermissionSet)
userGroupAuthority
.InvokeQueryable(authority => authority.Queryable(true))
.OrderBy(x => x.Id))),
null,
page,
@@ -167,7 +167,7 @@ namespace Tgstation.Server.Host.GraphQL.Types
return null;
return await userGroupAuthority.InvokeTransformable<Models.UserGroup, UserGroup, UserGroupGraphQLTransformer>(
authority => authority.GetId(GroupId.Value, cancellationToken));
authority => authority.GetId(GroupId.Value, false, cancellationToken));
}
}
}
@@ -34,7 +34,7 @@ namespace Tgstation.Server.Host.GraphQL.Types
{
ArgumentNullException.ThrowIfNull(userGroupAuthority);
return userGroupAuthority.InvokeTransformable<Models.UserGroup, UserGroup, UserGroupGraphQLTransformer>(
authority => authority.GetId(id, cancellationToken));
authority => authority.GetId(id, false, cancellationToken));
}
/// <summary>
@@ -58,7 +58,7 @@ namespace Tgstation.Server.Host.GraphQL.Types
[Service] IGraphQLAuthorityInvoker<IUserGroupAuthority> userGroupAuthority)
{
ArgumentNullException.ThrowIfNull(userGroupAuthority);
var dtoQueryable = userGroupAuthority.InvokeTransformableQueryable<Models.UserGroup, UserGroup, UserGroupGraphQLTransformer>(authority => authority.Queryable());
var dtoQueryable = userGroupAuthority.InvokeTransformableQueryable<Models.UserGroup, UserGroup, UserGroupGraphQLTransformer>(authority => authority.Queryable(false));
return dtoQueryable;
}