Skip to content

add GetMyDirectories endpoint #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/Caster.Api/Features/Directories/Requests/GetAll.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Copyright 2021 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using AutoMapper;
using Caster.Api.Data;
using Microsoft.EntityFrameworkCore;
using System.Runtime.Serialization;
using Caster.Api.Infrastructure.Exceptions;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Caster.Api.Infrastructure.Authorization;
using Caster.Api.Infrastructure.Identity;
using Caster.Api.Features.Shared;
Expand All @@ -36,18 +34,38 @@ public class Query : IRequest<Directory[]>
public bool IncludeFileContent { get; set; }
}

public class Handler(ICasterAuthorizationService authorizationService, IMapper mapper, CasterContext dbContext) : BaseHandler<Query, Directory[]>
public class Handler(
ICasterAuthorizationService authorizationService,
IMapper mapper,
CasterContext dbContext,
IIdentityResolver identityResolver) : BaseHandler<Query, Directory[]>
{
public override async Task<bool> Authorize(Query request, CancellationToken cancellationToken) =>
await authorizationService.Authorize([SystemPermission.ViewProjects], cancellationToken);
public override Task<bool> Authorize(Query request, CancellationToken cancellationToken) => Task.FromResult(true);

public override async Task<Directory[]> HandleRequest(Query request, CancellationToken cancellationToken)
{
return await dbContext.Directories
.Expand(mapper.ConfigurationProvider, request.IncludeRelated, request.IncludeFileContent)
.ToArrayAsync();
if (await authorizationService.Authorize([SystemPermission.ViewProjects], cancellationToken))
{
return await dbContext.Directories
.Expand(mapper.ConfigurationProvider, request.IncludeRelated, request.IncludeFileContent)
.ToArrayAsync(cancellationToken);
}
else
{
var userId = identityResolver.GetId();
var myProjectIds = await dbContext.ProjectMemberships
.Where(pm => pm.UserId == userId)
.Select(pm => pm.ProjectId)
.ToListAsync(cancellationToken);

var myDirectories = await dbContext.Directories
.Where(d => myProjectIds.Contains(d.ProjectId))
.Expand(mapper.ConfigurationProvider, request.IncludeRelated, request.IncludeFileContent)
.ToArrayAsync(cancellationToken);

return myDirectories;
}
}
}
}
}

Loading