Git Product home page Git Product logo

efcore-adapter's Introduction

EF Core Adapter

Actions Status Coverage Status NuGet

EF Core Adapter is the EF Core adapter for Casbin. With this library, Casbin can load policy from EF Core supported database or save policy to it.

The current version supported all databases which EF Core supported, there is a part list:

  • SQL Server 2012 onwards
  • SQLite 3.7 onwards
  • Azure Cosmos DB SQL API
  • PostgreSQL
  • MySQL, MariaDB
  • Oracle DB
  • Db2, Informix
  • And more...

You can see all the list at Database Providers.

Installation

dotnet add package Casbin.NET.Adapter.EFCore

Simple Example

using Casbin.Adapter.EFCore;
using Microsoft.EntityFrameworkCore;
using NetCasbin;

namespace ConsoleAppExample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // You should build a DbContextOptions for CasbinDbContext<TKey>.
            // The example use the SQLite database named "casbin_example.sqlite3".
            var options = new DbContextOptionsBuilder<CasbinDbContext<int>>()
                .UseSqlite("Data Source=casbin_example.sqlite3")
                .Options;
            var context = new CasbinDbContext<int>(options);

            // If it doesn't exist, you can use this to create it automatically.
            context.Database.EnsureCreated();

            // Initialize a EF Core adapter and use it in a Casbin enforcer:
            var efCoreAdapter = new EFCoreAdapter<int>(context);
            var e = new Enforcer("examples/rbac_model.conf", efCoreAdapter);

            // Load the policy from DB.
            e.LoadPolicy();

            // Check the permission.
            e.Enforce("alice", "data1", "read");
            
            // Modify the policy.
            // e.AddPolicy(...)
            // e.RemovePolicy(...)
	
            // Save the policy back to DB.
            e.SavePolicy();
        }
    }
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

efcore-adapter's People

Contributors

arthuridea avatar asakusarinne avatar crskycode avatar dacongda avatar hsluoyz avatar huazhikui avatar pprometey avatar sagilio avatar samchenws avatar skmonkeyway avatar sociometry avatar tanyuu avatar xcaptain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

efcore-adapter's Issues

RBAC and argument out of range exception

Hello;

When using this adapter (code from master branch) and deleting policy, I am getting exception.
The same configuration and code is working as expected when using only in memory Enforcer and Model.

Any help would be much appreciated.

[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act, eft

[role_definition]
g = _, _, _
g2 = _, _, _

[policy_effect]
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))

[matchers]
m = g(r.sub, p.sub, r.dom) && g2(r.obj, p.obj, r.dom) && r.act == p.act && r.dom == p.dom

And test:

	var policyAdded = enforcer.AddNamedPolicy("p", "alice", "orga1", "site1:orga1", "READ", "allow");
	var policyRemoved = enforcer.RemoveNamedPolicy("p", "alice", "orga1", "site1:orga1", "READ", "allow");

I am getting ArgumentOutOfRange exception:
Message:
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

StackTrace:

   at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
   at System.SZArrayHelper.get_Item[T](Int32 index)
   at Casbin.Adapter.EFCore.Extensions.CasbinRuleExtenstion.ApplyQueryFilter[TCasbinRule](IQueryable`1 query, String policyType, Int32 fieldIndex, IEnumerable`1 fieldValues) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\Extensions\CasbinRuleExtenstion.cs:line 131
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemoveFilteredPolicyInMemory(String section, String policyType, Int32 fieldIndex, String[] fieldValues) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 320
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemoveFilteredPolicy(String section, String policyType, Int32 fieldIndex, String[] fieldValues) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 233
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemovePolicyInMemory(String section, String policyType, IEnumerable`1 rule) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 315
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemovePolicy(String section, String policyType, IList`1 rule) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 211
   at NetCasbin.InternalEnforcer.InternalRemovePolicy(String sec, String ptype, List`1 rule)
   at NetCasbin.ManagementEnforcer.RemoveNamedPolicy(String ptype, List`1 parameters)
   at NetCasbin.ManagementEnforcer.RemoveNamedPolicy(String ptype, String[] parameters)
   at Casbin.Adapter.EFCore.UnitTest.AdapterTest.TestAddRemoveNamedPolicy() in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore.UnitTest\AdapterTest.cs:line 284

