Skip to content

Commit

Permalink
add endpoint to check if user is a worker (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-jmattson authored Oct 28, 2024
1 parent 28c3b84 commit 8968fd1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/TopoMojo.Api/Features/User/IUserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public interface IUserStore : IStore<User>
Task<User> ResolveApiKey(string hash);
Task DeleteApiKey(string id);
Task<string[]> ListScopes();
Task<int> WorkspaceCount(string id);
}
}
10 changes: 10 additions & 0 deletions src/TopoMojo.Api/Features/User/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ public async Task<IActionResult> DeleteUser(string id)
return Ok();
}

[HttpGet("api/user/{id}/worker")]
[SwaggerOperation(OperationId = "IsWorker")]
[Authorize]
public async Task<IActionResult> IsWorker(string id)
{
return Ok(
await _svc.CanWork(id)
);
}

/// <summary>
/// Get a user's api key records (no values)
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions src/TopoMojo.Api/Features/User/UserModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ public class User
;
public bool IsObserver =>
Role == UserRole.Observer ||
Role == UserRole.Creator ||
Role == UserRole.Administrator
;
public bool IsCreator =>
Role == UserRole.Creator ||
Role == UserRole.Observer ||
Role == UserRole.Administrator
;
public bool IsBuilder =>
Role == UserRole.Builder ||
Role == UserRole.Creator ||
Role == UserRole.Observer ||
Role == UserRole.Administrator
;
public bool HasScope(string scope) {
Expand Down
14 changes: 8 additions & 6 deletions src/TopoMojo.Api/Features/User/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// Copyright 2021 Carnegie Mellon University.
// Released under a MIT (SEI) license. See LICENSE.md in the project root.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using TopoMojo.Api.Data.Abstractions;
using TopoMojo.Api.Extensions;
using TopoMojo.Api.Models;
Expand Down Expand Up @@ -47,7 +42,7 @@ IMemoryCache userCache

if (search.WantsAdmins)
q = q.Where(p => p.Role == UserRole.Administrator);

if (search.WantsObservers)
q = q.Where(p => p.Role == UserRole.Observer);

Expand Down Expand Up @@ -163,6 +158,13 @@ public async Task Delete(string id)
await _store.Delete(id);
}

public async Task<bool> CanWork(string id)
{
var user = await Load(id);
int count = await _store.WorkspaceCount(id);
return user.IsCreator || user.WorkspaceLimit > 0 || count > 0;
}

public async Task<bool> CanInteract(string subjectId, string isolationId)
{
return await _store.CanInteract(subjectId, isolationId);
Expand Down
9 changes: 7 additions & 2 deletions src/TopoMojo.Api/Features/User/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public async Task<bool> CanInteractWithAudience(string scope, string isolationId
);
if (gamespace != null)
return gamespace.Workspace.Audience.HasAnyToken(scope);

var workspace = await DbContext.Workspaces
.FirstOrDefaultAsync(g =>
g.Id == isolationId
);
if (workspace != null)
return workspace.Audience.HasAnyToken(scope);

return false;
}

Expand Down Expand Up @@ -139,5 +139,10 @@ public async Task<string[]> ListScopes()
.ToArrayAsync()
;
}

public async Task<int> WorkspaceCount(string id)
{
return await DbContext.Workers.CountAsync(w => w.SubjectId == id);
}
}
}

0 comments on commit 8968fd1

Please sign in to comment.