mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +08:00
Adding support for restricting comments to authenticated users only
--HG-- branch : 1.x extra : rebase_source : a8b7abba754a5dc70eb301b308f6b9becbd66a95
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Comments.Models;
|
||||
using Orchard.Comments.Services;
|
||||
using Orchard.Comments.Settings;
|
||||
using Orchard.Comments.ViewModels;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Localization;
|
||||
@@ -43,9 +44,13 @@ namespace Orchard.Comments.Controllers {
|
||||
CommentsPart commentsPart = null;
|
||||
if(container != null) {
|
||||
commentsPart = container.As<CommentsPart>();
|
||||
if(commentsPart != null && !commentsPart.CommentsActive) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return this.RedirectLocal(returnUrl, "~/");
|
||||
if (commentsPart != null) {
|
||||
var settings = commentsPart.TypePartDefinition.Settings.GetModel<CommentsPartSettings>();
|
||||
if (!commentsPart.CommentsActive
|
||||
|| (settings.MustBeAuthenticated && Services.WorkContext.CurrentUser == null)) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return this.RedirectLocal(returnUrl, "~/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -12,12 +12,15 @@ namespace Orchard.Comments.Drivers {
|
||||
public class CommentsPartDriver : ContentPartDriver<CommentsPart> {
|
||||
private readonly ICommentService _commentService;
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
|
||||
public CommentsPartDriver(
|
||||
ICommentService commentService,
|
||||
IContentManager contentManager) {
|
||||
IContentManager contentManager,
|
||||
IWorkContextAccessor workContextAccessor) {
|
||||
_commentService = commentService;
|
||||
_contentManager = contentManager;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
}
|
||||
|
||||
protected override DriverResult Display(CommentsPart part, string displayType, dynamic shapeHelper) {
|
||||
@@ -58,6 +61,7 @@ namespace Orchard.Comments.Drivers {
|
||||
}),
|
||||
ContentShape("Parts_CommentForm",
|
||||
() => {
|
||||
|
||||
var newComment = _contentManager.New("Comment");
|
||||
if (newComment.Has<CommentPart>()) newComment.As<CommentPart>().CommentedOn = part.Id;
|
||||
var editorShape = _contentManager.BuildEditor(newComment);
|
||||
|
@@ -1,5 +1,6 @@
|
||||
namespace Orchard.Comments.Settings {
|
||||
public class CommentsPartSettings {
|
||||
public bool DefaultThreadedComments { get; set; }
|
||||
public bool MustBeAuthenticated { get; set; }
|
||||
}
|
||||
}
|
@@ -30,6 +30,7 @@ namespace Orchard.Comments.Settings {
|
||||
|
||||
if (updateModel.TryUpdateModel(settings, "CommentsPartSettings", null, null)) {
|
||||
builder.WithSetting("CommentsPartSettings.DefaultThreadedComments", settings.DefaultThreadedComments.ToString(CultureInfo.InvariantCulture));
|
||||
builder.WithSetting("CommentsPartSettings.MustBeAuthenticated", settings.MustBeAuthenticated.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
yield return DefinitionTemplate(settings);
|
||||
|
@@ -5,5 +5,9 @@
|
||||
@Html.EditorFor(m => m.DefaultThreadedComments)
|
||||
<label class="forcheckbox" for="@Html.FieldIdFor( m => m.DefaultThreadedComments)">@T("Use threaded comments by default")</label>
|
||||
</div>
|
||||
<div>
|
||||
@Html.EditorFor(m => m.MustBeAuthenticated)
|
||||
<label class="forcheckbox" for="@Html.FieldIdFor( m => m.MustBeAuthenticated)">@T("Users must be authenticated to comment")</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
|
@@ -1,6 +1,13 @@
|
||||
@using Orchard.Comments;
|
||||
@using Orchard.Comments.Models
|
||||
@using Orchard.Comments.Settings
|
||||
@using Orchard.Utility.Extensions;
|
||||
|
||||
@{
|
||||
CommentsPart commentsPart = Model.ContentPart;
|
||||
var settings = commentsPart.TypePartDefinition.Settings.GetModel<CommentsPartSettings>();
|
||||
}
|
||||
|
||||
@if (!Model.ContentPart.CommentsActive) {
|
||||
if (Model.ContentPart.Comments.Count > 0) {
|
||||
<div id="comments">
|
||||
@@ -8,6 +15,11 @@
|
||||
</div>
|
||||
}
|
||||
}
|
||||
else if (settings.MustBeAuthenticated && WorkContext.CurrentUser == null) {
|
||||
<div id="comments">
|
||||
<p class="comment-disabled">@T("You must be authenticated in order to add a comment.")</p>
|
||||
</div>
|
||||
}
|
||||
else if (WorkContext.CurrentUser == null && !AuthorizedFor(Permissions.AddComment)) {
|
||||
<h2 id="add-comment">@T("Add a Comment")</h2>
|
||||
<p class="info message">@T("You must {0} to comment.", Html.ActionLink(T("log on").ToString(), "LogOn",
|
||||
|
Reference in New Issue
Block a user