Skip to content
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

Wire up EditForumPost permission #1915

Merged
merged 2 commits into from
Jul 7, 2024
Merged
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
18 changes: 10 additions & 8 deletions TASVideos/Pages/Forum/Posts/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TASVideos.Pages.Forum.Posts;
true,
PermissionTo.SeeRestrictedForums,
PermissionTo.CreateForumPosts,
PermissionTo.EditForumPosts,
PermissionTo.DeleteForumPosts,
PermissionTo.EditUsersForumPosts)]
public class EditModel(
Expand Down Expand Up @@ -51,6 +52,11 @@ public async Task<IActionResult> OnGet()
return NotFound();
}

if (!CanEditPost(post.PosterId))
{
return AccessDenied();
}

Post = post;
var firstPostId = await db.ForumPosts
.ForTopic(Post.TopicId)
Expand All @@ -60,12 +66,6 @@ public async Task<IActionResult> OnGet()

IsFirstPost = Id == firstPostId;

if (!User.Has(PermissionTo.EditUsersForumPosts)
&& Post.PosterId != User.GetUserId())
{
return AccessDenied();
}

PreviousPosts = await db.ForumPosts
.ForTopic(Post.TopicId)
.Where(fp => fp.CreateTimestamp < Post.CreateTimestamp)
Expand Down Expand Up @@ -112,8 +112,7 @@ public async Task<IActionResult> OnPost()
return NotFound();
}

if (!User.Has(PermissionTo.EditUsersForumPosts)
&& forumPost.PosterId != User.GetUserId())
if (!CanEditPost(forumPost.PosterId))
{
ModelState.AddModelError("", "Unable to edit post.");
return Page();
Expand Down Expand Up @@ -273,6 +272,9 @@ await publisher.SendForum(
: BasePageRedirect("/Forum/Topics/Index", new { id = post.TopicId });
}

private bool CanEditPost(int posterId) => User.Has(PermissionTo.EditUsersForumPosts)
|| (User.Has(PermissionTo.EditForumPosts) && posterId == User.GetUserId());

public class ForumPostEditModel
{
public int PosterId { get; init; }
Expand Down
2 changes: 1 addition & 1 deletion TASVideos/Pages/Forum/Posts/User.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task<IActionResult> OnGet()

var isOwnPost = post.PosterId == User.GetUserId();
var isOpenTopic = !post.TopicIsLocked;
post.IsEditable = User.Has(PermissionTo.EditUsersForumPosts) || (isOwnPost && isOpenTopic);
post.IsEditable = User.Has(PermissionTo.EditUsersForumPosts) || (isOwnPost && User.Has(PermissionTo.EditForumPosts) && isOpenTopic);

// Note: IsLastPost is always false, because calculating it for every topic is too expensive, so we only check permissions
// The goal here is for moderators to be able to modify posts from this screen, as a convenience
Expand Down
2 changes: 1 addition & 1 deletion TASVideos/Pages/Forum/Topics/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public async Task<IActionResult> OnGet()
var isOwnPost = post.PosterId == userId;
var isOpenTopic = !topic.IsLocked;
post.IsEditable = User.Has(PermissionTo.EditUsersForumPosts)
|| (isOwnPost && isOpenTopic);
|| (isOwnPost && User.Has(PermissionTo.EditForumPosts) && isOpenTopic);
post.IsDeletable = User.Has(PermissionTo.DeleteForumPosts)
|| (isOwnPost && isOpenTopic && post.IsLastPost);
}
Expand Down
Loading