Code location:
Class CasbinRuleExtension.cs
Method

 internal static IQueryable<TCasbinRule> ApplyQueryFilter<TCasbinRule>(this IQueryable<TCasbinRule> query, 
            string policyType , int fieldIndex, IEnumerable<string> fieldValues)
            where TCasbinRule : ICasbinRule

Failing line (fieldValueList count is 5, index is 5-0):

if (lastIndex is 5)
            {
                string field = fieldValuesList[5 - fieldIndex]; //here
                if (string.IsNullOrWhiteSpace(field) is false)
                {
                    query = query.Where(p => p.V5 == field);
                }
            }

LoadFilteredPolicy not working when both P and G are defined for Filter

I'm trying to load a subset of policies similar to the example shown here in the Casbin docs:
https://casbin.org/docs/en/policy-subset-loading

I have an RBAC model with domains and I want to do exactly what is shown in this example where I can load a filtered policy with everything from "domain1". I have 1 "g" group/role defined for "domain1" and then I've given that group permission to 2 objects also in "domain1"...so my casbin_rule table has 3 rows: 1 "g" row and 2 "p" rows.

After I was having issues, I stepped through the LoadFilteredPolicy code, and determined that

  • If I set it with only the P filter, it loads the 2 rows from casbin_rule
  • if I set it with only the G filter, it loads the 1 row from casbin_rule
  • if I set up a filter with both "P" and "G" set to values, the function loads with 0 rows.

The reason is because in CasbinRuleExtension.ApplyQueryFilter, it performs the following code

            if (filter.P is not null)
            {
                query = query.ApplyQueryFilter(PermConstants.DefaultPolicyType, 0, filter.P);
            }

            if (filter.G is not null)
            {
                query = query.ApplyQueryFilter(PermConstants.DefaultGroupingPolicyType, 0, filter.G);
            }

This results in a query that takes the the P filter AND the G filter. This will never return any values. In order for both to work, there would have to be an OR instead of AND.

As I mentioned above, based on the example here: https://casbin.org/docs/en/policy-subset-loading, it seems like this should work as I've described where it does an OR between the 2 filters. Is this a bug? Is there a workaround you can provide?

Thanks!

filtered policies are not supported by this adapter

Hi Team,

I get the below exception when I call the method enforcer.LoadFilteredPolicy(filter) method.
I am filtering based on the below code, Please see is this correct?

                          NetCasbin.Persist.Filter filter = new NetCasbin.Persist.Filter()
                                {
                                    P = new List<string> { "", "", "", "Edit" }
                               };
                                  enforcer.LoadFilteredPolicy(filter);

in the DB, I have metnion "Edit" in the 3rd column i.e, V3

         Id	PType	V0	    V1	 V2	         V3	          V4	         V5
        253	p	      Alice	    data2	Write	Edit   	NULL	NULL
        254	p	      Alice	    data1	read 	Read  	NULL	NULL
  $exception	{"filtered policies are not supported by this adapter"}	System.Exception

{Casbin.NET.Adapter.EFCore.CasbinDbAdapter<int>}
        /// <summary>
        /// Returns true if the loaded policy has been filtered.
        /// </summary>
        /// <returns>if the loaded policy has been filtered.</returns>
        public bool IsFiltered()
        {
            if (adapter is IFilteredAdapter filteredAdapter)
            {
                return filteredAdapter.IsFiltered;
            }
            return false;
        }

Upgrade to net6

Hey guys, what are your plans regarding upgrading to net6? Migration seems straightforward. I migrated locally (including tests) but could not push / create a PR. :)

Example on how to support ef-core migrations and custom table name

