Adding support for restricting comments to authenticated users only

--HG--
branch : 1.x
extra : rebase_source : a8b7abba754a5dc70eb301b308f6b9becbd66a95
This commit is contained in:
Sebastien Ros
2012-11-02 15:05:51 -07:00
parent cc4e654538
commit 2cd444ceb7
6 changed files with 31 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
using System.Web.Mvc; using System.Web.Mvc;
using Orchard.Comments.Models; using Orchard.Comments.Models;
using Orchard.Comments.Services; using Orchard.Comments.Services;
using Orchard.Comments.Settings;
using Orchard.Comments.ViewModels; using Orchard.Comments.ViewModels;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.Localization; using Orchard.Localization;
@@ -43,9 +44,13 @@ namespace Orchard.Comments.Controllers {
CommentsPart commentsPart = null; CommentsPart commentsPart = null;
if(container != null) { if(container != null) {
commentsPart = container.As<CommentsPart>(); commentsPart = container.As<CommentsPart>();
if(commentsPart != null && !commentsPart.CommentsActive) { if (commentsPart != null) {
Services.TransactionManager.Cancel(); var settings = commentsPart.TypePartDefinition.Settings.GetModel<CommentsPartSettings>();
return this.RedirectLocal(returnUrl, "~/"); if (!commentsPart.CommentsActive
|| (settings.MustBeAuthenticated && Services.WorkContext.CurrentUser == null)) {
Services.TransactionManager.Cancel();
return this.RedirectLocal(returnUrl, "~/");
}
} }
} }

View File

@@ -12,12 +12,15 @@ namespace Orchard.Comments.Drivers {
public class CommentsPartDriver : ContentPartDriver<CommentsPart> { public class CommentsPartDriver : ContentPartDriver<CommentsPart> {
private readonly ICommentService _commentService; private readonly ICommentService _commentService;
private readonly IContentManager _contentManager; private readonly IContentManager _contentManager;
private readonly IWorkContextAccessor _workContextAccessor;
public CommentsPartDriver( public CommentsPartDriver(
ICommentService commentService, ICommentService commentService,
IContentManager contentManager) { IContentManager contentManager,
IWorkContextAccessor workContextAccessor) {
_commentService = commentService; _commentService = commentService;
_contentManager = contentManager; _contentManager = contentManager;
_workContextAccessor = workContextAccessor;
} }
protected override DriverResult Display(CommentsPart part, string displayType, dynamic shapeHelper) { protected override DriverResult Display(CommentsPart part, string displayType, dynamic shapeHelper) {
@@ -58,6 +61,7 @@ namespace Orchard.Comments.Drivers {
}), }),
ContentShape("Parts_CommentForm", ContentShape("Parts_CommentForm",
() => { () => {
var newComment = _contentManager.New("Comment"); var newComment = _contentManager.New("Comment");
if (newComment.Has<CommentPart>()) newComment.As<CommentPart>().CommentedOn = part.Id; if (newComment.Has<CommentPart>()) newComment.As<CommentPart>().CommentedOn = part.Id;
var editorShape = _contentManager.BuildEditor(newComment); var editorShape = _contentManager.BuildEditor(newComment);

View File

@@ -1,5 +1,6 @@
namespace Orchard.Comments.Settings { namespace Orchard.Comments.Settings {
public class CommentsPartSettings { public class CommentsPartSettings {
public bool DefaultThreadedComments { get; set; } public bool DefaultThreadedComments { get; set; }
public bool MustBeAuthenticated { get; set; }
} }
} }

View File

@@ -30,6 +30,7 @@ namespace Orchard.Comments.Settings {
if (updateModel.TryUpdateModel(settings, "CommentsPartSettings", null, null)) { if (updateModel.TryUpdateModel(settings, "CommentsPartSettings", null, null)) {
builder.WithSetting("CommentsPartSettings.DefaultThreadedComments", settings.DefaultThreadedComments.ToString(CultureInfo.InvariantCulture)); builder.WithSetting("CommentsPartSettings.DefaultThreadedComments", settings.DefaultThreadedComments.ToString(CultureInfo.InvariantCulture));
builder.WithSetting("CommentsPartSettings.MustBeAuthenticated", settings.MustBeAuthenticated.ToString(CultureInfo.InvariantCulture));
} }
yield return DefinitionTemplate(settings); yield return DefinitionTemplate(settings);

View File

@@ -5,5 +5,9 @@
@Html.EditorFor(m => m.DefaultThreadedComments) @Html.EditorFor(m => m.DefaultThreadedComments)
<label class="forcheckbox" for="@Html.FieldIdFor( m => m.DefaultThreadedComments)">@T("Use threaded comments by default")</label> <label class="forcheckbox" for="@Html.FieldIdFor( m => m.DefaultThreadedComments)">@T("Use threaded comments by default")</label>
</div> </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> </fieldset>

View File

@@ -1,6 +1,13 @@
@using Orchard.Comments; @using Orchard.Comments;
@using Orchard.Comments.Models
@using Orchard.Comments.Settings
@using Orchard.Utility.Extensions; @using Orchard.Utility.Extensions;
@{
CommentsPart commentsPart = Model.ContentPart;
var settings = commentsPart.TypePartDefinition.Settings.GetModel<CommentsPartSettings>();
}
@if (!Model.ContentPart.CommentsActive) { @if (!Model.ContentPart.CommentsActive) {
if (Model.ContentPart.Comments.Count > 0) { if (Model.ContentPart.Comments.Count > 0) {
<div id="comments"> <div id="comments">
@@ -8,6 +15,11 @@
</div> </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)) { else if (WorkContext.CurrentUser == null && !AuthorizedFor(Permissions.AddComment)) {
<h2 id="add-comment">@T("Add a Comment")</h2> <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", <p class="info message">@T("You must {0} to comment.", Html.ActionLink(T("log on").ToString(), "LogOn",