Skip to content

Commit

Permalink
[AMEND] Various modifications
Browse files Browse the repository at this point in the history
 - [AMEND] Change namespacing
 - [AMEND] Rework Startup Integrations
 - [FIX] Fix Migration execution flow
 - [AMEND] Split into Main and Core Projects
 - [FIX] Bugs with rendering
  • Loading branch information
Nik Rimington committed Jul 23, 2021
1 parent faf7317 commit 6dc605c
Show file tree
Hide file tree
Showing 31 changed files with 742 additions and 72 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Bjarke Berg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Page Not Found Manager

<img src="docs/img/logo.png?raw=true" alt="Page not found" width="250" align="right" />

[![NuGet](https://img.shields.io/nuget/v/HotChilli.Umbraco.PageNotFound.svg)](https://www.nuget.org/packages/HotChilli.Umbraco.PageNotFound/)
[![Our Umbraco](https://img.shields.io/badge/our-umbraco-orange.svg)](https://our.umbraco.com/packages/backoffice-extensions/hot-chilli-page-not-found-manager/)

## Getting Started

**This is currently a beta package**

Page Not Found Manager supports Umbraco v9 RC1+.

Umbraco v9 Packages are only available via Nuget, although there is a page on [Our.Umbraco.com](https://our.umbraco.com/packages/backoffice-extensions/hot-chilli-page-not-found-manager/) to aid discoverability.

## Installation

To [install from NuGet](https://www.nuget.org/packages/HotChilli.Umbraco.PageNotFound/), run the following command in your instance of Visual Studio.

PM> Install-Package HotChilli.Umbraco.PageNotFound

## Credits

The logo uses [404](https://thenounproject.com/term/404/3283006 ) from the [Noun Project](https://thenounproject.com) by [Ilham Fitrotul Hayat](https://thenounproject.com/fhilham), licensed under [CC BY 3.0 US](https://creativecommons.org/licenses/by/3.0/us/).
Binary file added docs/img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Ignore Umbraco files/folders generated for each instance
##
## Get latest from https://github.com/github/gitignore/blob/master/Umbraco.gitignore

# Note: VisualStudio gitignore rules may also be relevant

# Umbraco
# Ignore unimportant folders generated by Umbraco
**/App_Data/Logs/
**/App_Data/[Pp]review/
**/App_Data/TEMP/
**/App_Data/NuGetBackup/

# Ignore Umbraco content cache file
**/App_Data/umbraco.config

## this [Uu]mbraco/ folder should be created by cmd like `Install-Package UmbracoCms -Version 8.5.3`
## you can find your Umbraco version in your Web.config. (i.e. <add key="Umbraco.Core.ConfigurationStatus" value="8.5.3" />)
## Uncomment this line if you think it fits the way you work on your project.
## **/[Uu]mbraco/

# Don't ignore Umbraco packages (VisualStudio.gitignore mistakes this for a NuGet packages folder)
# Make sure to include details from VisualStudio.gitignore BEFORE this
!**/App_Data/[Pp]ackages/*
!**/[Uu]mbraco/[Dd]eveloper/[Pp]ackages/*
!**/[Uu]mbraco/[Vv]iews/[Pp]ackages/*

# ImageProcessor DiskCache
**/App_Data/cache/

# Ignore the Models Builder models out of date flag
**/ood.flag

# NEW for version 9 .Net 5 (Core)
# Umbraco 9 now stores it's unimportant folders within the umbraco directory
**/[Uu]mbraco/Data/*
# we want to keep the packages directory
!**/[Uu]mbraco/Data/[Pp]ackages/*
**/[Uu]mbraco/Logs/*
**/[Uu]mbraco/mediacache/*
**/wwwroot/umbraco/*
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using HCS.PageNotFoundManager.Core.Caching;
using HCS.PageNotFoundManager.Core.Config;
using HCS.PageNotFoundManager.Core.Models;
using HC.PageNotFoundManager.Core.Caching;
using HC.PageNotFoundManager.Core.Config;
using HC.PageNotFoundManager.Core.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Cms.Web.Common.Attributes;

namespace HCS.PageNotFoundManager.Core.Backoffice
namespace HC.PageNotFoundManager.Core.Backoffice
{
[PluginController(Constants.BackOffice)]
public class MenuController : UmbracoAuthorizedJsonController
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using HC.PageNotFoundManager.Core.Config;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models.Trees;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Security;
using Umbraco.Extensions;

namespace HC.PageNotFoundManager.Core.Backoffice
{
public class MenuRenderingNotificationHandler : INotificationHandler<MenuRenderingNotification>
{
private readonly List<string> UserGroupNames;
private readonly IBackOfficeSecurityAccessor backOfficeSecurity;

public MenuRenderingNotificationHandler(IOptions<PageNotFound> settings, IBackOfficeSecurityAccessor backOfficeSecurity)
{
if (settings is null) throw new System.ArgumentNullException(nameof(settings));

UserGroupNames = new List<string> { Umbraco.Cms.Core.Constants.Security.AdminGroupAlias };
if (settings.Value.UserGroups != null && settings.Value.UserGroups.Length > 0)
UserGroupNames.AddRange(settings.Value.UserGroups);
this.backOfficeSecurity = backOfficeSecurity ?? throw new System.ArgumentNullException(nameof(backOfficeSecurity));
}

public void Handle(MenuRenderingNotification notification)
{
if (notification.TreeAlias != Umbraco.Cms.Core.Constants.Trees.Content
&& int.TryParse(notification.NodeId, out var nodeId) && nodeId <= 0) return;

if (backOfficeSecurity.BackOfficeSecurity.CurrentUser == null) return;

if (backOfficeSecurity.BackOfficeSecurity.CurrentUser.Groups == null ||
backOfficeSecurity.BackOfficeSecurity.CurrentUser.Groups.All(g => !UserGroupNames.InvariantContains(g.Alias))) return;

var menuItem = new MenuItem(Constants.MenuAlias, Constants.MenuLabel)
{
Icon = Umbraco.Cms.Core.Constants.Icons.Content,
SeparatorBefore = true
};

menuItem.LaunchDialogView("/App_Plugins/HC.PageNotFound/Backoffice/Dialogs/dialog.html", Constants.MenuLabel);

notification.Menu.Items.Add(menuItem);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Umbraco.Cms.Core.WebAssets;

namespace HC.PageNotFoundManager.Core.Backoffice
{
public class PageNotFoundDialogControllerFile : JavaScriptFile
{
public PageNotFoundDialogControllerFile() : base("/App_Plugins/HC.PageNotFound/js/dialog.controller.js")
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Umbraco.Cms.Core.WebAssets;

namespace HC.PageNotFoundManager.Core.Backoffice
{
public class PageNotFoundResourceFile : JavaScriptFile
{
public PageNotFoundResourceFile() : base("/App_Plugins/HC.PageNotFound/js/resource.js")
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using HCS.PageNotFoundManager.Core.Config;
using HCS.PageNotFoundManager.Core.Models;
using HC.PageNotFoundManager.Core.Config;
using HC.PageNotFoundManager.Core.Models;
using System;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Serialization;

namespace HCS.PageNotFoundManager.Core.Caching
namespace HC.PageNotFoundManager.Core.Caching
{
public class PageNotFoundCacheRefresher : PayloadCacheRefresherBase<CacheRefresherNotification, PageNotFoundRequest>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using HCS.PageNotFoundManager.Core.Models;
using HC.PageNotFoundManager.Core.Models;
using System;
using Umbraco.Cms.Core.Cache;

namespace HCS.PageNotFoundManager.Core.Caching
namespace HC.PageNotFoundManager.Core.Caching
{
public static class DistributedCacheExtensions
{
Expand Down
16 changes: 16 additions & 0 deletions src/HC.PageNotFoundManager.Core/Config/PageNotFound.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace HC.PageNotFoundManager.Core.Config
{
/// <summary>
/// Class to represent the settings from appsettings
/// </summary>
public class PageNotFound
{
public PageNotFound()
{
UserGroups = Array.Empty<string>();
}
public string[] UserGroups { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using HCS.PageNotFoundManager.Core.Models;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Web;
using Umbraco.Extensions;

namespace HCS.PageNotFoundManager.Core.Config
namespace HC.PageNotFoundManager.Core.Config
{
public class PageNotFoundConfig : IPageNotFoundConfig
{
Expand Down Expand Up @@ -45,12 +44,10 @@ public int GetNotFoundPage(Guid parentKey)

public void SetNotFoundPage(int parentId, int pageNotFoundId, bool refreshCache)
{
using (var umbracoContext = umbracoContextFactory.EnsureUmbracoContext())
{
var parentPage = umbracoContext.UmbracoContext.Content.GetById(parentId);
var pageNotFoundPage = umbracoContext.UmbracoContext.Content.GetById(pageNotFoundId);
SetNotFoundPage(parentPage.Key, pageNotFoundPage != null ? pageNotFoundPage.Key : Guid.Empty, refreshCache);
}
using var umbracoContext = umbracoContextFactory.EnsureUmbracoContext();
var parentPage = umbracoContext.UmbracoContext.Content.GetById(parentId);
var pageNotFoundPage = umbracoContext.UmbracoContext.Content.GetById(pageNotFoundId);
SetNotFoundPage(parentPage.Key, pageNotFoundPage != null ? pageNotFoundPage.Key : Guid.Empty, refreshCache);
}

public void SetNotFoundPage(Guid parentKey, Guid pageNotFoundKey, bool refreshCache)
Expand All @@ -59,11 +56,11 @@ public void SetNotFoundPage(Guid parentKey, Guid pageNotFoundKey, bool refreshCa
using (var scope = scopeProvider.CreateScope())
{
var db = scope.Database;
var page = db.Query<PageNotFound>().Where(p => p.ParentId == parentKey).FirstOrDefault();
var page = db.Query<Models.PageNotFound>().Where(p => p.ParentId == parentKey).FirstOrDefault();
if (page == null && !Guid.Empty.Equals(pageNotFoundKey))
{
// create the page
db.Insert(new PageNotFound { ParentId = parentKey, NotFoundPageId = pageNotFoundKey });
db.Insert(new Models.PageNotFound { ParentId = parentKey, NotFoundPageId = pageNotFoundKey });
}
else if (page != null)
{
Expand All @@ -73,7 +70,7 @@ public void SetNotFoundPage(Guid parentKey, Guid pageNotFoundKey, bool refreshCa
{
// update the existing page
page.NotFoundPageId = pageNotFoundKey;
db.Update(PageNotFound.TableName, "ParentId", page);
db.Update(Models.PageNotFound.TableName, "ParentId", page);
}
}
scope.Complete();
Expand All @@ -89,20 +86,20 @@ public void RefreshCache()
appPolicyCache.Insert(CacheKey, LoadFromDb);
}

private List<PageNotFound> ConfiguredPages
private List<Models.PageNotFound> ConfiguredPages
{
get
{
var us = (List<PageNotFound>)appPolicyCache.Get(CacheKey, LoadFromDb);
var us = (List<Models.PageNotFound>)appPolicyCache.Get(CacheKey, LoadFromDb);
return us;
}
}

private List<PageNotFound> LoadFromDb()
private List<Models.PageNotFound> LoadFromDb()
{
using var scope = scopeProvider.CreateScope(autoComplete: true);
var sql = scope.SqlContext.Sql().Select("*").From<PageNotFound>();
var pages = scope.Database.Fetch<PageNotFound>(sql);
var sql = scope.SqlContext.Sql().Select("*").From<Models.PageNotFound>();
var pages = scope.Database.Fetch<Models.PageNotFound>(sql);
scope.Complete();
return pages;
}
Expand Down
11 changes: 11 additions & 0 deletions src/HC.PageNotFoundManager.Core/Constants/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace HC.PageNotFoundManager.Core
{
public static class Constants
{
public const string Base = "HCS";
public const string BackOffice = Base + "PageNotFound";

public const string MenuAlias = "pageNotFoundManagement";
public const string MenuLabel = "Manage 404 Page";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HCS.PageNotFoundManager.Core.Config;
using HC.PageNotFoundManager.Core.Config;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,7 +9,7 @@
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;

namespace HCS.PageNotFoundManager.Core.ContentFinders
namespace HC.PageNotFoundManager.Core.ContentFinders
{
public class PageNotFoundFinder : IContentLastChanceFinder
{
Expand Down
36 changes: 36 additions & 0 deletions src/HC.PageNotFoundManager.Core/HC.PageNotFoundManager.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Version>1.0.0</Version>
<InformationalVersion>1.0.0</InformationalVersion>
<TargetFramework>net5.0</TargetFramework>
<Authors>Nik Rimington</Authors>
<ContentTargetFolders>.</ContentTargetFolders>
<Product>HotChilli.Umbraco.PageNotFound.Core</Product>
<PackageId>HotChilli.Umbraco.PageNotFound.Core</PackageId>
<Title>HotChilli.Umbraco.PageNotFound.Core</Title>
<Description>Umbraco package for setting 404 response pages </Description>
<Product>HotChilli.Umbraco.PageNotFound.Core</Product>
<PackageTags>umbraco plugin package</PackageTags>
<PackageProjectUrl>https://github.com/NikRimington/HCS.Umbraco.PageNotFoundManager</PackageProjectUrl>
<PackageIcon>logo.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/NikRimington/HCS.Umbraco.PageNotFoundManager/main/docs/img/logo.png</PackageIconUrl>
<RepositoryUrl>https://github.com/NikRimington/HCS.Umbraco.PageNotFoundManager</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Umbraco.Cms.Core" Version="9.0.0-rc001" />
<PackageReference Include="Umbraco.Cms.Infrastructure" Version="9.0.0-rc001" />
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="9.0.0-rc001" />
<PackageReference Include="Umbraco.Cms.Web.Common" Version="9.0.0-rc001" />
<PackageReference Include="Umbraco.Cms.Web.Website" Version="9.0.0-rc001" />
</ItemGroup>
<ItemGroup>
<None Include="../../docs/img/logo.png" Pack="true" PackPath="\" />
<None Include="../../LICENSE" Pack="true" PackPath="" />
</ItemGroup>

</Project>
Loading

0 comments on commit 6dc605c

Please sign in to comment.