Needing some help - finding quite some hard time to add it to a project with Top level statements.
What I am looking for is

  1. Support ef-core migrations - something like
dotnet ef migrations add CasbinSupport --context MYSQLCasbinDbContext --output-dir Migrations/MYSQLMigrations --verbose
  1. Customize table name somewhere

What I am doing now

  1. Subclassing
public class MYSQLCasbinDbContext : CasbinDbContext<int>
    {
        protected readonly IConfiguration _configuration;
        public MYSQLCasbinDbContext(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public MYSQLCasbinDbContext(DbContextOptions<MYSQLCasbinDbContext> options, IConfiguration configuration) : base(options)
        {
            _configuration = configuration;
        }

        public MYSQLCasbinDbContext(DbContextOptions<MYSQLCasbinDbContext> options, IConfiguration configuration, string defaultSchemaName = null, string defaultTableName = "auth_casbin_rule")
    : base(options, defaultSchemaName, defaultTableName)
        {
            _configuration = configuration;
        }

        protected MYSQLCasbinDbContext(DbContextOptions options, IConfiguration configuration, string defaultSchemaName = null, string defaultTableName = "auth_casbin_rule")
            : base(options, defaultSchemaName, defaultTableName)
        {
            _configuration = configuration;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionString = _configuration.GetConnectionString("MYSQL");
            var serverVersion = new MariaDbServerVersion("10.9.4");
            optionsBuilder.UseMySql(connectionString, serverVersion)
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging()
                .EnableDetailedErrors();
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
  1. Configuring Program.cs
builder.Services.AddDbContext<CasbinDbContext<int>, MYSQLCasbinDbContext>(optionsBuilder =>
{
}).AddScoped<IAdapter, EFCoreAdapter<int>>();

I have no idea if it is proper / creating other issues

Release of v2

Hi efcore-adapter Team,

we would like to build our AuthZ custom solution based on Casbin.NET version 2. As we use the EFCoreAdapter to store/load the policies we are wondering if you could share with use the release date of the EFCoreAdapter v2.0.0. Is there any roadmap or additional information about the release date?

Best regards
George

Unable resolve CasbinDbContext

services.AddDbContext<CasbinDbContext<int>>(options =>
            {
                DbContextOptionsBuilder optionsBuilder = null;
                switch (type.ToLower())
                {
                    case "mysql":
                        optionsBuilder = options.UseMySQL(connectionString);
                        break;
                    case "sqlite3":
                    default:
                        optionsBuilder = options.UseSqlite(connectionString);
                        break;
                }
                var context = new CasbinDbContext<int>(optionsBuilder.Options);
                context.Database.EnsureCreated();
            }, ServiceLifetime.Transient);
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Casbin.Adapter.EFCore.CasbinDbContext`1[System.Int32] Lifetime: Transient ImplementationType: Casbin.Adapter.EFCore.CasbinDbContext`1[System.Int32]': Unable to activate type 'Casbin.Adapter.EFCore.CasbinDbContext`1[System.Int32]'. The following constructors are ambiguous:
Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions, System.String, System.String)
Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[Casbin.Adapter.EFCore.CasbinDbContext`1[System.Int32]], System.String, System.String))

EFCore adapter performance and caching [Question]

Hi,

I apologize if my question may sounds simple or if I have overlooked something, or if this is the wrong place to ask this question. We are using Casbin.Net for performing authorization library for our microservice project. We also use MassTransit and we need to perform authorization of commands. For that we are implemented custom filters where we make calls to the authorization service.
If we are regsitering the services as shown in the readme of this repo then a single DBContext and the enforcers are singleton for each request. In this scenario basically DBContext becomes singleton and after first query all policies from DB are loaded and cached in-memory. The first problem that we are facing is that in this scenario production use (100+ users) a lot of concurrent updates happen and we are getting EF errors saying that the DBContext cannot be used by different threads, which totally logical. If we change the service registration and a new DContext is constructed for each request we are hitting performance issues as each time it has to read all policies from the database.
To be honest I didn't find a proper place to hook in the caching in the EFCore adaptor, and that only leaves us with options to hook into EF Core's in-memeory "shared cache" or cache the command results at MassTransit level. Am I missing something? What production level best-practices are there for Casbin.net and EFCore Adapter in-terms of performance?

