mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adding some auth checks in content CRUD (from Orchard.Pages)
--HG-- branch : dev
This commit is contained in:
@@ -20,7 +20,7 @@ namespace Orchard.Core.Common {
|
|||||||
new PermissionStereotype {
|
new PermissionStereotype {
|
||||||
Name = "Administrator",
|
Name = "Administrator",
|
||||||
Permissions = new[] {ChangeOwner}
|
Permissions = new[] {ChangeOwner}
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using JetBrains.Annotations;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
|
using Orchard.ContentManagement.Aspects;
|
||||||
|
using Orchard.Core.Common.Models;
|
||||||
|
using Orchard.Security;
|
||||||
|
using Orchard.Security.Permissions;
|
||||||
|
|
||||||
|
namespace Orchard.Core.Common.Security
|
||||||
|
{
|
||||||
|
[UsedImplicitly]
|
||||||
|
public class AuthorizationEventHandler : IAuthorizationServiceEventHandler
|
||||||
|
{
|
||||||
|
public void Checking(CheckAccessContext context) { }
|
||||||
|
public void Complete(CheckAccessContext context) { }
|
||||||
|
|
||||||
|
public void Adjust(CheckAccessContext context)
|
||||||
|
{
|
||||||
|
if (!context.Granted &&
|
||||||
|
context.Content.Is<CommonPart>() &&
|
||||||
|
OwnerVariationExists(context.Permission) &&
|
||||||
|
HasOwnership(context.User, context.Content))
|
||||||
|
{
|
||||||
|
|
||||||
|
context.Adjusted = true;
|
||||||
|
context.Permission = GetOwnerVariation(context.Permission);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool HasOwnership(IUser user, IContent content)
|
||||||
|
{
|
||||||
|
if (user == null || content == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var common = content.As<ICommonPart>();
|
||||||
|
if (common == null || common.Owner == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return user.Id == common.Owner.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool OwnerVariationExists(Permission permission)
|
||||||
|
{
|
||||||
|
return GetOwnerVariation(permission) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Permission GetOwnerVariation(Permission permission)
|
||||||
|
{
|
||||||
|
if (permission.Name == Contents.Permissions.PublishOthersContent.Name)
|
||||||
|
return Contents.Permissions.PublishContent;
|
||||||
|
if (permission.Name == Contents.Permissions.EditOthersContent.Name)
|
||||||
|
return Contents.Permissions.EditContent;
|
||||||
|
if (permission.Name == Contents.Permissions.DeleteOthersContent.Name)
|
||||||
|
return Contents.Permissions.DeleteContent;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -134,34 +134,38 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
[HttpPost, ActionName("List")]
|
[HttpPost, ActionName("List")]
|
||||||
[FormValueRequired("submit.BulkEdit")]
|
[FormValueRequired("submit.BulkEdit")]
|
||||||
public ActionResult ListPOST(ContentOptions options, IEnumerable<int> itemIds, string returnUrl) {
|
public ActionResult ListPOST(ContentOptions options, IEnumerable<int> itemIds, string returnUrl) {
|
||||||
|
var accessChecked = false;
|
||||||
switch (options.BulkAction) {
|
switch (options.BulkAction) {
|
||||||
case ContentsBulkAction.None:
|
case ContentsBulkAction.None:
|
||||||
break;
|
break;
|
||||||
case ContentsBulkAction.PublishNow:
|
case ContentsBulkAction.PublishNow:
|
||||||
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't publish selected content.")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
|
|
||||||
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
||||||
|
if (!accessChecked && !Services.Authorizer.Authorize(Permissions.PublishContent, item, T("Couldn't publish selected content.")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
|
accessChecked = true;
|
||||||
_contentManager.Publish(item);
|
_contentManager.Publish(item);
|
||||||
Services.ContentManager.Flush();
|
Services.ContentManager.Flush();
|
||||||
}
|
}
|
||||||
Services.Notifier.Information(T("Content successfully published."));
|
Services.Notifier.Information(T("Content successfully published."));
|
||||||
break;
|
break;
|
||||||
case ContentsBulkAction.Unpublish:
|
case ContentsBulkAction.Unpublish:
|
||||||
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't unpublish selected content.")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
|
|
||||||
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
||||||
|
if (!accessChecked && !Services.Authorizer.Authorize(Permissions.PublishContent, item, T("Couldn't unpublish selected content.")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
|
accessChecked = true;
|
||||||
_contentManager.Unpublish(item);
|
_contentManager.Unpublish(item);
|
||||||
Services.ContentManager.Flush();
|
Services.ContentManager.Flush();
|
||||||
}
|
}
|
||||||
Services.Notifier.Information(T("Content successfully unpublished."));
|
Services.Notifier.Information(T("Content successfully unpublished."));
|
||||||
break;
|
break;
|
||||||
case ContentsBulkAction.Remove:
|
case ContentsBulkAction.Remove:
|
||||||
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't delete selected content.")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
|
|
||||||
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
||||||
|
if (!accessChecked && !Services.Authorizer.Authorize(Permissions.DeleteContent, item, T("Couldn't remove selected content.")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
|
accessChecked = true;
|
||||||
_contentManager.Remove(item);
|
_contentManager.Remove(item);
|
||||||
Services.ContentManager.Flush();
|
Services.ContentManager.Flush();
|
||||||
}
|
}
|
||||||
@@ -210,6 +214,10 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
return CreatableTypeList();
|
return CreatableTypeList();
|
||||||
|
|
||||||
var contentItem = _contentManager.New(id);
|
var contentItem = _contentManager.New(id);
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, contentItem, T("Cannot create content")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
var model = new CreateItemViewModel {
|
var model = new CreateItemViewModel {
|
||||||
Id = id,
|
Id = id,
|
||||||
Content = _contentManager.BuildEditorModel(contentItem)
|
Content = _contentManager.BuildEditorModel(contentItem)
|
||||||
@@ -221,8 +229,11 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Create(CreateItemViewModel model) {
|
public ActionResult Create(CreateItemViewModel model) {
|
||||||
//todo: need to integrate permissions into generic content management
|
|
||||||
var contentItem = _contentManager.New(model.Id);
|
var contentItem = _contentManager.New(model.Id);
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, contentItem, T("Couldn't create content")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
_contentManager.Create(contentItem, VersionOptions.Draft);
|
_contentManager.Create(contentItem, VersionOptions.Draft);
|
||||||
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
||||||
|
|
||||||
@@ -246,6 +257,9 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
if (contentItem == null)
|
if (contentItem == null)
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.EditOthersContent, contentItem, T("Cannot edit content")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
var model = new EditItemViewModel {
|
var model = new EditItemViewModel {
|
||||||
Id = id,
|
Id = id,
|
||||||
Content = _contentManager.BuildEditorModel(contentItem)
|
Content = _contentManager.BuildEditorModel(contentItem)
|
||||||
@@ -263,6 +277,9 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
if (contentItem == null)
|
if (contentItem == null)
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.EditOthersContent, contentItem, T("Couldn't edit content")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
||||||
if (!ModelState.IsValid) {
|
if (!ModelState.IsValid) {
|
||||||
_transactionManager.Cancel();
|
_transactionManager.Cancel();
|
||||||
@@ -280,6 +297,10 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
[HttpPost, ActionName("Remove")]
|
[HttpPost, ActionName("Remove")]
|
||||||
public ActionResult RemovePOST(int id, string returnUrl) {
|
public ActionResult RemovePOST(int id, string returnUrl) {
|
||||||
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
|
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.DeleteOthersContent, contentItem, T("Couldn't remove content")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
if (contentItem != null)
|
if (contentItem != null)
|
||||||
_contentManager.Remove(contentItem);
|
_contentManager.Remove(contentItem);
|
||||||
|
|
||||||
@@ -291,13 +312,13 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Publish(int id, string returnUrl) {
|
public ActionResult Publish(int id, string returnUrl) {
|
||||||
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't publish content")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
|
|
||||||
var contentItem = _contentManager.GetLatest(id);
|
var contentItem = _contentManager.GetLatest(id);
|
||||||
if (contentItem == null)
|
if (contentItem == null)
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, contentItem, T("Couldn't publish content")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
_contentManager.Publish(contentItem);
|
_contentManager.Publish(contentItem);
|
||||||
Services.ContentManager.Flush();
|
Services.ContentManager.Flush();
|
||||||
Services.Notifier.Information(T("{0} successfully published.", contentItem.TypeDefinition.DisplayName));
|
Services.Notifier.Information(T("{0} successfully published.", contentItem.TypeDefinition.DisplayName));
|
||||||
@@ -310,13 +331,13 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Unpublish(int id, string returnUrl) {
|
public ActionResult Unpublish(int id, string returnUrl) {
|
||||||
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't unpublish content")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
|
|
||||||
var contentItem = _contentManager.GetLatest(id);
|
var contentItem = _contentManager.GetLatest(id);
|
||||||
if (contentItem == null)
|
if (contentItem == null)
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
|
|
||||||
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, contentItem, T("Couldn't unpublish content")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
_contentManager.Unpublish(contentItem);
|
_contentManager.Unpublish(contentItem);
|
||||||
Services.ContentManager.Flush();
|
Services.ContentManager.Flush();
|
||||||
Services.Notifier.Information(T("{0} successfully unpublished.", contentItem.TypeDefinition.DisplayName));
|
Services.Notifier.Information(T("{0} successfully unpublished.", contentItem.TypeDefinition.DisplayName));
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace Orchard.Core.Contents {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Permission> GetPermissions() {
|
public IEnumerable<Permission> GetPermissions() {
|
||||||
return new Permission[] {
|
return new [] {
|
||||||
EditContent,
|
EditContent,
|
||||||
EditOthersContent,
|
EditOthersContent,
|
||||||
PublishContent,
|
PublishContent,
|
||||||
|
|||||||
@@ -67,6 +67,7 @@
|
|||||||
<Compile Include="Common\Drivers\TextFieldDriver.cs" />
|
<Compile Include="Common\Drivers\TextFieldDriver.cs" />
|
||||||
<Compile Include="Common\Extensions\HtmlHelperExtensions.cs" />
|
<Compile Include="Common\Extensions\HtmlHelperExtensions.cs" />
|
||||||
<Compile Include="Common\Fields\TextField.cs" />
|
<Compile Include="Common\Fields\TextField.cs" />
|
||||||
|
<Compile Include="Common\Security\AuthorizationEventHandler.cs" />
|
||||||
<Compile Include="ContentsLocation\Models\LocationDefinition.cs" />
|
<Compile Include="ContentsLocation\Models\LocationDefinition.cs" />
|
||||||
<Compile Include="Common\Services\ICommonService.cs" />
|
<Compile Include="Common\Services\ICommonService.cs" />
|
||||||
<Compile Include="Common\Services\CommonService.cs" />
|
<Compile Include="Common\Services\CommonService.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user