Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: Allow Member only posts #355

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/LinkDotNet.Blog.Domain/BlogPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public sealed partial class BlogPost : Entity

public int ReadingTimeInMinutes { get; private set; }

public bool IsMembersOnly { get; private set; }

public string Slug => GenerateSlug();

private string GenerateSlug()
Expand Down Expand Up @@ -89,6 +91,7 @@ public static BlogPost Create(
string content,
string previewImageUrl,
bool isPublished,
bool isMembersOnly,
DateTime? updatedDate = null,
DateTime? scheduledPublishDate = null,
IEnumerable<string>? tags = null,
Expand All @@ -113,6 +116,7 @@ public static BlogPost Create(
IsPublished = isPublished,
Tags = tags?.Select(t => t.Trim()).ToImmutableArray() ?? [],
ReadingTimeInMinutes = ReadingTimeCalculator.CalculateReadingTime(content),
IsMembersOnly = isMembersOnly,
};

return blogPost;
Expand Down Expand Up @@ -141,6 +145,7 @@ public void Update(BlogPost from)
PreviewImageUrl = from.PreviewImageUrl;
PreviewImageUrlFallback = from.PreviewImageUrlFallback;
IsPublished = from.IsPublished;
IsMembersOnly = from.IsMembersOnly;
Tags = from.Tags;
ReadingTimeInMinutes = from.ReadingTimeInMinutes;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace LinkDotNet.Blog.Infrastructure.Migrations

Check failure on line 5 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

Convert to file-scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0161)

Check failure on line 5 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

Convert to file-scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0161)
{
/// <inheritdoc />
public partial class BlogPost_MembersOnly : Migration

Check failure on line 8 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from type name LinkDotNet.Blog.Infrastructure.Migrations.BlogPost_MembersOnly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)

Check failure on line 8 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

Remove the underscores from type name LinkDotNet.Blog.Infrastructure.Migrations.BlogPost_MembersOnly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1707)
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(

Check failure on line 13 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

In externally visible method 'void BlogPost_MembersOnly.Up(MigrationBuilder migrationBuilder)', validate parameter 'migrationBuilder' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check failure on line 13 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

In externally visible method 'void BlogPost_MembersOnly.Up(MigrationBuilder migrationBuilder)', validate parameter 'migrationBuilder' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
name: "IsMembersOnly",
table: "BlogPosts",
type: "INTEGER",
nullable: false,
defaultValue: false);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(

Check failure on line 24 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

In externally visible method 'void BlogPost_MembersOnly.Down(MigrationBuilder migrationBuilder)', validate parameter 'migrationBuilder' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check failure on line 24 in src/LinkDotNet.Blog.Infrastructure/Migrations/20241203173853_BlogPost_MembersOnly.cs

View workflow job for this annotation

GitHub Actions / build

In externally visible method 'void BlogPost_MembersOnly.Down(MigrationBuilder migrationBuilder)', validate parameter 'migrationBuilder' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
name: "IsMembersOnly",
table: "BlogPosts");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.IsRequired()
.HasColumnType("TEXT");

b.Property<bool>("IsMembersOnly")
.HasColumnType("INTEGER");

b.Property<bool>("IsPublished")
.HasColumnType("INTEGER");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public static void UseAuthentication(this IServiceCollection services)
};
});

services.AddAuthorization(options =>
{
options.AddPolicy("Admin", policy => policy.RequireRole("Admin"));
options.AddPolicy("Member", policy => policy.RequireRole("Member"));
});

services.AddHttpContextAccessor();
services.AddScoped<ILoginManager, AuthLoginManager>();
}
Expand Down
5 changes: 3 additions & 2 deletions src/LinkDotNet.Blog.Web/Features/AboutMe/AboutMePage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

protected override async Task OnInitializedAsync()
{
var userIdentity = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User.Identity;
isAuthenticated = userIdentity?.IsAuthenticated ?? false;
var principal = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User;
var userIdentity = principal.Identity;
isAuthenticated = (userIdentity?.IsAuthenticated ?? false) && principal.IsInRole("Admin");
}
}
Loading
Loading