Skip to content

Commit

Permalink
fix: duplicate add and save
Browse files Browse the repository at this point in the history
  • Loading branch information
dacongda committed Apr 4, 2024
1 parent 2261440 commit 3084dd7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Casbin.NET" Version="2.5.0" />
<PackageReference Include="Casbin.NET" Version="2.5.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
Expand Down
26 changes: 26 additions & 0 deletions Casbin.Persist.Adapter.EFCore/EFCoreAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public virtual void SavePolicy(IPolicyStore store)
return;
}

var existRule = PersistPolicies.ToList();
PersistPolicies.RemoveRange(existRule);
DbContext.SaveChanges();

var saveRules = OnSavePolicy(store, persistPolicies);
PersistPolicies.AddRange(saveRules);
DbContext.SaveChanges();
Expand All @@ -94,6 +98,10 @@ public virtual async Task SavePolicyAsync(IPolicyStore store)
return;
}

var existRule = PersistPolicies.ToList();
PersistPolicies.RemoveRange(existRule);
await DbContext.SaveChangesAsync();

var saveRules = OnSavePolicy(store, persistPolicies);
await PersistPolicies.AddRangeAsync(saveRules);
await DbContext.SaveChangesAsync();
Expand All @@ -109,6 +117,15 @@ public virtual void AddPolicy(string section, string policyType, IPolicyValues v
{
return;
}

var filter = new PolicyFilter(policyType, 0, values);
var persistPolicies = filter.Apply(PersistPolicies);

if (persistPolicies.Any())
{
return;
}

InternalAddPolicy(section, policyType, values);
DbContext.SaveChanges();
}
Expand All @@ -119,6 +136,15 @@ public virtual async Task AddPolicyAsync(string section, string policyType, IPol
{
return;
}

var filter = new PolicyFilter(policyType, 0, values);
var persistPolicies = filter.Apply(PersistPolicies);

if (persistPolicies.Any())
{
return;
}

await InternalAddPolicyAsync(section, policyType, values);
await DbContext.SaveChangesAsync();
}
Expand Down

0 comments on commit 3084dd7

Please # to comment.