Thanks for your patience!

Roadmap for this project

This project is still under development, but basic RBAC functions have been tested on my own
projects, but still a lot of things need to be done, includes:

  • RemoveFilteredPolicy api
  • SavePolicy api
  • Make CasbinRule model configurable from application code, which means the table name, the column names can be changed. see #6
  • more unit tests #7
  • A consistent package id and namespace to publish to nuget or github package registry
  • Update to .NET Core and EFCore 3.1 #12
  • Code coverage badge doesn't work yet.
  • No README.
  • No nuget badge, download times badge.

I'm a little busy in the day time but would try to optimize this project

Add constructor to CasbinDbContext<TKey> to help using ef core migrations

I'm trying to use Casbin.Net with PostgreSQL persistence. We're trying to use efcore migrations to create the database and the table(s), and we're having some issues since efcore migrations does not work with database contexts with type parameters such as

CasbinDbContext<Guid>

The dotnet ef migration tooling simply will not detect/find the dbContext, and will fail with:

No DbContext named 'CasbinDbContext<Guid>' was found.

So I tried to create a derived type as shown under.

    /// <summary>
    /// Create Verji version of the CasbinDbContext without type parameters
    /// </summary>
    public class VerjiCasbinDbContext : CasbinDbContext<Guid>
    {
        /// <inheritdoc />
        public VerjiCasbinDbContext(DbContextOptions<VerjiCasbinDbContext> options)
            : base(options)
        {
        }
    }

This does not work since the base class CasbinDbContext<Guid> does not have a constructor which takes a non-generic DbContextOptions parameter.

Would it be possible to add such a constructor to CasbinDbContext<TKey> ?

Something like:

 public class CasbinDbContext<TKey> : DbContext where TKey : IEquatable<TKey>
    {
        private readonly IEntityTypeConfiguration<Casbin.Adapter.EFCore.Entities.CasbinRule<TKey>> _casbinModelConfig;
        private readonly string _defaultSchemaName;

        public virtual DbSet<Casbin.Adapter.EFCore.Entities.CasbinRule<TKey>> CasbinRule { get; set; }

        public CasbinDbContext2()
        {
        }

        // New constructor 
        public CasbinDbContext(DbContextOptions options) : base(options) { }

        ...
    }

Duplicate Policy Records when AutoSave=false

I think the issue raised by others before still exists. And that's the question:Casbin.Adapter.EFCore Results in Duplicate Policy Records in PostgreSQL Database #65.

I did not test PostgreSQL , but I tested SQLIite and SQLServer and found that AutoSave works normally. When AutoSave=false, the problem reappears. I am using it NET6+Casbin. NET Adapters EFCore 2.1.0.
Steps to reproduce the Issue:

  1. AutoSave=true
  2. Call AddPolicy to add PolicyA, PolicyB, PolicyC
  3. Call SavePolicy(). The database has three records: PolicyA, PolicyB, and PolicyC.
  4. Call RemovePolicy to remove PolicyC
  5. Call SavePolicy(). The database has five records: PolicyA, PolicyB, PolicyC, PolicyA, PolicyB.
    At this point, the issue reappears, and duplicate PolicyA and PolicyB will be found. PolicyC was also not inserted into the DB for the second time, so the permission of PolicyC still exists.

