Skip to content

Commit

Permalink
Added ability to create custom permission attributes. (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Feb 14, 2024
1 parent e9e54f3 commit 02487b3
Show file tree
Hide file tree
Showing 78 changed files with 205 additions and 274 deletions.
7 changes: 5 additions & 2 deletions AspNetCore.Authorization.Permissions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".items", ".items", "{241433
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EDEC7D8F-4C6F-4DE3-8022-17A2A23AB18B}"
ProjectSection(SolutionItems) = preProject
src\Directory.Build.props = src\Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{24F9EFE8-010B-48D9-B5D2-EF194BDEAC31}"
EndProject
Expand Down Expand Up @@ -44,9 +47,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoSamplePermissions", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoSampleTenant", "samples\MongoSampleTenant\MongoSampleTenant.csproj", "{B7172410-2A68-4DB9-9538-036C8FFC7DE4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.Identity.Permissions.MongoDB.IntegrationTests", "tests\AspNetCore.Identity.Permissions.MongoDB.IntegrationTests\AspNetCore.Identity.Permissions.MongoDB.IntegrationTests.csproj", "{61ADF77F-B1DB-4489-AD46-DD6956605D41}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Identity.Permissions.MongoDB.IntegrationTests", "tests\AspNetCore.Identity.Permissions.MongoDB.IntegrationTests\AspNetCore.Identity.Permissions.MongoDB.IntegrationTests.csproj", "{61ADF77F-B1DB-4489-AD46-DD6956605D41}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.Identity.Permissions.EntityFrameworkCore.IntegrationTests", "tests\AspNetCore.Identity.Permissions.EntityFrameworkCore.IntegrationTests\AspNetCore.Identity.Permissions.EntityFrameworkCore.IntegrationTests.csproj", "{637FE076-7CDE-49B1-BB14-1A6CE85382A8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Identity.Permissions.EntityFrameworkCore.IntegrationTests", "tests\AspNetCore.Identity.Permissions.EntityFrameworkCore.IntegrationTests\AspNetCore.Identity.Permissions.EntityFrameworkCore.IntegrationTests.csproj", "{637FE076-7CDE-49B1-BB14-1A6CE85382A8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
2 changes: 1 addition & 1 deletion samples/MongoSamplePermissions/InvoicesContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace MongoSamplePermissions
{
using System;
using MadEyeMatt.AspNetCore.Identity.Permissions;
using MadEyeMatt.Extensions.Identity.Permissions.Stores;
using MadEyeMatt.MongoDB.DbContext;
using Microsoft.AspNetCore.Identity;
using MongoSamplePermissions.Model;
Expand Down
3 changes: 1 addition & 2 deletions samples/MongoSamplePermissions/Pages/InvoiceDelete.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace MongoSamplePermissions.Pages
{
using System;
using MadEyeMatt.AspNetCore.Authorization.Permissions;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoSamplePermissions;
using MongoSamplePermissions.Model;

[RequirePermission("Invoice.Delete")]
[RequireInvoiceDeletePermission]
public class InvoiceDeleteModel : PageModel
{
private readonly ILogger<InvoiceReadModel> logger;
Expand Down
1 change: 1 addition & 0 deletions samples/MongoSamplePermissions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MadEyeMatt.AspNetCore.Identity.MongoDB;
using MadEyeMatt.AspNetCore.Identity.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions.MongoDB;
using MadEyeMatt.Extensions.Identity.Permissions;
using MadEyeMatt.MongoDB.DbContext;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace MongoSamplePermissions
{
using System;
using JetBrains.Annotations;
using MadEyeMatt.AspNetCore.Authorization.Permissions;

[PublicAPI]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class RequireInvoiceDeletePermissionAttribute : RequirePermissionAttribute
{
/// <inheritdoc />
public RequireInvoiceDeletePermissionAttribute()
: base("Invoice.Delete")
{
}
}
}
2 changes: 1 addition & 1 deletion samples/MongoSampleTenant/InvoicesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using System;
using MongoSampleTenant.Model;
using MadEyeMatt.AspNetCore.Identity.Permissions;
using MadEyeMatt.Extensions.Identity.Permissions.Stores;
using MadEyeMatt.MongoDB.DbContext;
using Microsoft.AspNetCore.Identity;

Expand Down
2 changes: 1 addition & 1 deletion samples/MongoSampleTenant/Pages/InvoiceDelete.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using MongoSampleTenant;
using MongoSampleTenant.Model;

[RequirePermission("Invoice.Delete")]
[RequireInvoiceDeletePermission]
public class InvoiceDeleteModel : PageModel
{
private readonly ILogger<InvoiceReadModel> logger;
Expand Down
2 changes: 1 addition & 1 deletion samples/MongoSampleTenant/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using System.Threading;
using System;
using System.Collections.Generic;
using MadEyeMatt.Extensions.Identity.Permissions;
using MadEyeMatt.MongoDB.DbContext;
using MadEyeMatt.MongoDB.DbContext.Initialization;
using MongoDB.Driver;
using MongoSampleTenant;
using MongoSampleTenant.Model;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace MongoSampleTenant
{
using System;
using JetBrains.Annotations;
using MadEyeMatt.AspNetCore.Authorization.Permissions;

[PublicAPI]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class RequireInvoiceDeletePermissionAttribute : RequirePermissionAttribute
{
/// <inheritdoc />
public RequireInvoiceDeletePermissionAttribute()
: base("Invoice.Delete")
{
}
}
}
2 changes: 1 addition & 1 deletion samples/SamplePermissions/InvoicesContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace SamplePermissions
{
using MadEyeMatt.AspNetCore.Identity.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions.EntityFrameworkCore;
using MadEyeMatt.Extensions.Identity.Permissions.Stores;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

Expand Down
3 changes: 1 addition & 2 deletions samples/SamplePermissions/Pages/InvoiceDelete.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
{
using System;
using System.Linq;
using MadEyeMatt.AspNetCore.Authorization.Permissions;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using SamplePermissions.Model;

[RequirePermission("Invoice.Delete")]
[RequireInvoiceDeletePermission]
public class InvoiceDeleteModel : PageModel
{
private readonly ILogger<InvoiceReadModel> logger;
Expand Down
2 changes: 2 additions & 0 deletions samples/SamplePermissions/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using MadEyeMatt.AspNetCore.Authorization.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions.EntityFrameworkCore;
using MadEyeMatt.Extensions.Identity.Permissions;
using MadEyeMatt.Extensions.Identity.Permissions.Stores;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SamplePermissions
{
using System;
using JetBrains.Annotations;
using MadEyeMatt.AspNetCore.Authorization.Permissions;

[PublicAPI]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class RequireInvoiceDeletePermissionAttribute : RequirePermissionAttribute
{
/// <inheritdoc />
public RequireInvoiceDeletePermissionAttribute()
: base("Invoice.Delete")
{
}
}
}
2 changes: 1 addition & 1 deletion samples/SampleTenant/InvoicesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Threading;
using System.Threading.Tasks;
using MadEyeMatt.AspNetCore.Authorization.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions.EntityFrameworkCore;
using MadEyeMatt.Extensions.Identity.Permissions.Stores;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

Expand Down
2 changes: 1 addition & 1 deletion samples/SampleTenant/Pages/InvoiceDelete.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.Extensions.Logging;
using SampleTenant.Model;

[RequirePermission("Invoice.Delete")]
[RequireInvoiceDeletePermission]
public class InvoiceDeleteModel : PageModel
{
private readonly ILogger<InvoiceReadModel> logger;
Expand Down
2 changes: 1 addition & 1 deletion samples/SampleTenant/Pages/Shared/_LoginPartial.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using MadEyeMatt.AspNetCore.Identity.Permissions
@using MadEyeMatt.Extensions.Identity.Permissions.Stores
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc.TagHelpers
@inject SignInManager<IdentityTenantUser> signInManager
Expand Down
2 changes: 2 additions & 0 deletions samples/SampleTenant/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using MadEyeMatt.AspNetCore.Authorization.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions;
using MadEyeMatt.AspNetCore.Identity.Permissions.EntityFrameworkCore;
using MadEyeMatt.Extensions.Identity.Permissions;
using MadEyeMatt.Extensions.Identity.Permissions.Stores;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
Expand Down
17 changes: 17 additions & 0 deletions samples/SampleTenant/RequireInvoiceDeletePermissionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SampleTenant
{
using System;
using JetBrains.Annotations;
using MadEyeMatt.AspNetCore.Authorization.Permissions;

[PublicAPI]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class RequireInvoiceDeletePermissionAttribute : RequirePermissionAttribute
{
/// <inheritdoc />
public RequireInvoiceDeletePermissionAttribute()
: base("Invoice.Delete")
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,10 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup>
<PackageId>MadEyeMatt.$(AssemblyName)</PackageId>
<AssemblyName>MadEyeMatt.$(MSBuildProjectName)</AssemblyName>
<RootNamespace>MadEyeMatt.$(MSBuildProjectName.Replace(" ", "_").Replace(".Abstractions", ""))</RootNamespace>
<IncludeSymbols>false</IncludeSymbols>
<Copyright>Copyright © 2022-2024 Matthias Gernand. All rights reserved.</Copyright>
<Version>8.6.0</Version>
<AssemblyVersion>8.6.0</AssemblyVersion>
<FileVersion>8.6.0</FileVersion>
<Authors>Matthias Gernand</Authors>
<Description>A libary that adds permission-based authorization.</Description>
<NeutralLanguage>en</NeutralLanguage>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/mgernand/AspNetCore.Authorization.Permissions</PackageProjectUrl>
<RepositoryUrl>https://github.com/mgernand/AspNetCore.Authorization.Permissions</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>aspnetcore;authorization;permissions</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup>
<PackageId>MadEyeMatt.$(AssemblyName)</PackageId>
<AssemblyName>MadEyeMatt.$(MSBuildProjectName)</AssemblyName>
<RootNamespace>MadEyeMatt.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<IncludeSymbols>false</IncludeSymbols>
<Copyright>Copyright © 2022-2024 Matthias Gernand. All rights reserved.</Copyright>
<Version>8.6.0</Version>
<AssemblyVersion>8.6.0</AssemblyVersion>
<FileVersion>8.6.0</FileVersion>
<Authors>Matthias Gernand</Authors>
<Description>A libary that adds permission-based authorization.</Description>
<NeutralLanguage>en</NeutralLanguage>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/mgernand/AspNetCore.Authorization.Permissions</PackageProjectUrl>
<RepositoryUrl>https://github.com/mgernand/AspNetCore.Authorization.Permissions</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>aspnetcore;authorization;permissions</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>

<ItemGroup>
Expand Down
45 changes: 0 additions & 45 deletions src/AspNetCore.Authorization.Permissions/HasPermissionAttribute.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
namespace MadEyeMatt.AspNetCore.Authorization.Permissions
{
using System;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Builder;

/// <summary>
/// Authorization extension methods for <see cref="IEndpointConventionBuilder"/>.
/// </summary>
[PublicAPI]
public static class PermissionEndpointConventionBuilderExtensions
{

/// <summary>
/// Adds a permission policy with the specified name to the endpoint(s).
/// </summary>
Expand All @@ -19,12 +20,15 @@ public static class PermissionEndpointConventionBuilderExtensions
public static TBuilder RequirePermission<TBuilder>(this TBuilder builder, string permissionName)
where TBuilder : IEndpointConventionBuilder
{
if (builder == null)
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

ArgumentNullException.ThrowIfNull(permissionName);
if (string.IsNullOrWhiteSpace(permissionName))
{
throw new ArgumentNullException(nameof(permissionName));
}

return builder.RequireAuthorization(permissionName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// </summary>
[PublicAPI]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public sealed class RequirePermissionAttribute : Attribute, IAuthorizeData
public class RequirePermissionAttribute : Attribute, IAuthorizeData
{
private string policy;

Expand Down
Loading

0 comments on commit 02487b3

Please sign in to comment.