Fixing access to the CurrentSite and CurrentUser in the MediaService

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-11-18 12:26:07 -08:00
parent 42fbe44c6b
commit 1a83ea5233
@@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
using System.Web; using System.Web;
using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip;
using JetBrains.Annotations; using JetBrains.Annotations;
@@ -9,24 +8,20 @@ using Orchard.ContentManagement;
using Orchard.FileSystems.Media; using Orchard.FileSystems.Media;
using Orchard.Logging; using Orchard.Logging;
using Orchard.Media.Models; using Orchard.Media.Models;
using Orchard.Security;
using Orchard.Settings;
namespace Orchard.Media.Services { namespace Orchard.Media.Services {
[UsedImplicitly] [UsedImplicitly]
public class MediaService : IMediaService { public class MediaService : IMediaService {
private readonly IStorageProvider _storageProvider; private readonly IStorageProvider _storageProvider;
private readonly IOrchardServices _orchardServices;
public MediaService( public MediaService(IStorageProvider storageProvider, IOrchardServices orchardServices) {
IStorageProvider storageProvider) {
_storageProvider = storageProvider; _storageProvider = storageProvider;
_orchardServices = orchardServices;
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
} }
public ILogger Logger { get; set; } public ILogger Logger { get; set; }
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
protected virtual IUser CurrentUser { get; [UsedImplicitly] private set; }
public string GetPublicUrl(string path) { public string GetPublicUrl(string path) {
return _storageProvider.GetPublicUrl(path); return _storageProvider.GetPublicUrl(path);
@@ -114,14 +109,16 @@ namespace Orchard.Media.Services {
if (string.IsNullOrWhiteSpace(name)) { if (string.IsNullOrWhiteSpace(name)) {
return false; return false;
} }
var mediaSettings = CurrentSite.As<MediaSettingsPart>(); var currentSite = _orchardServices.WorkContext.CurrentSite;
var mediaSettings = currentSite.As<MediaSettingsPart>();
var allowedExtensions = mediaSettings.UploadAllowedFileTypeWhitelist.ToUpperInvariant().Split(' '); var allowedExtensions = mediaSettings.UploadAllowedFileTypeWhitelist.ToUpperInvariant().Split(' ');
var ext = (Path.GetExtension(name) ?? "").TrimStart('.').ToUpperInvariant(); var ext = (Path.GetExtension(name) ?? "").TrimStart('.').ToUpperInvariant();
if (string.IsNullOrWhiteSpace(ext)) { if (string.IsNullOrWhiteSpace(ext)) {
return false; return false;
} }
// whitelist does not apply to the superuser // whitelist does not apply to the superuser
if (CurrentUser == null || !CurrentSite.SuperUser.Equals(CurrentUser.UserName, StringComparison.Ordinal)) { var currentUser = _orchardServices.WorkContext.CurrentUser;
if (currentUser == null || !currentSite.SuperUser.Equals(currentUser.UserName, StringComparison.Ordinal)) {
// zip files at the top level are allowed since this is how you upload multiple files at once. // zip files at the top level are allowed since this is how you upload multiple files at once.
if (allowZip && ext.Equals("zip", StringComparison.OrdinalIgnoreCase)) { if (allowZip && ext.Equals("zip", StringComparison.OrdinalIgnoreCase)) {
return true; return true;