Another detail issue is that I directly use Visual Studio to retrieve Cabin. NET from the Nuget repository Adapters EFCore 2.1.0, cannot use AutoSave=true (i.e. default). Nuget URL is https://www.nuget.org/packages/Casbin.NET.Adapter.EFCore/2.1.0?_src=template.
This will generate an exception when calling the AddPolicy method of Enforcer. The exception is: System.EntryPointNotFoundException:“Entry point was not found.”
The details of exception is:
  | StackTrace | at System.Collections.Generic.IReadOnlyCollection1.get_Count() at Casbin.Persist.Adapter.EFCore.EFCoreAdapter3.AddPolicy(String section, String policyType, IPolicyValues values)
at Casbin.Model.DefaultPolicyManager.AddPolicy(IPolicyValues values)
at Casbin.ManagementEnforcerExtension.AddPolicy(IEnforcer enforcer, String[] values)
at Casbin.NETDemo.Form1.Test2() in C:\ZData\mywork\VS2022\Casbin.NETDemo\Casbin.NETDemo\Form1.cs:line 59
at Casbin.NETDemo.Form1.button1_Click(Object sender, EventArgs e) in C:\ZData\mywork\VS2022\Casbin.NETDemo\Casbin.NETDemo\Form1.cs:line 22
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

But I downloaded the latest source code of Casbin. NET Adapters EFCore and compiled it to use in the project.
At this point, AutoSave=true can be used correctly and the above exception will not occur.

Unable to save policy.

model:

# Request definition
[request_definition]
r = sub, obj, act

# Policy definition
[policy_definition]
p = sub, obj, act

# Policy effect
[policy_effect]
e = some(where (p.eft == allow))

# Matchers
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act

Hello,
I am trying to follow your example here and I get an exception on saving.

My code:

// You should build a DbContextOptions for CasbinDbContext<TKey>.
            // The example use the SQLite database named "casbin_example.sqlite3".
            var options = new DbContextOptionsBuilder<CasbinDbContext<int>>()
                .UseSqlite("Data Source=casbin_example.sqlite3")
                .Options;
            var context = new CasbinDbContext<int>(options);

            // If it doesn't exist, you can use this to create it automatically.
            context.Database.EnsureCreated();

            // Initialize a EF Core adapter and use it in a Casbin enforcer:
            var efCoreAdapter = new EFCoreAdapter<int>(context);
            var e = new Enforcer("files/acl.conf", efCoreAdapter);

            // Load the policy from DB.
            e.LoadPolicy();

            e.AddPolicy("alice", "data1", "read");

            // Save the policy back to DB.
            e.SavePolicy();

xUnit.net 00:00:01.57] System.InvalidOperationException : Cannot save a filtered policy
[xUnit.net 00:00:01.57] Stack Trace:
[xUnit.net 00:00:01.57] at NetCasbin.CoreEnforcer.SavePolicy()

CasbinRule table name - consistency across projects

I have two projects, one in .Net and a Python project.

Both are going to point to the same DB, and share the same casbin table.

I just noticed that in .net the DB table is 'CasbinRule' and the python one is 'casbin_rule' - so the two projects aren't actually using the same table.

Is there a good way to set them both to the same table name?

I am using:
for Python, the SQLAlchemy adapter - https://github.com/pycasbin/sqlalchemy-adapter
for .NET, the EFCore adapter - https://github.com/casbin-net/EFCore-Adapter

Thank you

nuget package dependency requires sqlsever related package in version 1.2.0

In version 1.2.0,the dependency package "Microsoft.EntityFrameworkCore.Design" is replaced with "Microsoft.EntityFrameworkCore.SqlServer".the latter one only works with Microsoft sqlserver database. Is it possible to rollback to "Microsoft.EntityFrameworkCore.Design" which provide an enough support with design-time feature?

Enforcer instances in different processes, different regions

Hello!

Once again, I apologize if there's information I overlooked, or if this is the wrong place to ask this question. As I understand things, Cosmos handles replication across regions, so any policies added or removed in one region will replicate to all other regions (assuming that's what we want and assuming we've configured things correctly).

Is there anything inherent in this adapter that facilitates this behavior? i.e. If I write a policy in Europe, Cosmos will replicate that policy to the US, Asia, etc. - does this adapter ensure that calls to Enforce() hit Cosmos if caching is disabled, absent a preceding call to LoadPolicy()?

