From 220951b75a383732d57bd73656a68f2d873cca70 Mon Sep 17 00:00:00 2001 From: Cameron Hurst Date: Tue, 11 Aug 2020 07:05:55 -0400 Subject: [PATCH] feat: added support for filtered policies Signed-off-by: Cameron Hurst --- casbin/core_enforcer.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/casbin/core_enforcer.py b/casbin/core_enforcer.py index d705f68b..fd2efe93 100644 --- a/casbin/core_enforcer.py +++ b/casbin/core_enforcer.py @@ -59,7 +59,7 @@ def init_with_model_and_adapter(self, m, adapter=None): self._initialize() # Do not initialize the full policy when using a filtered adapter - if self.adapter: + if self.adapter and not self.is_filtered(): self.load_policy() def _initialize(self): @@ -147,13 +147,20 @@ def load_policy(self): def load_filtered_policy(self, filter): """reloads a filtered policy from file/database.""" + self.model.clear_policy() - pass + if not hasattr(self.adapter, "is_filtered"): + raise ValueError("filtered policies are not supported by this adapter") + + self.adapter.load_filtered_policy(self.model, filter) + self.model.print_policy() + if self.auto_build_role_links: + self.build_role_links() def is_filtered(self): """returns true if the loaded policy has been filtered.""" - pass + return hasattr(self.adapter, "is_filtered") and self.adapter.is_filtered() def save_policy(self): if self.is_filtered():