mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-19 01:57:55 +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.ViewModels;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Security;
|
||||
using Orchard.Security.Permissions;
|
||||
using Orchard.Tests.Stubs;
|
||||
@@ -23,7 +24,7 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
||||
private AdminController _controller;
|
||||
private IPageManager _pageManager;
|
||||
private IPageScheduler _pageScheduler;
|
||||
private IAuthorizationService _authorizationService;
|
||||
private IAuthorizer _authorizer;
|
||||
private ITemplateProvider _templateProvider;
|
||||
private int _slugPageId;
|
||||
private IRepository<Page> _pagesRepository;
|
||||
@@ -37,12 +38,13 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
||||
_pageManager = _container.Resolve<IPageManager>();
|
||||
_pageScheduler = _container.Resolve<IPageScheduler>();
|
||||
_templateProvider = _container.Resolve<ITemplateProvider>();
|
||||
_authorizationService = _container.Resolve<IAuthorizationService>();
|
||||
_authorizer = _container.Resolve<IAuthorizer>();
|
||||
var page = _pageManager.CreatePage(new PageCreateViewModel { Slug = "slug", Templates = _templateProvider.List() });
|
||||
_slugPageId = page.Id;
|
||||
|
||||
_controller = _container.Resolve<AdminController>();
|
||||
_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) {
|
||||
@@ -51,7 +53,7 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
||||
builder.Register<PageScheduler>().As<IPageScheduler>();
|
||||
builder.Register<Notifier>().As<INotifier>();
|
||||
builder.Register(new StubTemplateProvider()).As<ITemplateProvider>();
|
||||
builder.Register(new StubAuthorizationService()).As<IAuthorizationService>();
|
||||
builder.Register(new StubAuthorizer()).As<IAuthorizer>();
|
||||
}
|
||||
|
||||
protected override IEnumerable<Type> DatabaseTypes {
|
||||
@@ -82,16 +84,24 @@ namespace Orchard.Tests.Packages.Pages.Controllers {
|
||||
}
|
||||
}
|
||||
|
||||
class StubAuthorizationService : IAuthorizationService {
|
||||
#region Implementation of IAuthorizationService
|
||||
class StubAuthorizer: IAuthorizer {
|
||||
#region IAuthorizer Members
|
||||
|
||||
public bool CheckAccess(IUser user, Permission permission) {
|
||||
public bool Authorize(Permission permission, LocalizedString message) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#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]
|
||||
public void CreateShouldReturnViewWithErrorIfSlugIsNull() {
|
||||
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
|
||||
ClearSession();
|
||||
pages = _pagesRepository.Table.ToList();
|
||||
_pagesRepository.Table.ToList();
|
||||
Assert.That(result, Is.InstanceOf<RedirectToRouteResult>());
|
||||
}
|
||||
|
||||
|
@@ -13,9 +13,12 @@ namespace Orchard.Tests.UI.Notify {
|
||||
notifier.Error("Boom");
|
||||
|
||||
Assert.That(notifier.List(), Has.Count.EqualTo(3));
|
||||
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo("Hello world"));
|
||||
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo("More Info"));
|
||||
Assert.That(notifier.List(), Has.Some.Property("Message").EqualTo("Boom"));
|
||||
foreach (var notifyEntries in notifier.List()) {
|
||||
Assert.That(new[] {notifyEntries.Message.ToString()}, Is.SubsetOf(new[]
|
||||
{
|
||||
"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, Has.Count.EqualTo(2));
|
||||
Assert.That(model.Messages, Has.Some.Property("Message").EqualTo("dont-destroy"));
|
||||
Assert.That(model.Messages, Has.Some.Property("Message").EqualTo("Working"));
|
||||
foreach (var notifyEntries in model.Messages) {
|
||||
Assert.That(new[] { notifyEntries.Message.ToString() }, Is.SubsetOf(new[]
|
||||
{
|
||||
"dont-destroy", "Working"
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -127,7 +127,7 @@ namespace Orchard.CmsPages.Controllers {
|
||||
break;
|
||||
|
||||
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();
|
||||
|
||||
foreach (PageEntry entry in checkedEntries) {
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Media.Models;
|
||||
using Orchard.Media.Services;
|
||||
using Orchard.Media.ViewModels;
|
||||
@@ -12,12 +13,12 @@ namespace Orchard.Media.Controllers {
|
||||
[ValidateInput(false)]
|
||||
public class AdminController : Controller {
|
||||
private readonly IMediaService _mediaService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IAuthorizer _authorizer;
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public AdminController(IMediaService mediaService, IAuthorizationService authorizationService, INotifier notifier) {
|
||||
public AdminController(IMediaService mediaService, IAuthorizer authorizer, INotifier notifier) {
|
||||
_mediaService = mediaService;
|
||||
_authorizationService = authorizationService;
|
||||
_authorizer = authorizer;
|
||||
_notifier = notifier;
|
||||
}
|
||||
|
||||
@@ -25,6 +26,8 @@ namespace Orchard.Media.Controllers {
|
||||
// See Orchard.Security.SecurityModule.
|
||||
public IUser CurrentUser { get; set; }
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public ActionResult Index() {
|
||||
// Root media folders
|
||||
IEnumerable<MediaFolder> mediaFolders = _mediaService.GetMediaFolders(null);
|
||||
@@ -58,13 +61,8 @@ namespace Orchard.Media.Controllers {
|
||||
var viewModel = new MediaFolderCreateViewModel();
|
||||
try {
|
||||
UpdateModel(viewModel, input.ToValueProvider());
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.CreateMediaFolder)) {
|
||||
_notifier.Error("Couldn't create media folder, user " +
|
||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
||||
Permissions.CreateMediaFolder.Name);
|
||||
//return new HttpUnauthorizedResult();
|
||||
return View(viewModel);
|
||||
}
|
||||
if (!_authorizer.Authorize(Permissions.CreateMediaFolder, T("Couldn't create media folder")))
|
||||
return new HttpUnauthorizedResult();
|
||||
_mediaService.CreateFolder(viewModel.MediaPath, viewModel.Name);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
@@ -88,25 +86,15 @@ namespace Orchard.Media.Controllers {
|
||||
if (key.StartsWith("Checkbox.File.") && input[key] == "true") {
|
||||
string fileName = key.Substring("Checkbox.File.".Length);
|
||||
string folderName = input[fileName];
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMedia)) {
|
||||
_notifier.Error("Couldn't delete media file, user " +
|
||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
||||
Permissions.DeleteMedia.Name);
|
||||
//return new HttpUnauthorizedResult();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
if (!_authorizer.Authorize(Permissions.DeleteMedia, T("Couldn't delete media file")))
|
||||
return new HttpUnauthorizedResult();
|
||||
_mediaService.DeleteFile(fileName, folderName);
|
||||
}
|
||||
else if (key.StartsWith("Checkbox.Folder.") && input[key] == "true") {
|
||||
string folderName = key.Substring("Checkbox.Folder.".Length);
|
||||
string folderPath = input[folderName];
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMediaFolder)) {
|
||||
_notifier.Error("Couldn't delete media folder, user " +
|
||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
||||
Permissions.DeleteMediaFolder.Name);
|
||||
//return new HttpUnauthorizedResult();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
if (!_authorizer.Authorize(Permissions.DeleteMediaFolder, T("Couldn't delete media folder")))
|
||||
return new HttpUnauthorizedResult();
|
||||
_mediaService.DeleteFolder(folderPath);
|
||||
}
|
||||
}
|
||||
@@ -131,24 +119,14 @@ namespace Orchard.Media.Controllers {
|
||||
//TODO: There may be better ways to do this.
|
||||
// Delete
|
||||
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMediaFolder)) {
|
||||
_notifier.Error("Couldn't delete media folder, user " +
|
||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
||||
Permissions.DeleteMediaFolder.Name);
|
||||
//return new HttpUnauthorizedResult();
|
||||
return View(viewModel);
|
||||
}
|
||||
if (!_authorizer.Authorize(Permissions.DeleteMediaFolder, T("Couldn't delete media folder")))
|
||||
return new HttpUnauthorizedResult();
|
||||
_mediaService.DeleteFolder(viewModel.MediaPath);
|
||||
}
|
||||
// Save
|
||||
else {
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.RenameMediaFolder)) {
|
||||
_notifier.Error("Couldn't rename media folder, user " +
|
||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
||||
Permissions.RenameMediaFolder.Name);
|
||||
//return new HttpUnauthorizedResult();
|
||||
return View(viewModel);
|
||||
}
|
||||
if (!_authorizer.Authorize(Permissions.RenameMediaFolder, T("Couldn't rename media folder")))
|
||||
return new HttpUnauthorizedResult();
|
||||
_mediaService.RenameFolder(viewModel.MediaPath, viewModel.Name);
|
||||
}
|
||||
|
||||
@@ -170,14 +148,8 @@ namespace Orchard.Media.Controllers {
|
||||
var viewModel = new MediaItemAddViewModel();
|
||||
try {
|
||||
UpdateModel(viewModel, input.ToValueProvider());
|
||||
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.UploadMedia)) {
|
||||
_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);
|
||||
}
|
||||
if (!_authorizer.Authorize(Permissions.UploadMedia, T("Couldn't upload media file")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
foreach (string fileName in Request.Files) {
|
||||
HttpPostedFileBase file = Request.Files[fileName];
|
||||
@@ -208,23 +180,12 @@ namespace Orchard.Media.Controllers {
|
||||
var viewModel = new MediaItemEditViewModel();
|
||||
try {
|
||||
UpdateModel(viewModel, input.ToValueProvider());
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.ModifyMedia)) {
|
||||
_notifier.Error("Couldn't modify media file, user " +
|
||||
(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.
|
||||
if (!_authorizer.Authorize(Permissions.ModifyMedia, T("Couldn't modify media file")))
|
||||
return new HttpUnauthorizedResult();
|
||||
// Delete
|
||||
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
|
||||
if (!_authorizationService.CheckAccess(CurrentUser, Permissions.DeleteMedia)) {
|
||||
_notifier.Error("Couldn't delete media file, user " +
|
||||
(CurrentUser != null ? CurrentUser.UserName : String.Empty) + " doesn't have " +
|
||||
Permissions.DeleteMedia.Name);
|
||||
//return new HttpUnauthorizedResult();
|
||||
return View(viewModel);
|
||||
}
|
||||
if (!_authorizer.Authorize(Permissions.DeleteMedia, T("Couldn't delete media file")))
|
||||
return new HttpUnauthorizedResult();
|
||||
_mediaService.DeleteFile(viewModel.Name, 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">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head id="Head1" runat="server">
|
||||
<title>Create Media Folder</title>
|
||||
<title>Edit Media File</title>
|
||||
<% Html.Include("Head"); %>
|
||||
</head>
|
||||
<body>
|
||||
|
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Security.Permissions;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
|
@@ -1,6 +1,4 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Orchard.Security {
|
||||
namespace Orchard.Security {
|
||||
public interface IAuthenticationService : IDependency {
|
||||
void SignIn(IUser user, bool createPersistentCookie);
|
||||
void SignOut();
|
||||
|
@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Security;
|
||||
using System.Web.Security;
|
||||
|
||||
namespace Orchard.Security {
|
||||
public interface IMembershipService : IDependency {
|
||||
|
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Security;
|
||||
using Orchard.Logging;
|
||||
|
Reference in New Issue
Block a user