Skip to content

Commit

Permalink
Attempt to add GQL query for looking up user
Browse files Browse the repository at this point in the history
This fails: despite the AllowAnonymous, the GraphQL query is being
rejected because the RegisterAccount audience is not allowed to use GQL
queries. I'll need to find some other way to do this.
  • Loading branch information
rmunn committed Sep 25, 2024
1 parent 66a20a1 commit 05eda53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 21 additions & 0 deletions backend/LexBoxApi/GraphQL/LexQueries.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using HotChocolate.Authorization;
using HotChocolate.Resolvers;
using LexBoxApi.Auth;
using LexBoxApi.Auth.Attributes;
Expand Down Expand Up @@ -201,6 +202,26 @@ public IQueryable<User> Users(LexBoxDbContext context)
};
}

[AllowAnonymous]
public async Task<MeDto?> UserById(LexBoxDbContext context, LoggedInContext loggedInContext, Guid userId)
{
var registeringUser = loggedInContext.User;
// Only admins can look up users other than themselves via this query
if (!registeringUser.IsAdmin && registeringUser.Id != userId)
{
throw new UnauthorizedAccessException();
}
var user = await context.Users.FindAsync(userId);
if (user == null) return null;
return new MeDto
{
Id = user.Id,
Name = user.Name,
Email = user.Email,
Locale = user.LocalizationCode
};
}

public async Task<OrgMemberDto?> OrgMemberById(LexBoxDbContext context, IPermissionService permissionService, Guid orgId, Guid userId)
{
// Only site admins and org admins are allowed to run this query
Expand Down
3 changes: 2 additions & 1 deletion frontend/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
schema {
schema {
query: Query
mutation: Mutation
}
Expand Down Expand Up @@ -426,6 +426,7 @@ type Query {
orgById(orgId: UUID!): OrgById
users(skip: Int take: Int where: UserFilterInput orderBy: [UserSortInput!]): UsersCollectionSegment @authorize(policy: "AdminRequiredPolicy")
me: MeDto
userById(userId: UUID!): MeDto @authorize(policy: "AllowAnyAudiencePolicy")
orgMemberById(orgId: UUID! userId: UUID!): OrgMemberDto
meAuth: LexAuthUser!
testingThrowsError: LexAuthUser!
Expand Down

0 comments on commit 05eda53

Please sign in to comment.