Skip to content

Commit ea85f7a

Browse files
committed
feat: Allow Member only posts
1 parent 11a46d5 commit ea85f7a

File tree

22 files changed

+219
-61
lines changed

22 files changed

+219
-61
lines changed

src/LinkDotNet.Blog.Domain/BlogPost.cs

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public sealed partial class BlogPost : Entity
3636

3737
public int ReadingTimeInMinutes { get; private set; }
3838

39+
public bool IsMembersOnly { get; private set; }
40+
3941
public string Slug => GenerateSlug();
4042

4143
private string GenerateSlug()
@@ -89,6 +91,7 @@ public static BlogPost Create(
8991
string content,
9092
string previewImageUrl,
9193
bool isPublished,
94+
bool isMembersOnly,
9295
DateTime? updatedDate = null,
9396
DateTime? scheduledPublishDate = null,
9497
IEnumerable<string>? tags = null,
@@ -113,6 +116,7 @@ public static BlogPost Create(
113116
IsPublished = isPublished,
114117
Tags = tags?.Select(t => t.Trim()).ToImmutableArray() ?? [],
115118
ReadingTimeInMinutes = ReadingTimeCalculator.CalculateReadingTime(content),
119+
IsMembersOnly = isMembersOnly,
116120
};
117121

118122
return blogPost;
@@ -141,6 +145,7 @@ public void Update(BlogPost from)
141145
PreviewImageUrl = from.PreviewImageUrl;
142146
PreviewImageUrlFallback = from.PreviewImageUrlFallback;
143147
IsPublished = from.IsPublished;
148+
IsMembersOnly = from.IsMembersOnly;
144149
Tags = from.Tags;
145150
ReadingTimeInMinutes = from.ReadingTimeInMinutes;
146151
}

src/LinkDotNet.Blog.Web/Authentication/OpenIdConnect/AuthExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public static void UseAuthentication(this IServiceCollection services)
5353
};
5454
});
5555

56+
services.AddAuthorization(options =>
57+
{
58+
options.AddPolicy("Admin", policy => policy.RequireRole("Admin"));
59+
options.AddPolicy("Member", policy => policy.RequireRole("Member"));
60+
});
61+
5662
services.AddHttpContextAccessor();
5763
services.AddScoped<ILoginManager, AuthLoginManager>();
5864
}

src/LinkDotNet.Blog.Web/Features/AboutMe/AboutMePage.razor

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
protected override async Task OnInitializedAsync()
3232
{
33-
var userIdentity = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User.Identity;
34-
isAuthenticated = userIdentity?.IsAuthenticated ?? false;
33+
var principal = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User;
34+
var userIdentity = principal.Identity;
35+
isAuthenticated = (userIdentity?.IsAuthenticated ?? false) && principal.IsInRole("Admin");
3536
}
3637
}

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewBlogPost.razor

+6-1
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@
8080
<InputText type="text" class="form-control" id="tags" placeholder="Tags" @bind-Value="model.Tags"/>
8181
<label for="tags">Tags</label>
8282
</div>
83+
<div class="form-check form-switch mb-3">
84+
<InputCheckbox class="form-check-input" id="members-only" @bind-Value="model.IsMembersOnly" />
85+
<label class="form-check-label" for="members-only">Members only?</label><br/>
86+
<small for="updatedate" class="form-text text-body-secondary">The blog post can only be read by members.</small>
87+
</div>
8388
@if (BlogPost is not null && !IsScheduled)
8489
{
8590
<div class="form-check form-switch mb-3">
86-
<InputCheckbox class="form-check-input" id="updatedate" @bind-Value="model.ShouldUpdateDate" />
91+
<InputCheckbox class="form-check-input" id="updatedate" @bind-Value="model.ShouldUpdateDate" />
8792
<label class="form-check-label" for="updatedate">Update Publish Date</label><br/>
8893
<small for="updatedate" class="form-text text-body-secondary">If set the publish date is set to now,
8994
otherwise its original date.</small>

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewModel.cs

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public sealed class CreateNewModel
1818
private string tags = string.Empty;
1919
private string previewImageUrlFallback = string.Empty;
2020
private DateTime? scheduledPublishDate;
21+
private bool isMembersOnly;
2122

2223
[Required]
2324
[MaxLength(256)]
@@ -64,6 +65,12 @@ public bool ShouldUpdateDate
6465
set => SetProperty(out shouldUpdateDate, value);
6566
}
6667

