Skip to content

Commit

Permalink
Allow review from unauthorized users
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszgarstecki committed Oct 14, 2024
1 parent 6b35bd7 commit 5467faa
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions backend/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Update="LeanCode.CQRS.MassTransitRelay" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.CQRS.Validation.Fluent" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.DomainModels.EF" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.DomainModels.Generators" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.SendGrid" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.TimeProvider" Version="$(CoreLibVersion)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace LeanCode.AppRating.Contracts;

[AuthorizeWhenHasAnyOf(RatingPermissions.RateApp)]
[AllowUnauthorized]
public class SubmitAppRating : ICommand
{
public double Rating { get; set; }
Expand Down
7 changes: 6 additions & 1 deletion backend/src/LeanCode.AppRating/DataAccess/AppRating.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Collections.Immutable;
using LeanCode.DomainModels.Ids;

namespace LeanCode.AppRating.DataAccess;

[TypedId(TypedIdFormat.RawGuid)]
public readonly partial record struct AppRatingId;

public sealed record class AppRating<TUserId>(
TUserId UserId,
AppRatingId Id,
TUserId? UserId,
DateTimeOffset DateCreated,
double Rating,
string? AdditionalComment,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Text.Json;
using LeanCode.DomainModels.EF;
using Microsoft.EntityFrameworkCore;

namespace LeanCode.AppRating.DataAccess;
Expand All @@ -11,7 +12,10 @@ public static void ConfigureAppRatingEntity<TUserId>(this ModelBuilder builder,
{
builder.Entity<AppRating<TUserId>>(c =>
{
c.HasKey(e => new { e.UserId, e.DateCreated });
c.HasKey(e => e.Id);
c.Property(e => e.Id).HasConversion<RawTypedIdConverter<Guid, AppRatingId>>();

c.HasIndex(e => new { e.UserId, e.DateCreated });

c.Property(e => e.UserId).ValueGeneratedNever();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace LeanCode.AppRating.EmailViewModels;
public class LowRateSubmittedEmail
{
public double Rating { get; set; }
public string UserId { get; set; } = null!;
public string? UserId { get; set; }
public string? AdditionalComment { get; set; }
}
12 changes: 8 additions & 4 deletions backend/src/LeanCode.AppRating/Handlers/RatingAlreadySentQH.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public RatingAlreadySentQH(IAppRatingStore<TUserId> store, IUserIdExtractor<TUse

public Task<bool> ExecuteAsync(HttpContext context, RatingAlreadySent query)
{
return store
.AppRatings
.Where(r => (object)r.UserId == (object)extractor.Extract(context))
.AnyAsync(context.RequestAborted);
if (extractor.TryExtract(context, out var userId))
{
return store.AppRatings.Where(r => (object?)r.UserId == (object?)userId).AnyAsync(context.RequestAborted);
}
else
{
throw new InvalidOperationException("UserId could not be extracted.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Consume(ConsumeContext<LowRateSubmitted<TUserId>> context)
{
var vm = new LowRateSubmittedEmail
{
UserId = context.Message.UserId.ToString()!,
UserId = context.Message.UserId?.ToString(),
AdditionalComment = context.Message.AdditionalComment,
Rating = context.Message.Rating,
};
Expand All @@ -47,5 +47,5 @@ public async Task Consume(ConsumeContext<LowRateSubmitted<TUserId>> context)
}
}

public sealed record class LowRateSubmitted<TUserId>(TUserId UserId, double Rating, string? AdditionalComment)
public sealed record class LowRateSubmitted<TUserId>(TUserId? UserId, double Rating, string? AdditionalComment)
where TUserId : notnull, IEquatable<TUserId>;
4 changes: 2 additions & 2 deletions backend/src/LeanCode.AppRating/Handlers/SubmitAppRatingCH.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Runtime.InteropServices;
using FluentValidation;
using LeanCode.AppRating.Configuration;
using LeanCode.AppRating.Contracts;
Expand Down Expand Up @@ -59,12 +58,13 @@ AppRatingReportsConfiguration appRatingReportsConfiguration

public async Task ExecuteAsync(HttpContext context, SubmitAppRating command)
{
var userId = extractor.Extract(context);
var userId = extractor.TryExtract(context, out var uid) ? uid : default;

store
.AppRatings
.Add(
new AppRating<TUserId>(
AppRatingId.New(),
userId,
Time.NowWithOffset,
command.Rating,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/LeanCode.AppRating/IUserIdExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace LeanCode.AppRating;

public interface IUserIdExtractor<TUserId>
{
public TUserId Extract(HttpContext httpContext);
public bool TryExtract(HttpContext httpContext, out TUserId? userId);
}
6 changes: 6 additions & 0 deletions backend/src/LeanCode.AppRating/LeanCode.AppRating.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LeanCode.Components" />
<PackageReference Include="LeanCode.CQRS.AspNetCore" />
<PackageReference Include="LeanCode.CQRS.Execution" />
<PackageReference Include="LeanCode.CQRS.MassTransitRelay" />
<PackageReference Include="LeanCode.CQRS.Validation.Fluent" />
<PackageReference Include="LeanCode.DomainModels.EF" />
<PackageReference Include="LeanCode.DomainModels.Generators" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="LeanCode.SendGrid" />
<PackageReference Include="LeanCode.TimeProvider" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ protected override void ConfigureApp(IApplicationBuilder app)

public sealed class UserIdExtractor : IUserIdExtractor<Guid>
{
public Guid Extract(HttpContext httpContext)
public bool TryExtract(HttpContext httpContext, out Guid userId)
{
var claim = httpContext.User.FindFirstValue(KnownClaims.UserId);

ArgumentException.ThrowIfNullOrEmpty(claim);

return Guid.Parse(claim);
return Guid.TryParse(claim, out userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using LeanCode.AppRating.DataAccess;
using LeanCode.CQRS.MassTransitRelay.LockProviders;
using LeanCode.DomainModels.EF;
using LeanCode.IntegrationTestHelpers;
using MassTransit;
using Microsoft.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using FluentAssertions;
using LeanCode.AppRating.Contracts;
using LeanCode.AppRating.IntegrationTests;
using LeanCode.AppRating.IntegrationTests.App;
using LeanCode.AppRating.IntegrationTests.Helpers;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace LeanCode.NotificationCenter.IntegrationTests.Tests;
namespace LeanCode.AppRating.IntegrationTests.Tests;

public class SubmitReviewTests : TestsBase<TestApp>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using LeanCode.AppRating.Handlers;
using Xunit;

namespace LeanCode.AppRating.Tests;
namespace LeanCode.AppRating.Tests.CQRS;

public class SubmitAppRatingCVTests
{
Expand Down

0 comments on commit 5467faa

Please sign in to comment.