Or do I need to watch for changes to the database/container separately, triggering calls to LoadPolicyAsync() in response?

Thanks for your patience!
John

Exception thrown while trying to add policy using Cosmos DB

Hello!

First, I apologize if I've overlooked something in the examples or elsewhere, though I spent the better part of yesterday looking. Starting with the example in the readme, I've modified it to use Cosmos:

let options = DbContextOptionsBuilder<CasbinDbContext<int>>()
                      .UseCosmos("https://localhost:8081", "key","CasbinAuthorizationDB")
                      .Options

let context = new CasbinDbContext<int>(options)
let! _ = context.Database.EnsureCreatedAsync () |> Async.AwaitTask

let model = Model.CreateDefaultFromFile "examples/rbac_model.conf"
let efCoreAdapter = EFCoreAdapter<int>(context);
let e = Enforcer(model, efCoreAdapter);

This works correctly, in that I end up with a database and container. However, this next bit fails:

let! result = e.AddPolicyAsync ("alice", "data1", "admin") |> Async.AwaitTask

And it throws this:

The property 'CasbinRule<int>.Id' does not have a value set and no value generator is available for properties of type 'int'. Either set a value for the property before adding the entity or configure a value generator for properties of type 'int' in 'OnModelCreating'.

I can see from poking around some of the EF (which I am utterly unfamiliar with) and Cosmos documentation that something ought be generating the IDs - however, I assumed the adapter would do that, as it's providing the class (CasbinRule) to use.

Am I wrong in my assumptions? And can anyone suggest how to resolve this?

Thanks!
John

Casbin.Adapter.EFCore Results in Duplicate Policy Records in PostgreSQL Database

Issue Description:

When using the EFCore adapter (Casbin.Adapter.EFCore) with Casbin.NET and PostgreSQL as the database, I noticed that after calling AddPolicy() and SavePolicy(), duplicate policy records are present in the database. I am certain that I am not calling AddPolicy() multiple times in my code.

Steps to Reproduce:

Configure Casbin.NET to use the Casbin.Adapter.EFCore adapter and connect to a PostgreSQL database.
Call AddPolicy() in the code to add a new policy.
Call SavePolicy() to save the changes.
Check the database and observe duplicate policy records.
Expected Behavior:

There should be only one new policy record in the database.

Actual Behavior:

There are two identical policy records in the database.

Environment Information:

Operating System: [e.g., MacOS]
.NET Version: [e.g., .NET 6.0]
Casbin.NET Version: [e.g., 1.14.0 ]
Casbin.NET.Adapter.EFCore Version: [e.g., 1.8.1]
Casbin.AspNetCore.Core Version : [e.g., 0.2.1]
PostgreSQL Version: [e.g., 14.4]
Any other libraries or frameworks that might be relevant

Here is how I register the service

Code Sample:

public static class CasbinConfigurationExtensions
{
    public static IServiceCollection AddCustomCasbinConfiguration(this IServiceCollection services, IConfiguration configuration)
    {
        var options = new DbContextOptionsBuilder<CasbinDbContext<long>>()
            .UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
            .Options;
        var context = new CasbinDbContext<long>(options, defaultSchemaName: "manage");
        context.Database.EnsureCreated();
        services.AddSingleton<Enforcer>(provider =>
        {
            var efCoreAdapter = new EFCoreAdapter<long>(context);
            var e = new Enforcer("./CasbinConfigs/basic_model.conf", efCoreAdapter);
            e.LoadPolicy();
            return e;
        });

        return services;
    }
}

Program.cs

builder.Services.AddCustomCasbinConfiguration(configuration);

DI

 public class XXXService : IXXXService
{
    private readonly Enforcer _enforcer;
    public XXXService( Enforcer enforcer)
    {
  
        _enforcer = enforcer;
    }
}

