22from casbin import persist
33from pymongo import MongoClient
44
5- class CasbinRule () :
5+ class CasbinRule :
66 '''
77 CasbinRule model
88 '''
9+ ptype = None
10+ v0 = None
11+ v1 = None
12+ v2 = None
13+ v3 = None
14+ v4 = None
15+ v5 = None
916
10- __tablename__ = "casbin_rule"
11-
12- def __init__ (self , ptype = '' , v0 = '' , v1 = '' , v2 = '' , v3 = '' , v4 = '' , v5 = '' ):
17+ def __init__ (self , ptype ):
1318 self .ptype = ptype
14- self .v0 = v0
15- self .v1 = v1
16- self .v2 = v2
17- self .v3 = v3
18- self .v4 = v4
19- self .v5 = v5
19+
20+ def dict (self ):
21+ d = {'ptype' : self .ptype }
22+
23+ for i , v in enumerate ([self .v0 , self .v1 , self .v2 , self .v3 , self .v4 , self .v5 ]):
24+ if v is None :
25+ break
26+ d ['v' + str (i )] = v
27+
28+ return d
2029
2130 def __str__ (self ):
22- dict = {'ptype' :self .ptype }
23- if self .v0 :
24- dict ['v0' ] = self .v0
25- if self .v1 :
26- dict ['v1' ] = self .v1
27- if self .v2 :
28- dict ['v2' ] = self .v2
29- if self .v3 :
30- dict ['v3' ] = self .v3
31- if self .v4 :
32- dict ['v4' ] = self .v4
33- if self .v5 :
34- dict ['v5' ] = self .v5
35- return dict
31+ return ', ' .join (self .dict ().values ())
3632
3733 def __repr__ (self ):
3834 return '<CasbinRule :"{}">' .format (str (self ))
3935
40-
4136class Adapter (persist .Adapter ):
4237 """the interface for Casbin adapters."""
4338
@@ -51,8 +46,26 @@ def load_policy(self, model):
5146 implementing add Interface for casbin \n
5247 load all policy rules from mongodb \n
5348 '''
54- for lines in self ._collection .find ():
55- persist .load_policy_line (str (lines ), model )
49+
50+ for line in self ._collection .find ():
51+ if 'ptype' not in line :
52+ continue
53+
54+ rule = CasbinRule (line ['ptype' ])
55+ if 'v0' in line :
56+ rule .v0 = line ['v0' ]
57+ if 'v1' in line :
58+ rule .v1 = line ['v1' ]
59+ if 'v2' in line :
60+ rule .v2 = line ['v2' ]
61+ if 'v3' in line :
62+ rule .v3 = line ['v3' ]
63+ if 'v4' in line :
64+ rule .v4 = line ['v4' ]
65+ if 'v5' in line :
66+ rule .v5 = line ['v5' ]
67+
68+ persist .load_policy_line (str (rule ), model )
5669
5770 def _save_policy_line (self , ptype , rule ):
5871 line = CasbinRule (ptype = ptype )
@@ -68,24 +81,40 @@ def _save_policy_line(self, ptype, rule):
6881 line .v4 = rule [4 ]
6982 if len (rule ) > 5 :
7083 line .v5 = rule [5 ]
71- self ._collection .insert_one (line .__str__ () )
84+ self ._collection .insert_one (line .dict )
7285
7386 def save_policy (self , model ):
7487 '''
7588 implementing add Interface for casbin \n
7689 save the policy in mongodb \n
7790 '''
78- for sec in ["p" , "g" ]:
79- if sec not in model .model .keys ():
80- continue
81- for ptype , ast in model .model [sec ].items ():
82- for rule in ast .policy :
83- self ._save_policy_line (ptype , rule )
84- return True
91+ # for sec in ["p", "g"]:
92+ # if sec not in model.model.keys():
93+ # continue
94+ # for ptype, ast in model.model[sec].items():
95+ # for rule in ast.policy:
96+ # self._save_policy_line(ptype, rule)
97+ # return True
98+ self ._collection .insert_one (model .dict )
8599
86100 def add_policy (self , sec , ptype , rule ):
87101 """add policy rules to mongodb"""
88- self ._save_policy_line (ptype , rule )
102+ # self._save_policy_line(ptype, rule)
103+ line = CasbinRule (ptype = ptype )
104+ if len (rule ) > 0 :
105+ line .v0 = rule [0 ]
106+ if len (rule ) > 1 :
107+ line .v1 = rule [1 ]
108+ if len (rule ) > 2 :
109+ line .v2 = rule [2 ]
110+ if len (rule ) > 3 :
111+ line .v3 = rule [3 ]
112+ if len (rule ) > 4 :
113+ line .v4 = rule [4 ]
114+ if len (rule ) > 5 :
115+ line .v5 = rule [5 ]
116+ self .save_policy (line )
117+
89118
90119 def remove_policy (self , sec , ptype , rule ):
91120 """delete policy rules from mongodb"""
0 commit comments