Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 31 additions & 33 deletions src/OrchardCoreContrib.GoogleMaps/AdminMenu.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
using Microsoft.Extensions.Localization;
using OrchardCoreContrib.GoogleMaps.Drivers;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Localization;
using OrchardCore.Navigation;
using OrchardCoreContrib.GoogleMaps.Drivers;

namespace OrchardCoreContrib.GoogleMaps;

using OrchardCoreContrib.Navigation;

namespace OrchardCoreContrib.GoogleMaps
/// <summary>
/// Represents an admin menu for GoogleMaps module.
/// </summary>
/// <remarks>
/// Initializes a new instance of <see cref="AdminMenu"/>.
/// </remarks>
/// <param name="stringLocalizer"></param>
public class AdminMenu(IStringLocalizer<AdminMenu> S) : AdminNavigationProvider
{
using OrchardCoreContrib.Navigation;

/// <summary>
/// Represents an admin menu for GoogleMaps module.
/// </summary>
public class AdminMenu : AdminNavigationProvider
private static readonly RouteValueDictionary _routeValues = new()
{
private readonly IStringLocalizer S;

/// <summary>
/// Initializes a new instance of <see cref="AdminMenu"/>.
/// </summary>
/// <param name="stringLocalizer"></param>
public AdminMenu(IStringLocalizer<AdminMenu> stringLocalizer)
{
S = stringLocalizer;
}
{ "area", "OrchardCore.Settings" },
{ "groupId", GoogleMapsSettingsDisplayDriver.GroupId },
};

/// <inheritdoc/>
public override void BuildNavigation(NavigationBuilder builder)
{
builder
.Add(S["Configuration"], configuration => configuration
.Add(S["Settings"], settings => settings
.Add(S["Google Maps"], S["Google Maps"].PrefixPosition(), entry => entry
.AddClass("googlemaps").Id("googlemaps")
.Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = GoogleMapsSettingsDisplayDriver.GroupId })
.Permission(Permissions.ManageGoogleMapsSettings)
.LocalNav()
)
/// <inheritdoc/>
public override void BuildNavigation(NavigationBuilder builder)
{
builder
.Add(S["Configuration"], configuration => configuration
.Add(S["Settings"], settings => settings
.Add(S["Google Maps"], S["Google Maps"].PrefixPosition(), entry => entry
.AddClass("googlemaps").Id("googlemaps")
.Action("Index", "Admin", _routeValues)
.Permission(GoogleMapsPermissions.ManageGoogleMapsSettings)
.LocalNav()
)
);
}
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
using OrchardCore.ContentManagement.Display.ContentDisplay;
using OrchardCore.ContentManagement.Display.Models;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCoreContrib.GoogleMaps.Models;
using OrchardCoreContrib.GoogleMaps.ViewModels;
using System.Threading.Tasks;

namespace OrchardCoreContrib.GoogleMaps.Drivers
namespace OrchardCoreContrib.GoogleMaps.Drivers;

/// <summary>
/// Represents a display driver for <see cref="GoogleMapPart"/>.
/// </summary>
public class GoogleMapPartDisplayDriver : ContentPartDisplayDriver<GoogleMapPart>
{
/// <summary>
/// Represents a display driver for <see cref="GoogleMapPart"/>.
/// </summary>
public class GoogleMapPartDisplayDriver : ContentPartDisplayDriver<GoogleMapPart>
public override IDisplayResult Display(GoogleMapPart part, BuildPartDisplayContext context)
{
public override IDisplayResult Display(GoogleMapPart part, BuildPartDisplayContext context)
{
var settings = context.TypePartDefinition.GetSettings<GoogleMapsSettings>();
var settings = context.TypePartDefinition.GetSettings<GoogleMapsSettings>();

return Initialize<GoogleMapPartViewModel>(GetDisplayShapeType(context), model =>
{
model.Latitude = part.Latitude;
model.Longitude = part.Longitude;
model.GoogleMapPart = part;
model.ContentItem = part.ContentItem;
model.Settings = settings;
})
.Location("Detail", "Content:20")
.Location("Summary", "Meta:5");
return Initialize<GoogleMapPartViewModel>(GetDisplayShapeType(context), model =>
{
model.Latitude = part.Latitude;
model.Longitude = part.Longitude;
model.GoogleMapPart = part;
model.ContentItem = part.ContentItem;
model.Settings = settings;
})
.Location("Detail", "Content:20")
.Location("Summary", "Meta:5");

}
}

public override IDisplayResult Edit(GoogleMapPart part, BuildPartEditorContext context)
public override IDisplayResult Edit(GoogleMapPart part, BuildPartEditorContext context)
{
return Initialize<GoogleMapPartViewModel>(GetEditorShapeType(context), model =>
{
return Initialize<GoogleMapPartViewModel>(GetEditorShapeType(context), model =>
{
model.Latitude = part.Latitude;
model.Longitude = part.Longitude;
model.GoogleMapPart = part;
model.ContentItem = part.ContentItem;
model.Settings = context.TypePartDefinition.GetSettings<GoogleMapsSettings>();
});
}
model.Latitude = part.Latitude;
model.Longitude = part.Longitude;
model.GoogleMapPart = part;
model.ContentItem = part.ContentItem;
model.Settings = context.TypePartDefinition.GetSettings<GoogleMapsSettings>();
});
}

public override async Task<IDisplayResult> UpdateAsync(GoogleMapPart model, UpdatePartEditorContext context)
{
await context.Updater.TryUpdateModelAsync(model, Prefix, p => p.Latitude, p => p.Longitude);
public override async Task<IDisplayResult> UpdateAsync(GoogleMapPart model, UpdatePartEditorContext context)
{
await context.Updater.TryUpdateModelAsync(model, Prefix, p => p.Latitude, p => p.Longitude);

return Edit(model, context);
}
return Edit(model, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,77 @@
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Settings;
using OrchardCoreContrib.GoogleMaps.ViewModels;
using System.Threading.Tasks;

namespace OrchardCoreContrib.GoogleMaps.Drivers
namespace OrchardCoreContrib.GoogleMaps.Drivers;

/// <summary>
/// Represents a display driver for <see cref="GoogleMapsSettings"/>.
/// </summary>
/// <remarks>
/// Initializes a new instance of <see cref="GoogleMapsSettingsDisplayDriver"/>.
/// </remarks>
/// <param name="dataProtectionProvider">The <see cref="IDataProtectionProvider"/>.</param>
/// <param name="httpContextAccessor">The <see cref="IHttpContextAccessor"/>.</param>
/// <param name="authorizationService">The <see cref="IAuthorizationService"/>.</param>
public class GoogleMapsSettingsDisplayDriver(
IDataProtectionProvider dataProtectionProvider,
IHttpContextAccessor httpContextAccessor,
IAuthorizationService authorizationService) : SectionDisplayDriver<ISite, GoogleMapsSettings>
{
/// <summary>
/// Represents a display driver for <see cref="GoogleMapsSettings"/>.
/// </summary>
public class GoogleMapsSettingsDisplayDriver : SectionDisplayDriver<ISite, GoogleMapsSettings>
{
public const string GroupId = "google-maps";
public const string GroupId = "google-maps";

private readonly IDataProtectionProvider _dataProtectionProvider;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IAuthorizationService _authorizationService;
/// <inheritdoc/>
public override async Task<IDisplayResult> EditAsync(ISite model, GoogleMapsSettings section, BuildEditorContext context)
{
var user = httpContextAccessor.HttpContext?.User;

/// <summary>
/// Initializes a new instance of <see cref="GoogleMapsSettingsDisplayDriver"/>.
/// </summary>
/// <param name="dataProtectionProvider">The <see cref="IDataProtectionProvider"/>.</param>
/// <param name="httpContextAccessor">The <see cref="IHttpContextAccessor"/>.</param>
/// <param name="authorizationService">The <see cref="IAuthorizationService"/>.</param>
public GoogleMapsSettingsDisplayDriver(
IDataProtectionProvider dataProtectionProvider,
IHttpContextAccessor httpContextAccessor,
IAuthorizationService authorizationService)
if (!await authorizationService.AuthorizeAsync(user, GoogleMapsPermissions.ManageGoogleMapsSettings))
{
_dataProtectionProvider = dataProtectionProvider;
_httpContextAccessor = httpContextAccessor;
_authorizationService = authorizationService;
return null;
}

/// <inheritdoc/>
public override async Task<IDisplayResult> EditAsync(ISite model, GoogleMapsSettings section, BuildEditorContext context)
return Initialize<GoogleMapsSettingsViewModel>("GoogleMapsSettings_Edit", model =>
{
var user = _httpContextAccessor.HttpContext?.User;
model.ApiKey = section.ApiKey;
model.Latitude = section.Latitude;
model.Longitude = section.Longitude;
}).Location("Content").OnGroup(GroupId);
}

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageGoogleMapsSettings))
{
return null;
}
/// <inheritdoc/>
public override async Task<IDisplayResult> UpdateAsync(ISite model, GoogleMapsSettings section, UpdateEditorContext context)
{
var user = httpContextAccessor.HttpContext?.User;

return Initialize<GoogleMapsSettingsViewModel>("GoogleMapsSettings_Edit", model =>
{
model.ApiKey = section.ApiKey;
model.Latitude = section.Latitude;
model.Longitude = section.Longitude;
}).Location("Content").OnGroup(GroupId);
if (!await authorizationService.AuthorizeAsync(user, GoogleMapsPermissions.ManageGoogleMapsSettings))
{
return null;
}

/// <inheritdoc/>
public override async Task<IDisplayResult> UpdateAsync(ISite model, GoogleMapsSettings section, UpdateEditorContext context)
if (context.GroupId == GroupId)
{
var user = _httpContextAccessor.HttpContext?.User;

if (!await _authorizationService.AuthorizeAsync(user, Permissions.ManageGoogleMapsSettings))
var viewModel = new GoogleMapsSettingsViewModel();
var previousApiKey = section.ApiKey;

if (await context.Updater.TryUpdateModelAsync(viewModel, Prefix, m => m.ApiKey, m => m.Latitude, m => m.Longitude))
{
return null;
section.ApiKey = viewModel.ApiKey;
section.Latitude = viewModel.Latitude;
section.Longitude = viewModel.Longitude;
}

if (context.GroupId == GroupId)
if (string.IsNullOrWhiteSpace(section.ApiKey))
{
var viewModel = new GoogleMapsSettingsViewModel();
var previousApiKey = section.ApiKey;

if (await context.Updater.TryUpdateModelAsync(viewModel, Prefix, m => m.ApiKey, m => m.Latitude, m => m.Longitude))
{
section.ApiKey = viewModel.ApiKey;
section.Latitude = viewModel.Latitude;
section.Longitude = viewModel.Longitude;
}

if (string.IsNullOrWhiteSpace(section.ApiKey))
{
section.ApiKey = previousApiKey;
}
else
{
var protector = _dataProtectionProvider.CreateProtector(GroupId);

section.ApiKey = protector.Protect(section.ApiKey);
}
section.ApiKey = previousApiKey;
}
else
{
var protector = dataProtectionProvider.CreateProtector(GroupId);

return await EditAsync(model, section, context);
section.ApiKey = protector.Protect(section.ApiKey);
}
}

return await EditAsync(model, section, context);
}
}
11 changes: 5 additions & 6 deletions src/OrchardCoreContrib.GoogleMaps/GoogleMapsDefaults.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace OrchardCoreContrib.GoogleMaps
namespace OrchardCoreContrib.GoogleMaps;

public class GoogleMapsDefaults
{
public class GoogleMapsDefaults
{
public static readonly double Latitude = 15.5539;
public static readonly double Latitude = 15.5539;

public static readonly double Longitude = 48.1748;
}
public static readonly double Longitude = 48.1748;
}
14 changes: 14 additions & 0 deletions src/OrchardCoreContrib.GoogleMaps/GoogleMapsPermissions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using OrchardCore.Security.Permissions;

namespace OrchardCoreContrib.GoogleMaps;

/// <summary>
/// Represents a permissions that will be applied into GoogleMaps module.
/// </summary>
public class GoogleMapsPermissions
{
/// <summary>
/// Gets a permission for managing a Google Maps settings.
/// </summary>
public static readonly Permission ManageGoogleMapsSettings = new("ManageGoogleMapsSettings", "Manage Google Maps Settings");
}
13 changes: 6 additions & 7 deletions src/OrchardCoreContrib.GoogleMaps/GoogleMapsSettings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace OrchardCoreContrib.GoogleMaps
namespace OrchardCoreContrib.GoogleMaps;

public class GoogleMapsSettings
{
public class GoogleMapsSettings
{
public string ApiKey { get; set; }
public string ApiKey { get; set; }

public double Latitude { get; set; } = GoogleMapsDefaults.Latitude;
public double Latitude { get; set; } = GoogleMapsDefaults.Latitude;

public double Longitude { get; set; } = GoogleMapsDefaults.Longitude;
}
public double Longitude { get; set; } = GoogleMapsDefaults.Longitude;
}
2 changes: 1 addition & 1 deletion src/OrchardCoreContrib.GoogleMaps/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Name = "Google Maps",
Author = ManifestConstants.Author,
Website = ManifestConstants.Website,
Version = "1.3.1",
Version = "1.5.0",
Description = "Displays Google maps.",
Dependencies = new[] { "OrchardCore.Contents" },
Category = "Content Management"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@
namespace OrchardCoreContrib.GoogleMaps.Migrations;

[Migration(1)]
public class CreateGoogleMapPart : Migration
public class CreateGoogleMapPart(IContentDefinitionManager contentDefinitionManager) : Migration
{
private readonly IContentDefinitionManager _contentDefinitionManager;

public CreateGoogleMapPart(IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
}

public override void Up()
{
_contentDefinitionManager.AlterPartDefinitionAsync("GoogleMapPart", builder => builder
contentDefinitionManager.AlterPartDefinitionAsync("GoogleMapPart", builder => builder
.Attachable()
.WithDescription("Provides a Google Map that you can use for your content item."));
}

public override void Down()
{
_contentDefinitionManager.DeletePartDefinitionAsync("GoogleMapPart").GetAwaiter().GetResult();
contentDefinitionManager.DeletePartDefinitionAsync("GoogleMapPart").GetAwaiter().GetResult();
}
}
Loading
Loading