main

 _enforcer.AddPolicy(roleId, path, method);
  _enforcer.SavePolicy();

SQL Adapter it throws error on loadfilter "The data types text and varchar are incompatible in the equal to operator."

Hi Team,
I am using enforcer.LoadFilterPolicy with filter, it throws error " the data type text and varchar are incompatible".
I debugged further and the issue is the DB is text field and it takes nVarchar() for comparing and "=" is used. I have corrected the code locally to work and I can give the fix as given below>

// Error while locading:
enforcer.LoadFilteredPolicy(new Filter
            {
                P = new List<string> { "", "", "","domainname" },
            });

Degug Query result:

((Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<Casbin.NET.Adapter.EFCore.CasbinRule<int>>)query).DebugView.Query
DECLARE @__policyType_0 nvarchar(4000) = N'p';
DECLARE @__field_1 nvarchar(4000) = N'prledit';

DECLARE @__policyType_0 nvarchar(4000) = N'p';
DECLARE @__field_1 nvarchar(4000) = N'prledit';

**SELECT [c].[Id], [c].[PType], [c].[V0], [c].[V1], [c].[V2], [c].[V3], [c].[V4], [c].[V5]
FROM [CasbinRule] AS [c]
WHERE ([c].[PType] = @__policyType_0) AND ([c].[V3] = @__field_1)**
Error:
The data types text and varchar are incompatible in the equal to operator.

Corrected Query:
_**SELECT [c].[Id], [c].[PType], [c].[V0], [c].[V1], [c].[V2], [c].[V3], [c].[V4], [c].[V5]
FROM [CasbinRule] AS [c]
WHERE ((@__policyType_0 LIKE N'') OR (CHARINDEX(@__policyType_0, [c].[PType]) > 0)) AND ((@__field_1 LIKE N'') OR (CHARINDEX(@__field_1, [c].[V3]) > 0))**_

Code changes Required:

CasbinRulesExtension.cs file Line No 81.

      Error causing line: query = query.Where(p => string.Equals(p.PType, policyType));
        Corrected Code:  query = query.Where(p => p.PType.Contains(policyType));

Similarly for other indexes as highlighted here:

 if (fieldIndex <= 2 && lastIndex > 2)
            {
                string field = fieldValuesList[2 - fieldIndex];
                if (string.IsNullOrWhiteSpace(field) is false)
                {
                    **query = query.Where(p => p.V2.Contains(field));**
                }
            }

Actual error message:
Microsoft.Data.SqlClient.SqlException: 'The data types text and nvarchar are incompatible in the equal to operator.'

Thanks
siva

DeleteRoleForUser is not working

Hi, I am using EFAdapter with cosmos DB, and seems the method DeleteRoleForUser is not working.
I am using SavePolicy after calling DeleteRoleForUser(user, role, domain);

is there something I can do?

Cannot be used with Casbin.NET package > 2.1.1

Hi, we are evaluating casbin for a .NET project that we are starting out.

We want to store policies in a database (thinking about postgres) and using Casbin.NET.Adapter.EFCore v2.1.0 and Npgsql.EntityFrameworkCore.PostgreSQL v8.0.2.

If we are a version of the package Casbin.NET higher that 2.1.1 we get the following exception:

System.EntryPointNotFoundException: Entry point was not found.
         at System.Collections.Generic.IReadOnlyCollection`1.get_Count()
         at Casbin.Persist.Adapter.EFCore.EFCoreAdapter`3.AddPolicyAsync(String section, String policyType, IPolicyValues values)
         at Casbin.Model.DefaultPolicyManager.AddPolicyAsync(IPolicyValues rule)
         at Casbin.InternalEnforcerExtension.InternalAddPolicyAsync(IEnforcer enforcer, String section, String policyType, IPolicyValues values)

Of course we can evaluate the solution with a 5 month-old version, but that would be a showstopper to continue with casbin.

I am not sure what could be the solution for this problem.

Thanks for your help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.