68+
public bool IsMembersOnly
69+
{
70+
get => isMembersOnly;
71+
set => SetProperty(out isMembersOnly, value);
72+
}
73+
6774
[FutureDateValidation]
6875
public DateTime? ScheduledPublishDate
6976
{
@@ -128,6 +135,7 @@ public BlogPost ToBlogPost()
128135
Content,
129136
PreviewImageUrl,
130137
IsPublished,
138+
IsMembersOnly,
131139
updatedDate,
132140
scheduledPublishDate,
133141
tagList,

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/CreateBlogPost.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/create"
2-
@attribute [Authorize]
2+
@attribute [Authorize(Roles = "Admin")]
33
@using LinkDotNet.Blog.Domain
44
@using LinkDotNet.Blog.Infrastructure.Persistence
55
@using LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components

src/LinkDotNet.Blog.Web/Features/Admin/Sitemap/SitemapPage.razor

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@page "/Sitemap"
22
@using LinkDotNet.Blog.Web.Features.Admin.Sitemap.Services
33
@inject ISitemapService SitemapService
4-
@attribute [Authorize]
4+
@attribute [Authorize(Roles = "Admin")]
55
<div class="container">
66
<h3>Sitemap</h3>
77
<div class="row px-2">
@@ -11,12 +11,12 @@
1111
If you get a 404 there is currently no sitemap.xml</p>
1212
<button class="btn btn-primary" @onclick="CreateSitemap" disabled="@isGenerating">Create Sitemap</button>
1313

14-
@if (isGenerating)
15-
{
16-
<Loading></Loading>
17-
}
18-
@if (sitemapUrlSet is not null)
19-
{
14+
@if (isGenerating)
15+
{
16+
<Loading></Loading>
17+
}
18+
@if (sitemapUrlSet is not null)
19+
{
2020
<table class="table table-striped table-hover h-50">
2121
<thead>
2222
<tr>

src/LinkDotNet.Blog.Web/Features/Components/ShortBlogPost.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@using LinkDotNet.Blog.Domain
22

33
<article>
4-
<div class="blog-card @AltCssClass">
4+
<div class="blog-card @AltCssClass @(BlogPost.IsMembersOnly ? "border border-warning" : "")">
55
<div class="meta">
66
<div class="photo">
77
<PreviewImage PreviewImageUrl="@BlogPost.PreviewImageUrl"
@@ -37,7 +37,7 @@
3737
<h2></h2>
3838
<p>@MarkdownConverter.ToMarkupString(BlogPost.ShortDescription)</p>
3939
<p class="read-more">
40-
<a href="/blogPost/@BlogPost.Id/@BlogPost.Slug" aria-label="@BlogPost.Title">Read the whole article</a>
40+
<a href="/blogPost/@BlogPost.Id/@BlogPost.Slug" aria-label="@BlogPost.Title">Read the whole article</a>
4141
</p>
4242
</div>
4343
</div>

src/LinkDotNet.Blog.Web/Features/Home/Components/AccessControl.razor

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
<AuthorizeView>
2-
<Authorized>
3-
<li class="nav-item dropdown">
4-
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown"
5-
aria-expanded="false">
6-
<i class="user-tie"></i> Admin
7-
</a>
8-
<ul class="dropdown-menu ps-0" aria-labelledby="navbarDropdown">
9-
<li><h6 class="dropdown-header">Blog posts</h6></li>
10-
<li><a class="dropdown-item" href="create">Create new</a></li>
11-
<li><a class="dropdown-item" href="draft">Show drafts</a></li>
12-
<li><a class="dropdown-item" href="settings">Show settings</a></li>
13-
<li><hr class="dropdown-divider"></li>
14-
<li><h6 class="dropdown-header">Analytics</h6></li>
15-
<li><a class="dropdown-item" href="dashboard">Dashboard</a></li>
16-
<li><hr class="dropdown-divider"></li>
17-
<li><h6 class="dropdown-header">Others</h6></li>
18-
<li><a class="dropdown-item" href="short-codes">Shortcodes</a></li>
19-
<li><a class="dropdown-item" href="Sitemap">Sitemap</a></li>
20-
<li><hr class="dropdown-divider"></li>
21-
<li><a class="dropdown-item" target="_blank" href="https://github.com/linkdotnet/Blog/releases" rel="noreferrer">Releases</a></li>
22-
</ul>
23-
</li>
24-
<li class="nav-item"><a class="nav-link" href="logout?redirectUri=@CurrentUri"><i class="lock"></i> Log out</a></li>
25-
</Authorized>
26-
<NotAuthorized>
27-
<li class="nav-item"><a class="nav-link" href="login?redirectUri=@CurrentUri" rel="nofollow"><i class="unlocked"></i> Log in</a></li>
28-
</NotAuthorized>
1+
<AuthorizeView Roles="Admin,Member">
2+
<Authorized>
3+
<AuthorizeView Roles="Admin" Context="AdminContext">
4+
<li class="nav-item dropdown" id="admin-actions">
5+
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown"
6+
aria-expanded="false">
7+
<i class="user-tie"></i> Admin
8+
</a>
9+
<ul class="dropdown-menu ps-0" aria-labelledby="navbarDropdown">
10+
<li><h6 class="dropdown-header">Blog posts</h6></li>
11+
<li><a class="dropdown-item" href="create">Create new</a></li>
12+
<li><a class="dropdown-item" href="draft">Show drafts</a></li>
13+
<li><a class="dropdown-item" href="settings">Show settings</a></li>
14+
<li><hr class="dropdown-divider"></li>
15+
<li><h6 class="dropdown-header">Analytics</h6></li>
16+
<li><a class="dropdown-item" href="dashboard">Dashboard</a></li>
17+
<li><hr class="dropdown-divider"></li>
18+
<li><h6 class="dropdown-header">Others</h6></li>
19+
<li><a class="dropdown-item" href="short-codes">Shortcodes</a></li>
20+
<li><a class="dropdown-item" href="Sitemap">Sitemap</a></li>
21+
<li><hr class="dropdown-divider"></li>
22+
<li><a class="dropdown-item" target="_blank" href="https://github.com/linkdotnet/Blog/releases" rel="noreferrer">Releases</a></li>
23+
</ul>
24+
</li>
25+
</AuthorizeView>
26+
<li class="nav-item"><a class="nav-link" href="logout?redirectUri=@CurrentUri"><i class="lock"></i> Log out</a></li>
27+
</Authorized>
28+
<NotAuthorized>
29+
<li class="nav-item"><a class="nav-link" href="login?redirectUri=@CurrentUri" rel="nofollow"><i class="unlocked"></i> Log in</a></li>
30+
</NotAuthorized>
2931
</AuthorizeView>
3032

3133
@code {

src/LinkDotNet.Blog.Web/Features/Home/Index.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
AbsolutePreviewImageUrl="@ImageUrl"
2020
Description="@(Markdown.ToPlainText(Introduction.Value.Description))"></OgData>
2121
<section>
22-
<IntroductionCard></IntroductionCard>
22+
<IntroductionCard></IntroductionCard>
2323
</section>
2424

2525
<section>

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/BlogPostAdminActions.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@inject IRepository<BlogPost> BlogPostRepository
77
@inject IInstantJobRegistry InstantJobRegistry
88

9-
<AuthorizeView>
9+
<AuthorizeView Roles="Admin">
1010
<div class="d-flex justify-content-start gap-2">
1111
<a id="edit-blogpost" type="button" class="btn btn-primary d-flex align-items-center gap-2" href="update/@BlogPostId" aria-label="edit">
1212
<i class="pencil"></i>

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/ShowBlogPostPage.razor

+71-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@inject IOptions<ApplicationConfiguration> AppConfiguration
1515
@inject IOptions<ProfileInformation> ProfileInformation
1616
@inject IOptions<SupportMeConfiguration> SupportConfiguration
17+
@inject AuthenticationStateProvider AuthenticationStateProvider
1718

1819
@if (isLoading)
1920
{
@@ -45,6 +46,8 @@ else if (BlogPost is not null)
4546

4647
<div class="d-flex justify-content-center pt-2 blog-outer-box">
4748
<div class="blog-container">
49+
@if (hasPermission)
50+
{
4851
<div class="blog-inner-content">
4952
<header class="text-center">
5053
<h1 class="fw-bold">@BlogPost.Title</h1></header>
@@ -79,6 +82,13 @@ else if (BlogPost is not null)
7982
@(EnrichWithShortCodes(BlogPost.Content))
8083
</div>
8184
</div>
85+
}
86+
else
87+
{
88+
<div class="alert alert-warning text-center" role="alert">
89+
<h1 class="fs-3">This content is only available for members.</h1>
90+
</div>
91+
}
8292
<div class="d-flex justify-content-between py-2 border-top border-bottom align-items-center">
8393
<Like BlogPost="@BlogPost" OnBlogPostLiked="@UpdateLikes"></Like>
8494
<ShareBlogPost></ShareBlogPost>
@@ -89,9 +99,58 @@ else if (BlogPost is not null)
8999
}
90100
@if (AppConfiguration.Value.ShowSimilarPosts)
91101
{
92-
<SimilarBlogPostSection BlogPost="@BlogPost" />
102+
<div class="blog-inner-content">
103+
<header class="text-center">
104+
<h1 class="fw-bold">@BlogPost.Title</h1></header>
105+
<div class="text-dark-emphasis d-flex flex-wrap gap-2">
106+
<div class="me-2">
107+
<span class="date"></span>
108+
<span class="ms-1">@BlogPost.UpdatedDate.ToShortDateString()</span>
109+
</div>
110+
@if (BlogPost.Tags is not null && BlogPost.Tags.Any())
111+
{
112+
<div class="d-flex align-items-center">
113+
<span class="blogpost-tag me-2"></span>
114+
<div class="d-flex flex-wrap gap-2">
115+
@foreach (var tag in BlogPost.Tags)
116+
{
117+
<a class="goto-tag badge bg-primary rounded-pill text-decoration-none" href="/searchByTag/@(Uri.EscapeDataString(tag))">@tag</a>
118+
}
119+
</div>
120+
</div>
121+
}
122+
</div>
123+
124+
<div class="pt-2">
125+
<BlogPostAdminActions BlogPostId="@BlogPostId"></BlogPostAdminActions>
126+
</div>
127+
128+
<div class="pt-2">
129+
<TableOfContents Content="@BlogPost.Content" CurrentUri="@NavigationManager.Uri"></TableOfContents>
130+
</div>
131+
132+
<div class="blogpost-content">
133+
@(EnrichWithShortCodes(BlogPost.Content))
134+
</div>
135+
</div>
136+
<div class="d-flex justify-content-between py-2 border-top border-bottom align-items-center">
137+
<Like BlogPost="@BlogPost" OnBlogPostLiked="@UpdateLikes"></Like>
138+
<ShareBlogPost></ShareBlogPost>
139+
</div>
140+
<DonationSection></DonationSection>
141+
@if (AppConfiguration.Value.ShowSimilarPosts)
142+
{
143+
<SimilarBlogPostSection BlogPost="@BlogPost"/>
144+
}
145+
146+
<CommentSection></CommentSection>
147+
}
148+
else
149+
{
150+
<div class="alert alert-warning text-center" role="alert">
151+
<h1 class="fs-3">This content is only available for members.</h1>
152+
</div>
93153
}
94-
<CommentSection></CommentSection>
95154
</div>
96155
</div>
97156

@@ -113,6 +172,7 @@ else if (BlogPost is not null)
113172
private string OgDataImage => BlogPost!.PreviewImageUrlFallback ?? BlogPost.PreviewImageUrl;
114173
private string BlogPostCanoncialUrl => $"blogPost/{BlogPost?.Id}";
115174
private IReadOnlyCollection<ShortCode> shortCodes = [];
175+
private bool hasPermission;
116176

117177
private BlogPost? BlogPost { get; set; }
118178

@@ -125,6 +185,15 @@ else if (BlogPost is not null)
125185
{
126186
isLoading = true;
127187
BlogPost = await BlogPostRepository.GetByIdAsync(BlogPostId);
188+
if (BlogPost?.IsMembersOnly ?? false)
189+
{
190+
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
191+
hasPermission = state.User.IsInRole("Admin") || state.User.IsInRole("Member");
192+
}
193+
else
194+
{
195+
hasPermission = true;
196+
}
128197
isLoading = false;
129198
}
130199

src/LinkDotNet.Blog.Web/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private static void RegisterServices(WebApplicationBuilder builder)
3737
.AddResponseCompression()
3838
.AddHealthCheckSetup();
3939

40-
if (builder.Environment.IsDevelopment())
40+
if (!builder.Environment.IsDevelopment())
4141
{
4242
builder.Services.UseDummyAuthentication();
4343
}

0 commit comments

Comments
 (0)