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
<PackageReference Include="MatinDeWet.EntitySecurity.Logic" Version="1.1.1" />
<PackageVersion Include="MatinDeWet.EntitySecurity.Logic" Version="1.1.1" />
<PackageReference Include="MatinDeWet.EntitySecurity.Logic" />
paket add MatinDeWet.EntitySecurity.Logic --version 1.1.1
#r "nuget: MatinDeWet.EntitySecurity.Logic, 1.1.1"
#:package MatinDeWet.EntitySecurity.Logic@1.1.1
#addin nuget:?package=MatinDeWet.EntitySecurity.Logic&version=1.1.1
#tool nuget:?package=MatinDeWet.EntitySecurity.Logic&version=1.1.1
EntitySecurity
Packedges
EntitySecurity.Domain
EntitySecurity.Contract
EntitySecurity.Logic
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 | Versions 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. |
-
net8.0
- MatinDeWet.EntitySecurity.Contract (>= 1.1.0)
- Microsoft.EntityFrameworkCore (>= 8.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.