mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
- Fixing some broken unit tests (due to Localizer/Authorize changes).
- Refactoring Media module (Authorize,Localize). - Minor cleanup... --HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4040469
This commit is contained in:
@@ -11,6 +11,7 @@ using Orchard.CmsPages.Services;
|
|||||||
using Orchard.CmsPages.Services.Templates;
|
using Orchard.CmsPages.Services.Templates;
|
||||||
using Orchard.CmsPages.ViewModels;
|
using Orchard.CmsPages.ViewModels;
|
||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
|
using Orchard.Localization;
|
||||||
using Orchard.Security;
|
using Orchard.Security;
|
||||||
using Orchard.Security.Permissions;
|
using Orchard.Security.Permissions;
|
||||||
using Orchard.Tests.Stubs;
|
using Orchard.Tests.Stubs;
|
||||||
@@ -23,7 +24,7 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
|||||||
private AdminController _controller;
|
private AdminController _controller;
|
||||||
private IPageManager _pageManager;
|
private IPageManager _pageManager;
|
||||||
private IPageScheduler _pageScheduler;
|
private IPageScheduler _pageScheduler;
|
||||||
private IAuthorizationService _authorizationService;
|
private IAuthorizer _authorizer;
|
||||||
private ITemplateProvider _templateProvider;
|
private ITemplateProvider _templateProvider;
|
||||||
private int _slugPageId;
|
private int _slugPageId;
|
||||||
private IRepository<Page> _pagesRepository;
|
private IRepository<Page> _pagesRepository;
|
||||||
@@ -37,12 +38,13 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
|||||||
_pageManager = _container.Resolve<IPageManager>();
|
_pageManager = _container.Resolve<IPageManager>();
|
||||||
_pageScheduler = _container.Resolve<IPageScheduler>();
|
_pageScheduler = _container.Resolve<IPageScheduler>();
|
||||||
_templateProvider = _container.Resolve<ITemplateProvider>();
|
_templateProvider = _container.Resolve<ITemplateProvider>();
|
||||||
_authorizationService = _container.Resolve<IAuthorizationService>();
|
_authorizer = _container.Resolve<IAuthorizer>();
|
||||||
var page = _pageManager.CreatePage(new PageCreateViewModel { Slug = "slug", Templates = _templateProvider.List() });
|
var page = _pageManager.CreatePage(new PageCreateViewModel { Slug = "slug", Templates = _templateProvider.List() });
|
||||||
_slugPageId = page.Id;
|
_slugPageId = page.Id;
|
||||||
|
|
||||||
_controller = _container.Resolve<AdminController>();
|
_controller = _container.Resolve<AdminController>();
|
||||||
_controller.ControllerContext = new ControllerContext(new StubHttpContext("~/admin/cmspages"), new RouteData(), _controller);
|
_controller.ControllerContext = new ControllerContext(new StubHttpContext("~/admin/cmspages"), new RouteData(), _controller);
|
||||||
|
_controller.T = new Localizer(StubLocalizer.Get);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Register(Autofac.Builder.ContainerBuilder builder) {
|
public override void Register(Autofac.Builder.ContainerBuilder builder) {
|
||||||
@@ -51,7 +53,7 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
|||||||
builder.Register<PageScheduler>().As<IPageScheduler>();
|
builder.Register<PageScheduler>().As<IPageScheduler>();
|
||||||
builder.Register<Notifier>().As<INotifier>();
|
builder.Register<Notifier>().As<INotifier>();
|
||||||
builder.Register(new StubTemplateProvider()).As<ITemplateProvider>();
|
builder.Register(new StubTemplateProvider()).As<ITemplateProvider>();
|
||||||
builder.Register(new StubAuthorizationService()).As<IAuthorizationService>();
|
builder.Register(new StubAuthorizer()).As<IAuthorizer>();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override IEnumerable<Type> DatabaseTypes {
|
protected override IEnumerable<Type> DatabaseTypes {
|
||||||
@@ -82,16 +84,24 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StubAuthorizationService : IAuthorizationService {
|
class StubAuthorizer: IAuthorizer {
|
||||||
#region Implementation of IAuthorizationService
|
#region IAuthorizer Members
|
||||||
|
|
||||||
public bool CheckAccess(IUser user, Permission permission) {
|
public bool Authorize(Permission permission, LocalizedString message) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class StubLocalizer {
|
||||||
|
public static LocalizedString Get(string textHint, params object[] args) {
|
||||||
|
var localizedFormat = textHint;
|
||||||
|
var localizedText = string.Format(localizedFormat, args);
|
||||||
|
return new LocalizedString(localizedText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void CreateShouldReturnViewWithErrorIfSlugIsNull() {
|
public void CreateShouldReturnViewWithErrorIfSlugIsNull() {
|
||||||
var input = new FormCollection { { ReflectOn<PageCreateViewModel>.NameOf(m => m.Slug), null } };
|
var input = new FormCollection { { ReflectOn<PageCreateViewModel>.NameOf(m => m.Slug), null } };
|
||||||
@@ -315,7 +325,7 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
|||||||
|
|
||||||
// Verify result, check database state
|
// Verify result, check database state
|
||||||
ClearSession();
|
ClearSession();
|
||||||
pages = _pagesRepository.Table.ToList();
|
_pagesRepository.Table.ToList();
|
||||||
Assert.That(result, Is.InstanceOf<RedirectToRouteResult>());
|
Assert.That(result, Is.InstanceOf<RedirectToRouteResult>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,12 @@ namespace Orchard.Tests.UI.Notify {
|
|||||||
notifier.Error("Boom");
|
notifier.Error("Boom");
|
||||||
|
|
||||||
Assert.That(notifier.List(), Has.Count.EqualTo(3));
|
Assert.That(notifier.List(), Has.Count.EqualTo(3));
|
||||||
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo("Hello world"));
|
foreach (var notifyEntries in notifier.List()) {
|
||||||
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo("More Info"));
|
Assert.That(new[] {notifyEntries.Message.ToString()}, Is.SubsetOf(new[]
|
||||||
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo("Boom"));
|
{
|
||||||
|
"Hello world", "More Info", "Boom"
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,8 +75,12 @@ namespace Orchard.Tests.UI.Notify {
|
|||||||
|
|
||||||
Assert.That(model.Messages, Is.Not.Null);
|
Assert.That(model.Messages, Is.Not.Null);
|
||||||
Assert.That(model.Messages, Has.Count.EqualTo(2));
|
Assert.That(model.Messages, Has.Count.EqualTo(2));
|
||||||
Assert.That(model.Messages, Has.Some.Property("Message").EqualTo("dont-destroy"));
|
foreach (var notifyEntries in model.Messages) {
|
||||||
Assert.That(model.Messages, Has.Some.Property("Message").EqualTo("Working"));
|
Assert.That(new[] { notifyEntries.Message.ToString() }, Is.SubsetOf(new[]
|
||||||
|
{
|
||||||
|
"dont-destroy", "Working"
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,7 +127,7 @@ namespace Orchard.CmsPages.Controllers {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PageIndexBulkAction.Unpublish:
|
case PageIndexBulkAction.Unpublish:
|
||||||
if (!_authorizer.Authorize(Permissions.UnpublishPages, T("Couldn't publish page")))
|
if (!_authorizer.Authorize(Permissions.UnpublishPages, T("Couldn't unpublish page")))
|
||||||
return new HttpUnauthorizedResult();
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
foreach (PageEntry entry in checkedEntries) {
|
foreach (PageEntry entry in checkedEntries) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
|
using Orchard.Localization;
|
||||||
using Orchard.Media.Models;
|
using Orchard.Media.Models;
|
||||||
using Orchard.Media.Services;
|
using Orchard.Media.Services;
|
||||||
using Orchard.Media.ViewModels;
|
using Orchard.Media.ViewModels;
|
||||||
@@ -12,12 +13,12 @@ namespace Orchard.Media.Controllers {
|
|||||||
[ValidateInput(false)]
|
[ValidateInput(false)]
|
||||||
public class AdminController : Controller {
|
public class AdminController : Controller {
|
||||||
private readonly IMediaService _mediaService;
|
private readonly IMediaService _mediaService;
|
||||||
private readonly IAuthorizationService _authorizationService;
|
private readonly IAuthorizer _authorizer;
|
||||||
private readonly INotifier _notifier;
|
private readonly INotifier _notifier;
|
||||||
|
|
||||||
public AdminController(IMediaService mediaService, IAuthorizationService authorizationService, INotifier notifier) {
|
public AdminController(IMediaService mediaService, IAuthorizer authorizer, INotifier notifier) {
|
||||||
_mediaService = mediaService;
|
_mediaService = mediaService;
|
||||||
_authorizationService = authorizationService;
|
_authorizer = authorizer;
|
||||||
_notifier = notifier;
|
_notifier = notifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +26,8 @@ namespace Orchard.Media.Controllers {
|
|||||||
// See Orchard.Security.SecurityModule.
|
// See Orchard.Security.SecurityModule.
|
||||||
public IUser CurrentUser { get; set; }
|
public IUser CurrentUser { get; set; }
|
||||||
|
|
||||||
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
public ActionResult Index() {
|
public ActionResult Index() {
|
||||||
// Root media folders
|
// Root media folders
|
||||||
IEnumerable<MediaFolder> mediaFolders = _mediaService.GetMediaFolders(null);
|
IEnumerable<MediaFolder> mediaFolders = _mediaService.GetMediaFolders(null);
|
||||||
@@ -58,13 +61,8 @@ namespace Orchard.Media.Controllers {
|
|||||||
var viewModel = new MediaFolderCreateViewModel();
|
var viewModel = new MediaFolderCreateViewModel();
|
||||||
try {
|
try {
|
||||||
UpdateModel(viewModel, input.ToValueProvider());
|
UpdateModel(viewModel, input.ToValueProvider());
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.CreateMediaFolder)) {
|
if (!_authorizer.Authorize(Permissions.CreateMediaFolder, T("Couldn't create media folder")))
|
||||||
_notifier.Error("Couldn't create media folder, user " +
|
return new HttpUnauthorizedResult();
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.CreateMediaFolder.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return View(viewModel);
|
|
||||||
}
|
|
||||||
_mediaService.CreateFolder(viewModel.MediaPath, viewModel.Name);
|
_mediaService.CreateFolder(viewModel.MediaPath, viewModel.Name);
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
@@ -88,25 +86,15 @@ namespace Orchard.Media.Controllers {
|
|||||||
if (key.StartsWith("Checkbox.File.") && input[key] == "true") {
|
if (key.StartsWith("Checkbox.File.") && input[key] == "true") {
|
||||||
string fileName = key.Substring("Checkbox.File.".Length);
|
string fileName = key.Substring("Checkbox.File.".Length);
|
||||||
string folderName = input[fileName];
|
string folderName = input[fileName];
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMedia)) {
|
if (!_authorizer.Authorize(Permissions.DeleteMedia, T("Couldn't delete media file")))
|
||||||
_notifier.Error("Couldn't delete media file, user " +
|
return new HttpUnauthorizedResult();
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.DeleteMedia.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
_mediaService.DeleteFile(fileName, folderName);
|
_mediaService.DeleteFile(fileName, folderName);
|
||||||
}
|
}
|
||||||
else if (key.StartsWith("Checkbox.Folder.") && input[key] == "true") {
|
else if (key.StartsWith("Checkbox.Folder.") && input[key] == "true") {
|
||||||
string folderName = key.Substring("Checkbox.Folder.".Length);
|
string folderName = key.Substring("Checkbox.Folder.".Length);
|
||||||
string folderPath = input[folderName];
|
string folderPath = input[folderName];
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMediaFolder)) {
|
if (!_authorizer.Authorize(Permissions.DeleteMediaFolder, T("Couldn't delete media folder")))
|
||||||
_notifier.Error("Couldn't delete media folder, user " +
|
return new HttpUnauthorizedResult();
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.DeleteMediaFolder.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return RedirectToAction("Index");
|
|
||||||
}
|
|
||||||
_mediaService.DeleteFolder(folderPath);
|
_mediaService.DeleteFolder(folderPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,24 +119,14 @@ namespace Orchard.Media.Controllers {
|
|||||||
//TODO: There may be better ways to do this.
|
//TODO: There may be better ways to do this.
|
||||||
// Delete
|
// Delete
|
||||||
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
|
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMediaFolder)) {
|
if (!_authorizer.Authorize(Permissions.DeleteMediaFolder, T("Couldn't delete media folder")))
|
||||||
_notifier.Error("Couldn't delete media folder, user " +
|
return new HttpUnauthorizedResult();
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.DeleteMediaFolder.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return View(viewModel);
|
|
||||||
}
|
|
||||||
_mediaService.DeleteFolder(viewModel.MediaPath);
|
_mediaService.DeleteFolder(viewModel.MediaPath);
|
||||||
}
|
}
|
||||||
// Save
|
// Save
|
||||||
else {
|
else {
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.RenameMediaFolder)) {
|
if (!_authorizer.Authorize(Permissions.RenameMediaFolder, T("Couldn't rename media folder")))
|
||||||
_notifier.Error("Couldn't rename media folder, user " +
|
return new HttpUnauthorizedResult();
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.RenameMediaFolder.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return View(viewModel);
|
|
||||||
}
|
|
||||||
_mediaService.RenameFolder(viewModel.MediaPath, viewModel.Name);
|
_mediaService.RenameFolder(viewModel.MediaPath, viewModel.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,14 +148,8 @@ namespace Orchard.Media.Controllers {
|
|||||||
var viewModel = new MediaItemAddViewModel();
|
var viewModel = new MediaItemAddViewModel();
|
||||||
try {
|
try {
|
||||||
UpdateModel(viewModel, input.ToValueProvider());
|
UpdateModel(viewModel, input.ToValueProvider());
|
||||||
|
if (!_authorizer.Authorize(Permissions.UploadMedia, T("Couldn't upload media file")))
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.UploadMedia)) {
|
return new HttpUnauthorizedResult();
|
||||||
_notifier.Error("Couldn't upload media file, user " +
|
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.UploadMedia.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return View(viewModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (string fileName in Request.Files) {
|
foreach (string fileName in Request.Files) {
|
||||||
HttpPostedFileBase file = Request.Files[fileName];
|
HttpPostedFileBase file = Request.Files[fileName];
|
||||||
@@ -208,23 +180,12 @@ namespace Orchard.Media.Controllers {
|
|||||||
var viewModel = new MediaItemEditViewModel();
|
var viewModel = new MediaItemEditViewModel();
|
||||||
try {
|
try {
|
||||||
UpdateModel(viewModel, input.ToValueProvider());
|
UpdateModel(viewModel, input.ToValueProvider());
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.ModifyMedia)) {
|
if (!_authorizer.Authorize(Permissions.ModifyMedia, T("Couldn't modify media file")))
|
||||||
_notifier.Error("Couldn't modify media file, user " +
|
return new HttpUnauthorizedResult();
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.ModifyMedia.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return View(viewModel);
|
|
||||||
}
|
|
||||||
//TODO: There may be better ways to do this.
|
|
||||||
// Delete
|
// Delete
|
||||||
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
|
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
|
||||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMedia)) {
|
if (!_authorizer.Authorize(Permissions.DeleteMedia, T("Couldn't delete media file")))
|
||||||
_notifier.Error("Couldn't delete media file, user " +
|
return new HttpUnauthorizedResult();
|
||||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
|
||||||
Permissions.DeleteMedia.Name);
|
|
||||||
//return new HttpUnauthorizedResult();
|
|
||||||
return View(viewModel);
|
|
||||||
}
|
|
||||||
_mediaService.DeleteFile(viewModel.Name, viewModel.MediaPath);
|
_mediaService.DeleteFile(viewModel.Name, viewModel.MediaPath);
|
||||||
return RedirectToAction("Edit", new { name = viewModel.FolderName, mediaPath = viewModel.MediaPath });
|
return RedirectToAction("Edit", new { name = viewModel.FolderName, mediaPath = viewModel.MediaPath });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
<head id="Head1" runat="server">
|
<head id="Head1" runat="server">
|
||||||
<title>Create Media Folder</title>
|
<title>Edit Media File</title>
|
||||||
<% Html.Include("Head"); %>
|
<% Html.Include("Head"); %>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
using System;
|
using Orchard.Localization;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Web.Mvc;
|
|
||||||
using Orchard.Localization;
|
|
||||||
using Orchard.Security.Permissions;
|
using Orchard.Security.Permissions;
|
||||||
using Orchard.UI.Notify;
|
using Orchard.UI.Notify;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
using System.Web;
|
namespace Orchard.Security {
|
||||||
|
|
||||||
namespace Orchard.Security {
|
|
||||||
public interface IAuthenticationService : IDependency {
|
public interface IAuthenticationService : IDependency {
|
||||||
void SignIn(IUser user, bool createPersistentCookie);
|
void SignIn(IUser user, bool createPersistentCookie);
|
||||||
void SignOut();
|
void SignOut();
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
using System;
|
using System.Web.Security;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Web.Security;
|
|
||||||
|
|
||||||
namespace Orchard.Security {
|
namespace Orchard.Security {
|
||||||
public interface IMembershipService : IDependency {
|
public interface IMembershipService : IDependency {
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Security;
|
using System.Web.Security;
|
||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
|
|||||||
Reference in New Issue
Block a user