MatinDeWet.EntitySecurity.Logic 1.1.1

dotnet add package MatinDeWet.EntitySecurity.Logic --version 1.1.1
                    
NuGet\Install-Package MatinDeWet.EntitySecurity.Logic -Version 1.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MatinDeWet.EntitySecurity.Logic" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MatinDeWet.EntitySecurity.Logic" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="MatinDeWet.EntitySecurity.Logic" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MatinDeWet.EntitySecurity.Logic --version 1.1.1
                    
#r "nuget: MatinDeWet.EntitySecurity.Logic, 1.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MatinDeWet.EntitySecurity.Logic@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MatinDeWet.EntitySecurity.Logic&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=MatinDeWet.EntitySecurity.Logic&version=1.1.1
                    
Install as a Cake Tool

EntitySecurity

GitHub Actions Workflow Status

Packedges

EntitySecurity.Domain

NuGet Version

EntitySecurity.Contract

NuGet Version

EntitySecurity.Logic

NuGet Version

Entity Security is a library that allows you to secure your entities in your application.

Setup

EntitySecurity.Logic contains a register method that you can use to register your entities.

	public void ConfigureServices(IServiceCollection services)
	{
		services.AddEntitySecurity();
	}

Creating a custom Repository

To be able to effectively make use of this library you will need to create a custom repository with a interface that will inherit from the IRepository interface.

	public interface IExampleRepository : IRepository
	{
	}
	public class ExampleRepository : JudgedRepository<TestContext>, IExampleRepository
	{
		public ExampleRepository(TestContext ctx) : base(ctx)
		{
		}
	}

Within the repository you will need to inherit from the JudgedRepository class where it takes in your context as a generic type. You will need to register this repository in the main project program file.

    services.AddScoped<IExampleRepository, ExampleRepository>();

Securing your data

To secure your data you will need to create locks on your data. Currently the library makes use of a locking mechanism. There are two methods that need to be implemented in the lock classes: (Secured and HasAccess)

  • Secured is used when reading data
  • HasAccess is used when saving data

They can be implemented as follows:

    public class ClientLock : Lock<Client>
    {
        private readonly TestContext _context;

        public ClientLock(TestContext context)
        {
            _context = context;
        }

        public override IQueryable<Client> Secured(int identityId)
        {
            var qry = from c in _context.Clients
                      join ut in _context.UserTeams on c.TeamId equals ut.TeamId
                      where ut.UserId == identityId
                      select c;

            return qry;
        }

        public override async Task<bool> HasAccess(Client obj, RepositoryOperationEnum operation, int identityId, CancellationToken cancellationToken)
        {
            var teamId = obj.TeamId;

            if (teamId == 0)
            {
                return false;
            }

            var query = from ut in _context.UserTeams
                        where ut.UserId == identityId
                        && ut.TeamId == teamId
                        select ut.TeamId;

            return await query.AnyAsync(cancellationToken);
        }
    }

You will also need to register the locks.

    services.AddScoped<IProtected, ClientLock>();

The Middleware

For the Library to work you will need to supply the library with the current identity. Specifically with a subjectid(EntitySecurityClaimTypes.sub) claim

The IdentityConstants Id will be used in the lock.

    new Claim(EntitySecurityClaimTypes.sub, "1")

You will need to pass the user claims through to the IInfoSetter interface and call the SetUser to pass the user claims through, this data is required data to the library.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 278 9/15/2024
1.1.0 133 9/15/2024
1.0.1 190 9/3/2024
1.0.0 119 9/3/2024