Adding AuthorizePage & AuthorizeFolder without requiring a policy
Refactoring
This commit is contained in:
parent
f7fd5114b3
commit
e44d875df4
|
|
@ -61,7 +61,16 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a <see cref="AuthorizeFilter"/> with the specified policy to all page under the specified path.
|
/// Adds a <see cref="AuthorizeFilter"/> to the page with the specified path.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
|
||||||
|
/// <param name="path">The path of the Razor Page.</param>
|
||||||
|
/// <returns>The <see cref="RazorPagesOptions"/>.</returns>
|
||||||
|
public static RazorPagesOptions AuthorizePage(this RazorPagesOptions options, string path) =>
|
||||||
|
AuthorizePage(options, path, policy: string.Empty);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a <see cref="AuthorizeFilter"/> with the specified policy to all pages under the specified path.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
|
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
|
||||||
/// <param name="folderPath">The folder path.</param>
|
/// <param name="folderPath">The folder path.</param>
|
||||||
|
|
@ -84,6 +93,15 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a <see cref="AuthorizeFilter"/> to all pages under the specified path.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
|
||||||
|
/// <param name="folderPath">The folder path.</param>
|
||||||
|
/// <returns>The <see cref="RazorPagesOptions"/>.</returns>
|
||||||
|
public static RazorPagesOptions AuthorizeFolder(this RazorPagesOptions options, string folderPath) =>
|
||||||
|
AuthorizeFolder(options, folderPath, policy: string.Empty);
|
||||||
|
|
||||||
private class PageConvention : IPageApplicationModelConvention
|
private class PageConvention : IPageApplicationModelConvention
|
||||||
{
|
{
|
||||||
private readonly string _path;
|
private readonly string _path;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void AuthorizePage_AddsAuthorizeFilterToSpecificPage()
|
public void AuthorizePage_AddsAuthorizeFilterWithPolicyToSpecificPage()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var options = new RazorPagesOptions();
|
var options = new RazorPagesOptions();
|
||||||
|
|
@ -66,10 +66,39 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
model => Assert.Empty(model.Filters));
|
model => Assert.Empty(model.Filters));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AuthorizePage_AddsAuthorizeFilterWithoutPolicyToSpecificPage()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var options = new RazorPagesOptions();
|
||||||
|
var models = new[]
|
||||||
|
{
|
||||||
|
new PageApplicationModel("/Pages/Index.cshtml", "/Index.cshtml"),
|
||||||
|
new PageApplicationModel("/Pages/Users/Account.cshtml", "/Users/Account.cshtml"),
|
||||||
|
new PageApplicationModel("/Pages/Users/Contact.cshtml", "/Users/Contact.cshtml"),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
options.AuthorizePage("/Users/Account.cshtml");
|
||||||
|
ApplyConventions(options, models);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Collection(models,
|
||||||
|
model => Assert.Empty(model.Filters),
|
||||||
|
model =>
|
||||||
|
{
|
||||||
|
Assert.Equal("/Users/Account.cshtml", model.ViewEnginePath);
|
||||||
|
var authorizeFilter = Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
|
||||||
|
var authorizeData = Assert.IsType<AuthorizeAttribute>(Assert.Single(authorizeFilter.AuthorizeData));
|
||||||
|
Assert.Equal(string.Empty, authorizeData.Policy);
|
||||||
|
},
|
||||||
|
model => Assert.Empty(model.Filters));
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("/Users")]
|
[InlineData("/Users")]
|
||||||
[InlineData("/Users/")]
|
[InlineData("/Users/")]
|
||||||
public void AuthorizePage_AddsAuthorizeFilterToPagesUnderFolder(string folderName)
|
public void AuthorizePage_AddsAuthorizeFilterWithPolicyToPagesUnderFolder(string folderName)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var options = new RazorPagesOptions();
|
var options = new RazorPagesOptions();
|
||||||
|
|
@ -103,6 +132,43 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("/Users")]
|
||||||
|
[InlineData("/Users/")]
|
||||||
|
public void AuthorizePage_AddsAuthorizeFilterWithoutPolicyToPagesUnderFolder(string folderName)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var options = new RazorPagesOptions();
|
||||||
|
var models = new[]
|
||||||
|
{
|
||||||
|
new PageApplicationModel("/Pages/Index.cshtml", "/Index.cshtml"),
|
||||||
|
new PageApplicationModel("/Pages/Users/Account.cshtml", "/Users/Account.cshtml"),
|
||||||
|
new PageApplicationModel("/Pages/Users/Contact.cshtml", "/Users/Contact.cshtml"),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
options.AuthorizeFolder(folderName);
|
||||||
|
ApplyConventions(options, models);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Collection(models,
|
||||||
|
model => Assert.Empty(model.Filters),
|
||||||
|
model =>
|
||||||
|
{
|
||||||
|
Assert.Equal("/Users/Account.cshtml", model.ViewEnginePath);
|
||||||
|
var authorizeFilter = Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
|
||||||
|
var authorizeData = Assert.IsType<AuthorizeAttribute>(Assert.Single(authorizeFilter.AuthorizeData));
|
||||||
|
Assert.Equal(string.Empty, authorizeData.Policy);
|
||||||
|
},
|
||||||
|
model =>
|
||||||
|
{
|
||||||
|
Assert.Equal("/Users/Contact.cshtml", model.ViewEnginePath);
|
||||||
|
var authorizeFilter = Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
|
||||||
|
var authorizeData = Assert.IsType<AuthorizeAttribute>(Assert.Single(authorizeFilter.AuthorizeData));
|
||||||
|
Assert.Equal(string.Empty, authorizeData.Policy);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private static void ApplyConventions(RazorPagesOptions options, PageApplicationModel[] models)
|
private static void ApplyConventions(RazorPagesOptions options, PageApplicationModel[] models)
|
||||||
{
|
{
|
||||||
foreach (var convention in options.Conventions)
|
foreach (var convention in options.Conventions)
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ namespace RazorPagesWebSite
|
||||||
.AddCookieTempDataProvider()
|
.AddCookieTempDataProvider()
|
||||||
.AddRazorPagesOptions(options =>
|
.AddRazorPagesOptions(options =>
|
||||||
{
|
{
|
||||||
options.AuthorizePage("/HelloWorldWithAuth", string.Empty);
|
options.AuthorizePage("/HelloWorldWithAuth");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,8 @@ namespace RazorPagesWebSite
|
||||||
.AddRazorPagesOptions(options =>
|
.AddRazorPagesOptions(options =>
|
||||||
{
|
{
|
||||||
options.RootDirectory = "/Pages";
|
options.RootDirectory = "/Pages";
|
||||||
options.AuthorizePage("/Conventions/Auth", string.Empty);
|
options.AuthorizePage("/Conventions/Auth");
|
||||||
options.AuthorizeFolder("/Conventions/AuthFolder", string.Empty);
|
options.AuthorizeFolder("/Conventions/AuthFolder");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue